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;
271 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
272 err = got_error_from_errno("socketpair");
278 err = got_error_from_errno("fork");
280 } else if (pid == 0) {
282 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
283 pack->path_packfile);
287 if (close(imsg_fds[1]) == -1)
288 return got_error_from_errno("close");
289 pack->privsep_child->imsg_fd = imsg_fds[0];
290 pack->privsep_child->pid = pid;
291 imsg_init(ibuf, imsg_fds[0]);
292 pack->privsep_child->ibuf = ibuf;
294 err = got_privsep_init_pack_child(ibuf, pack, packidx);
296 const struct got_error *child_err;
297 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
298 child_err = got_privsep_wait_for_child(
299 pack->privsep_child->pid);
300 if (child_err && err == NULL)
306 free(pack->privsep_child);
307 pack->privsep_child = NULL;
312 static const struct got_error *
313 read_packed_object_privsep(struct got_object **obj,
314 struct got_repository *repo, struct got_pack *pack,
315 struct got_packidx *packidx, int idx, struct got_object_id *id)
317 const struct got_error *err = NULL;
319 if (pack->privsep_child == NULL) {
320 err = start_pack_privsep_child(pack, packidx);
325 return request_packed_object(obj, pack, idx, id);
328 static const struct got_error *
329 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
330 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
331 struct got_object_id *id)
333 const struct got_error *err = NULL;
335 if (pack->privsep_child == NULL) {
336 err = start_pack_privsep_child(pack, packidx);
341 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
345 const struct got_error *
346 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
347 struct got_repository *repo)
349 const struct got_error *err = NULL;
350 struct got_pack *pack = NULL;
351 struct got_packidx *packidx = NULL;
355 err = got_repo_search_packidx(&packidx, &idx, repo, id);
359 err = got_packidx_get_packfile_path(&path_packfile,
360 packidx->path_packidx);
364 pack = got_repo_get_cached_pack(repo, path_packfile);
366 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
371 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
379 static const struct got_error *
380 request_object(struct got_object **obj, struct got_object_id *id,
381 struct got_repository *repo, int fd)
383 const struct got_error *err = NULL;
384 struct imsgbuf *ibuf;
386 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
388 err = got_privsep_send_obj_req(ibuf, fd, id);
392 return got_privsep_recv_obj(obj, ibuf);
395 static const struct got_error *
396 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
397 struct got_object_id *id, struct got_repository *repo, int infd)
399 const struct got_error *err = NULL;
400 struct imsgbuf *ibuf;
403 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
405 outfd_child = dup(outfd);
406 if (outfd_child == -1)
407 return got_error_from_errno("dup");
409 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
413 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
417 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
420 static const struct got_error *
421 start_read_object_child(struct got_repository *repo)
423 const struct got_error *err = NULL;
426 struct imsgbuf *ibuf;
428 ibuf = calloc(1, sizeof(*ibuf));
430 return got_error_from_errno("calloc");
432 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
433 err = got_error_from_errno("socketpair");
440 err = got_error_from_errno("fork");
445 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
450 if (close(imsg_fds[1]) == -1) {
451 err = got_error_from_errno("close");
456 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
458 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
459 imsg_init(ibuf, imsg_fds[0]);
460 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
465 const struct got_error *
466 got_object_read_header_privsep(struct got_object **obj,
467 struct got_object_id *id, struct got_repository *repo, int obj_fd)
469 const struct got_error *err;
471 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
472 return request_object(obj, id, repo, obj_fd);
474 err = start_read_object_child(repo);
480 return request_object(obj, id, repo, obj_fd);
483 static const struct got_error *
484 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
485 int outfd, struct got_object_id *id, struct got_repository *repo,
488 const struct got_error *err;
490 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
491 return request_raw_object(outbuf, size, hdrlen, outfd, id,
494 err = start_read_object_child(repo);
498 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
502 const struct got_error *
503 got_object_open(struct got_object **obj, struct got_repository *repo,
504 struct got_object_id *id)
506 const struct got_error *err = NULL;
509 *obj = got_repo_get_cached_object(repo, id);
515 err = got_object_open_packed(obj, id, repo);
516 if (err && err->code != GOT_ERR_NO_OBJ)
520 return got_repo_cache_object(repo, id, *obj);
523 err = got_object_open_loose_fd(&fd, id, repo);
525 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
526 err = got_error_no_obj(id);
530 err = got_object_read_header_privsep(obj, id, repo, fd);
534 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
537 return got_repo_cache_object(repo, id, *obj);
540 /* *outfd must be initialized to -1 by caller */
541 const struct got_error *
542 got_object_raw_open(struct got_raw_object **obj, int *outfd,
543 struct got_repository *repo, struct got_object_id *id)
545 const struct got_error *err = NULL;
546 struct got_packidx *packidx = NULL;
548 uint8_t *outbuf = NULL;
551 char *path_packfile = NULL;
553 *obj = got_repo_get_cached_raw_object(repo, id);
560 *outfd = got_opentempfd();
562 return got_error_from_errno("got_opentempfd");
565 err = got_repo_search_packidx(&packidx, &idx, repo, id);
567 struct got_pack *pack = NULL;
569 err = got_packidx_get_packfile_path(&path_packfile,
570 packidx->path_packidx);
574 pack = got_repo_get_cached_pack(repo, path_packfile);
576 err = got_repo_cache_pack(&pack, repo, path_packfile,
581 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
582 *outfd, pack, packidx, idx, id);
585 } else if (err->code == GOT_ERR_NO_OBJ) {
588 err = got_object_open_loose_fd(&fd, id, repo);
591 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
597 *obj = calloc(1, sizeof(**obj));
599 err = got_error_from_errno("calloc");
605 (*obj)->data = outbuf;
608 if (fstat(*outfd, &sb) == -1) {
609 err = got_error_from_errno("fstat");
613 if (sb.st_size != hdrlen + size) {
614 err = got_error(GOT_ERR_PRIVSEP_LEN);
617 #ifndef GOT_PACK_NO_MMAP
618 if (hdrlen + size > 0) {
619 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
620 MAP_PRIVATE, *outfd, 0);
621 if ((*obj)->data == MAP_FAILED) {
622 if (errno != ENOMEM) {
623 err = got_error_from_errno("mmap");
634 (*obj)->f = fdopen(*outfd, "r");
635 if ((*obj)->f == NULL) {
636 err = got_error_from_errno("fdopen");
642 (*obj)->hdrlen = hdrlen;
644 err = got_repo_cache_raw_object(repo, id, *obj);
649 got_object_raw_close(*obj);
658 const struct got_error *
659 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
662 struct got_object_id id;
664 if (!got_parse_sha1_digest(id.sha1, id_str))
665 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
667 return got_object_open(obj, repo, &id);
670 const struct got_error *
671 got_object_resolve_id_str(struct got_object_id **id,
672 struct got_repository *repo, const char *id_str)
674 const struct got_error *err = NULL;
675 struct got_object *obj;
677 err = got_object_open_by_id_str(&obj, repo, id_str);
681 *id = got_object_id_dup(got_object_get_id(obj));
682 got_object_close(obj);
684 return got_error_from_errno("got_object_id_dup");
689 static const struct got_error *
690 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
691 int pack_idx, struct got_object_id *id)
693 const struct got_error *err = NULL;
695 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
700 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
704 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
708 static const struct got_error *
709 read_packed_commit_privsep(struct got_commit_object **commit,
710 struct got_pack *pack, struct got_packidx *packidx, int idx,
711 struct got_object_id *id)
713 const struct got_error *err = NULL;
715 if (pack->privsep_child)
716 return request_packed_commit(commit, pack, idx, id);
718 err = start_pack_privsep_child(pack, packidx);
722 return request_packed_commit(commit, pack, idx, id);
725 static const struct got_error *
726 request_commit(struct got_commit_object **commit, struct got_repository *repo,
727 int fd, struct got_object_id *id)
729 const struct got_error *err = NULL;
730 struct imsgbuf *ibuf;
732 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
734 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
738 return got_privsep_recv_commit(commit, ibuf);
741 static const struct got_error *
742 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
743 struct got_object_id *id, struct got_repository *repo)
745 const struct got_error *err;
748 struct imsgbuf *ibuf;
750 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
751 return request_commit(commit, repo, obj_fd, id);
753 ibuf = calloc(1, sizeof(*ibuf));
755 return got_error_from_errno("calloc");
757 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
758 err = got_error_from_errno("socketpair");
765 err = got_error_from_errno("fork");
770 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
775 if (close(imsg_fds[1]) == -1) {
776 err = got_error_from_errno("close");
780 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
782 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
783 imsg_init(ibuf, imsg_fds[0]);
784 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
786 return request_commit(commit, repo, obj_fd, id);
790 static const struct got_error *
791 open_commit(struct got_commit_object **commit,
792 struct got_repository *repo, struct got_object_id *id, int check_cache)
794 const struct got_error *err = NULL;
795 struct got_packidx *packidx = NULL;
797 char *path_packfile = NULL;
800 *commit = got_repo_get_cached_commit(repo, id);
801 if (*commit != NULL) {
808 err = got_repo_search_packidx(&packidx, &idx, repo, id);
810 struct got_pack *pack = NULL;
812 err = got_packidx_get_packfile_path(&path_packfile,
813 packidx->path_packidx);
817 pack = got_repo_get_cached_pack(repo, path_packfile);
819 err = got_repo_cache_pack(&pack, repo, path_packfile,
824 err = read_packed_commit_privsep(commit, pack,
826 } else if (err->code == GOT_ERR_NO_OBJ) {
829 err = got_object_open_loose_fd(&fd, id, repo);
832 err = read_commit_privsep(commit, fd, id, repo);
837 err = got_repo_cache_commit(repo, id, *commit);
844 const struct got_error *
845 got_object_open_as_commit(struct got_commit_object **commit,
846 struct got_repository *repo, struct got_object_id *id)
848 *commit = got_repo_get_cached_commit(repo, id);
849 if (*commit != NULL) {
854 return open_commit(commit, repo, id, 0);
857 const struct got_error *
858 got_object_commit_open(struct got_commit_object **commit,
859 struct got_repository *repo, struct got_object *obj)
861 return open_commit(commit, repo, got_object_get_id(obj), 1);
864 const struct got_error *
865 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
867 const struct got_error *err = NULL;
869 *qid = calloc(1, sizeof(**qid));
871 return got_error_from_errno("calloc");
873 (*qid)->id = got_object_id_dup(id);
874 if ((*qid)->id == NULL) {
875 err = got_error_from_errno("got_object_id_dup");
876 got_object_qid_free(*qid);
884 const struct got_error *
885 got_object_id_queue_copy(const struct got_object_id_queue *src,
886 struct got_object_id_queue *dest)
888 const struct got_error *err;
889 struct got_object_qid *qid;
891 STAILQ_FOREACH(qid, src, entry) {
892 struct got_object_qid *new;
894 * Deep-copy the object ID only. Let the caller deal
895 * with setting up the new->data pointer if needed.
897 err = got_object_qid_alloc(&new, qid->id);
899 got_object_id_queue_free(dest);
902 STAILQ_INSERT_TAIL(dest, new, entry);
908 static const struct got_error *
909 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
910 int pack_idx, struct got_object_id *id)
912 const struct got_error *err = NULL;
914 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
919 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
922 static const struct got_error *
923 read_packed_tree_privsep(struct got_tree_object **tree,
924 struct got_pack *pack, struct got_packidx *packidx, int idx,
925 struct got_object_id *id)
927 const struct got_error *err = NULL;
929 if (pack->privsep_child)
930 return request_packed_tree(tree, pack, idx, id);
932 err = start_pack_privsep_child(pack, packidx);
936 return request_packed_tree(tree, pack, idx, id);
939 static const struct got_error *
940 request_tree(struct got_tree_object **tree, struct got_repository *repo,
941 int fd, struct got_object_id *id)
943 const struct got_error *err = NULL;
944 struct imsgbuf *ibuf;
946 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
948 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
952 return got_privsep_recv_tree(tree, ibuf);
955 const struct got_error *
956 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
957 struct got_object_id *id, struct got_repository *repo)
959 const struct got_error *err;
962 struct imsgbuf *ibuf;
964 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
965 return request_tree(tree, repo, obj_fd, id);
967 ibuf = calloc(1, sizeof(*ibuf));
969 return got_error_from_errno("calloc");
971 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
972 err = got_error_from_errno("socketpair");
979 err = got_error_from_errno("fork");
984 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
989 if (close(imsg_fds[1]) == -1) {
990 err = got_error_from_errno("close");
994 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
996 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
997 imsg_init(ibuf, imsg_fds[0]);
998 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1001 return request_tree(tree, repo, obj_fd, id);
1004 static const struct got_error *
1005 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1006 struct got_object_id *id, int check_cache)
1008 const struct got_error *err = NULL;
1009 struct got_packidx *packidx = NULL;
1011 char *path_packfile = NULL;
1014 *tree = got_repo_get_cached_tree(repo, id);
1015 if (*tree != NULL) {
1022 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1024 struct got_pack *pack = NULL;
1026 err = got_packidx_get_packfile_path(&path_packfile,
1027 packidx->path_packidx);
1031 pack = got_repo_get_cached_pack(repo, path_packfile);
1033 err = got_repo_cache_pack(&pack, repo, path_packfile,
1038 err = read_packed_tree_privsep(tree, pack,
1040 } else if (err->code == GOT_ERR_NO_OBJ) {
1043 err = got_object_open_loose_fd(&fd, id, repo);
1046 err = read_tree_privsep(tree, fd, id, repo);
1051 err = got_repo_cache_tree(repo, id, *tree);
1054 free(path_packfile);
1058 const struct got_error *
1059 got_object_open_as_tree(struct got_tree_object **tree,
1060 struct got_repository *repo, struct got_object_id *id)
1062 *tree = got_repo_get_cached_tree(repo, id);
1063 if (*tree != NULL) {
1068 return open_tree(tree, repo, id, 0);
1071 const struct got_error *
1072 got_object_tree_open(struct got_tree_object **tree,
1073 struct got_repository *repo, struct got_object *obj)
1075 return open_tree(tree, repo, got_object_get_id(obj), 1);
1079 got_object_tree_get_nentries(struct got_tree_object *tree)
1081 return tree->nentries;
1084 struct got_tree_entry *
1085 got_object_tree_get_first_entry(struct got_tree_object *tree)
1087 return got_object_tree_get_entry(tree, 0);
1090 struct got_tree_entry *
1091 got_object_tree_get_last_entry(struct got_tree_object *tree)
1093 return got_object_tree_get_entry(tree, tree->nentries - 1);
1096 struct got_tree_entry *
1097 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1099 if (i < 0 || i >= tree->nentries)
1101 return &tree->entries[i];
1105 got_tree_entry_get_mode(struct got_tree_entry *te)
1111 got_tree_entry_get_name(struct got_tree_entry *te)
1113 return &te->name[0];
1116 struct got_object_id *
1117 got_tree_entry_get_id(struct got_tree_entry *te)
1122 const struct got_error *
1123 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1125 const struct got_error *err = NULL;
1126 size_t len, totlen, hdrlen, offset;
1130 hdrlen = got_object_blob_get_hdrlen(blob);
1136 err = got_object_blob_read_block(&len, blob);
1143 totlen += len - hdrlen;
1144 p = realloc(*s, totlen + 1);
1146 err = got_error_from_errno("realloc");
1152 /* Skip blob object header first time around. */
1154 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1159 (*s)[totlen] = '\0';
1163 const struct got_error *
1164 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1165 struct got_repository *repo)
1167 const struct got_error *err = NULL;
1168 struct got_blob_object *blob = NULL;
1170 *link_target = NULL;
1172 if (!got_object_tree_entry_is_symlink(te))
1173 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1175 err = got_object_open_as_blob(&blob, repo,
1176 got_tree_entry_get_id(te), PATH_MAX);
1180 err = got_object_blob_read_to_str(link_target, blob);
1181 got_object_blob_close(blob);
1184 *link_target = NULL;
1190 got_tree_entry_get_index(struct got_tree_entry *te)
1195 struct got_tree_entry *
1196 got_tree_entry_get_next(struct got_tree_object *tree,
1197 struct got_tree_entry *te)
1199 return got_object_tree_get_entry(tree, te->idx + 1);
1202 struct got_tree_entry *
1203 got_tree_entry_get_prev(struct got_tree_object *tree,
1204 struct got_tree_entry *te)
1206 return got_object_tree_get_entry(tree, te->idx - 1);
1209 static const struct got_error *
1210 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1211 struct got_pack *pack, struct got_packidx *packidx, int idx,
1212 struct got_object_id *id)
1214 const struct got_error *err = NULL;
1215 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1218 err = pack_child_send_tempfiles(ibuf, pack);
1222 outfd_child = dup(outfd);
1223 if (outfd_child == -1)
1224 return got_error_from_errno("dup");
1226 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1230 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1236 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1237 pack->privsep_child->ibuf);
1241 if (lseek(outfd, SEEK_SET, 0) == -1)
1242 err = got_error_from_errno("lseek");
1247 static const struct got_error *
1248 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1249 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1250 struct got_object_id *id)
1252 const struct got_error *err = NULL;
1254 if (pack->privsep_child == NULL) {
1255 err = start_pack_privsep_child(pack, packidx);
1260 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1264 static const struct got_error *
1265 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1266 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1268 const struct got_error *err = NULL;
1271 outfd_child = dup(outfd);
1272 if (outfd_child == -1)
1273 return got_error_from_errno("dup");
1275 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1279 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1283 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1287 if (lseek(outfd, SEEK_SET, 0) == -1)
1288 return got_error_from_errno("lseek");
1293 static const struct got_error *
1294 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1295 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1297 const struct got_error *err;
1300 struct imsgbuf *ibuf;
1302 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1303 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1304 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1308 ibuf = calloc(1, sizeof(*ibuf));
1310 return got_error_from_errno("calloc");
1312 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1313 err = got_error_from_errno("socketpair");
1320 err = got_error_from_errno("fork");
1324 else if (pid == 0) {
1325 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1330 if (close(imsg_fds[1]) == -1) {
1331 err = got_error_from_errno("close");
1335 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1337 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1338 imsg_init(ibuf, imsg_fds[0]);
1339 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1341 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1344 static const struct got_error *
1345 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1346 struct got_object_id *id, size_t blocksize)
1348 const struct got_error *err = NULL;
1349 struct got_packidx *packidx = NULL;
1351 char *path_packfile = NULL;
1354 size_t size, hdrlen;
1357 *blob = calloc(1, sizeof(**blob));
1359 return got_error_from_errno("calloc");
1361 outfd = got_opentempfd();
1363 return got_error_from_errno("got_opentempfd");
1365 (*blob)->read_buf = malloc(blocksize);
1366 if ((*blob)->read_buf == NULL) {
1367 err = got_error_from_errno("malloc");
1371 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1373 struct got_pack *pack = NULL;
1375 err = got_packidx_get_packfile_path(&path_packfile,
1376 packidx->path_packidx);
1380 pack = got_repo_get_cached_pack(repo, path_packfile);
1382 err = got_repo_cache_pack(&pack, repo, path_packfile,
1387 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1388 pack, packidx, idx, id);
1389 } else if (err->code == GOT_ERR_NO_OBJ) {
1392 err = got_object_open_loose_fd(&infd, id, repo);
1395 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1401 if (hdrlen > size) {
1402 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1407 if (close(outfd) == -1 && err == NULL)
1408 err = got_error_from_errno("close");
1410 (*blob)->f = fmemopen(outbuf, size, "rb");
1411 if ((*blob)->f == NULL) {
1412 err = got_error_from_errno("fmemopen");
1416 (*blob)->data = outbuf;
1418 if (fstat(outfd, &sb) == -1) {
1419 err = got_error_from_errno("fstat");
1423 if (sb.st_size != size) {
1424 err = got_error(GOT_ERR_PRIVSEP_LEN);
1428 (*blob)->f = fdopen(outfd, "rb");
1429 if ((*blob)->f == NULL) {
1430 err = got_error_from_errno("fdopen");
1437 (*blob)->hdrlen = hdrlen;
1438 (*blob)->blocksize = blocksize;
1439 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1442 free(path_packfile);
1445 got_object_blob_close(*blob);
1447 } else if (outfd != -1)
1453 const struct got_error *
1454 got_object_open_as_blob(struct got_blob_object **blob,
1455 struct got_repository *repo, struct got_object_id *id,
1458 return open_blob(blob, repo, id, blocksize);
1461 const struct got_error *
1462 got_object_blob_open(struct got_blob_object **blob,
1463 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1465 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1468 const struct got_error *
1469 got_object_blob_close(struct got_blob_object *blob)
1471 const struct got_error *err = NULL;
1472 free(blob->read_buf);
1473 if (blob->f && fclose(blob->f) == EOF)
1474 err = got_error_from_errno("fclose");
1481 got_object_blob_rewind(struct got_blob_object *blob)
1488 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1490 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1494 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1496 return blob->hdrlen;
1500 got_object_blob_get_read_buf(struct got_blob_object *blob)
1502 return blob->read_buf;
1505 const struct got_error *
1506 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1510 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1511 if (n == 0 && ferror(blob->f))
1512 return got_ferror(blob->f, GOT_ERR_IO);
1517 const struct got_error *
1518 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1519 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1521 const struct got_error *err = NULL;
1522 size_t n, len, hdrlen;
1525 const int alloc_chunksz = 512;
1527 off_t off = 0, total_len = 0;
1530 *line_offsets = NULL;
1536 hdrlen = got_object_blob_get_hdrlen(blob);
1538 err = got_object_blob_read_block(&len, blob);
1543 buf = got_object_blob_get_read_buf(blob);
1546 if (line_offsets && *line_offsets == NULL) {
1547 /* Have some data but perhaps no '\n'. */
1549 nalloc = alloc_chunksz;
1550 *line_offsets = calloc(nalloc,
1551 sizeof(**line_offsets));
1552 if (*line_offsets == NULL)
1553 return got_error_from_errno("calloc");
1555 /* Skip forward over end of first line. */
1562 /* Scan '\n' offsets in remaining chunk of data. */
1564 if (buf[i] != '\n') {
1569 if (line_offsets && nalloc < *nlines) {
1570 size_t n = *nlines + alloc_chunksz;
1571 off_t *o = recallocarray(*line_offsets,
1572 nalloc, n, sizeof(**line_offsets));
1574 free(*line_offsets);
1575 *line_offsets = NULL;
1576 return got_error_from_errno(
1583 off = total_len + i - hdrlen + 1;
1584 (*line_offsets)[*nlines - 1] = off;
1589 /* Skip blob object header first time around. */
1590 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1591 if (n != len - hdrlen)
1592 return got_ferror(outfile, GOT_ERR_IO);
1593 total_len += len - hdrlen;
1597 if (fflush(outfile) != 0)
1598 return got_error_from_errno("fflush");
1602 *filesize = total_len;
1607 static const struct got_error *
1608 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1609 int pack_idx, struct got_object_id *id)
1611 const struct got_error *err = NULL;
1613 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1618 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1621 static const struct got_error *
1622 read_packed_tag_privsep(struct got_tag_object **tag,
1623 struct got_pack *pack, struct got_packidx *packidx, int idx,
1624 struct got_object_id *id)
1626 const struct got_error *err = NULL;
1628 if (pack->privsep_child)
1629 return request_packed_tag(tag, pack, idx, id);
1631 err = start_pack_privsep_child(pack, packidx);
1635 return request_packed_tag(tag, pack, idx, id);
1638 static const struct got_error *
1639 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1640 int fd, struct got_object_id *id)
1642 const struct got_error *err = NULL;
1643 struct imsgbuf *ibuf;
1645 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1647 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1651 return got_privsep_recv_tag(tag, ibuf);
1654 static const struct got_error *
1655 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1656 struct got_object_id *id, struct got_repository *repo)
1658 const struct got_error *err;
1661 struct imsgbuf *ibuf;
1663 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1664 return request_tag(tag, repo, obj_fd, id);
1666 ibuf = calloc(1, sizeof(*ibuf));
1668 return got_error_from_errno("calloc");
1670 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1671 err = got_error_from_errno("socketpair");
1678 err = got_error_from_errno("fork");
1682 else if (pid == 0) {
1683 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1688 if (close(imsg_fds[1]) == -1) {
1689 err = got_error_from_errno("close");
1693 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1695 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1696 imsg_init(ibuf, imsg_fds[0]);
1697 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1699 return request_tag(tag, repo, obj_fd, id);
1702 static const struct got_error *
1703 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1704 struct got_object_id *id, int check_cache)
1706 const struct got_error *err = NULL;
1707 struct got_packidx *packidx = NULL;
1709 char *path_packfile = NULL;
1710 struct got_object *obj = NULL;
1711 int obj_type = GOT_OBJ_TYPE_ANY;
1714 *tag = got_repo_get_cached_tag(repo, id);
1722 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1724 struct got_pack *pack = NULL;
1726 err = got_packidx_get_packfile_path(&path_packfile,
1727 packidx->path_packidx);
1731 pack = got_repo_get_cached_pack(repo, path_packfile);
1733 err = got_repo_cache_pack(&pack, repo, path_packfile,
1739 /* Beware of "lightweight" tags: Check object type first. */
1740 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1744 obj_type = obj->type;
1745 got_object_close(obj);
1746 if (obj_type != GOT_OBJ_TYPE_TAG) {
1747 err = got_error(GOT_ERR_OBJ_TYPE);
1750 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1751 } else if (err->code == GOT_ERR_NO_OBJ) {
1754 err = got_object_open_loose_fd(&fd, id, repo);
1757 err = got_object_read_header_privsep(&obj, id, repo, fd);
1760 obj_type = obj->type;
1761 got_object_close(obj);
1762 if (obj_type != GOT_OBJ_TYPE_TAG)
1763 return got_error(GOT_ERR_OBJ_TYPE);
1765 err = got_object_open_loose_fd(&fd, id, repo);
1768 err = read_tag_privsep(tag, fd, id, repo);
1773 err = got_repo_cache_tag(repo, id, *tag);
1776 free(path_packfile);
1780 const struct got_error *
1781 got_object_open_as_tag(struct got_tag_object **tag,
1782 struct got_repository *repo, struct got_object_id *id)
1784 *tag = got_repo_get_cached_tag(repo, id);
1790 return open_tag(tag, repo, id, 0);
1793 const struct got_error *
1794 got_object_tag_open(struct got_tag_object **tag,
1795 struct got_repository *repo, struct got_object *obj)
1797 return open_tag(tag, repo, got_object_get_id(obj), 1);
1801 got_object_tag_get_name(struct got_tag_object *tag)
1807 got_object_tag_get_object_type(struct got_tag_object *tag)
1809 return tag->obj_type;
1812 struct got_object_id *
1813 got_object_tag_get_object_id(struct got_tag_object *tag)
1819 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1821 return tag->tagger_time;
1825 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1827 return tag->tagger_gmtoff;
1831 got_object_tag_get_tagger(struct got_tag_object *tag)
1837 got_object_tag_get_message(struct got_tag_object *tag)
1842 static struct got_tree_entry *
1843 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1847 /* Note that tree entries are sorted in strncmp() order. */
1848 for (i = 0; i < tree->nentries; i++) {
1849 struct got_tree_entry *te = &tree->entries[i];
1850 int cmp = strncmp(te->name, name, len);
1855 if (te->name[len] == '\0')
1861 struct got_tree_entry *
1862 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1864 return find_entry_by_name(tree, name, strlen(name));
1867 const struct got_error *
1868 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1869 struct got_repository *repo, struct got_tree_object *tree,
1872 const struct got_error *err = NULL;
1873 struct got_tree_object *subtree = NULL;
1874 struct got_tree_entry *te = NULL;
1875 const char *seg, *s;
1887 struct got_tree_object *next_tree;
1896 te = find_entry_by_name(subtree, seg, seglen);
1898 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1909 err = got_object_open_as_tree(&next_tree, repo,
1914 if (subtree != tree)
1915 got_object_tree_close(subtree);
1916 subtree = next_tree;
1921 *id = got_object_id_dup(&te->id);
1923 return got_error_from_errno("got_object_id_dup");
1927 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1929 if (subtree && subtree != tree)
1930 got_object_tree_close(subtree);
1933 const struct got_error *
1934 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1935 struct got_object_id *commit_id, const char *path)
1937 const struct got_error *err = NULL;
1938 struct got_commit_object *commit = NULL;
1939 struct got_tree_object *tree = NULL;
1943 err = got_object_open_as_commit(&commit, repo, commit_id);
1947 /* Handle opening of root of commit's tree. */
1948 if (got_path_is_root_dir(path)) {
1949 *id = got_object_id_dup(commit->tree_id);
1951 err = got_error_from_errno("got_object_id_dup");
1953 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1956 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1960 got_object_commit_close(commit);
1962 got_object_tree_close(tree);
1967 * Normalize file mode bits to avoid false positive tree entry differences
1968 * in case tree entries have unexpected mode bits set.
1971 normalize_mode_for_comparison(mode_t mode)
1974 * For directories, the only relevant bit is the IFDIR bit.
1975 * This allows us to detect paths changing from a directory
1976 * to a file and vice versa.
1979 return mode & S_IFDIR;
1982 * For symlinks, the only relevant bit is the IFLNK bit.
1983 * This allows us to detect paths changing from a symlinks
1984 * to a file or directory and vice versa.
1987 return mode & S_IFLNK;
1989 /* For files, the only change we care about is the executable bit. */
1990 return mode & S_IXUSR;
1993 const struct got_error *
1994 got_object_tree_path_changed(int *changed,
1995 struct got_tree_object *tree01, struct got_tree_object *tree02,
1996 const char *path, struct got_repository *repo)
1998 const struct got_error *err = NULL;
1999 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2000 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2001 const char *seg, *s;
2006 /* We not do support comparing the root path. */
2007 if (got_path_is_root_dir(path))
2008 return got_error_path(path, GOT_ERR_BAD_PATH);
2018 struct got_tree_object *next_tree1, *next_tree2;
2019 mode_t mode1, mode2;
2028 te1 = find_entry_by_name(tree1, seg, seglen);
2030 err = got_error(GOT_ERR_NO_OBJ);
2035 te2 = find_entry_by_name(tree2, seg, seglen);
2038 mode1 = normalize_mode_for_comparison(te1->mode);
2039 mode2 = normalize_mode_for_comparison(te2->mode);
2040 if (mode1 != mode2) {
2045 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2051 if (*s == '\0') { /* final path element */
2060 err = got_object_open_as_tree(&next_tree1, repo,
2065 if (tree1 != tree01)
2066 got_object_tree_close(tree1);
2070 err = got_object_open_as_tree(&next_tree2, repo,
2075 if (tree2 != tree02)
2076 got_object_tree_close(tree2);
2079 if (tree2 != tree02)
2080 got_object_tree_close(tree2);
2086 if (tree1 && tree1 != tree01)
2087 got_object_tree_close(tree1);
2088 if (tree2 && tree2 != tree02)
2089 got_object_tree_close(tree2);
2093 const struct got_error *
2094 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2095 struct got_tree_entry *te)
2097 const struct got_error *err = NULL;
2099 *new_te = calloc(1, sizeof(**new_te));
2100 if (*new_te == NULL)
2101 return got_error_from_errno("calloc");
2103 (*new_te)->mode = te->mode;
2104 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2105 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2110 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2112 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2116 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2118 /* S_IFDIR check avoids confusing symlinks with submodules. */
2119 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2122 static const struct got_error *
2123 resolve_symlink(char **link_target, const char *path,
2124 struct got_object_id *commit_id, struct got_repository *repo)
2126 const struct got_error *err = NULL;
2128 char *name, *parent_path = NULL;
2129 struct got_object_id *tree_obj_id = NULL;
2130 struct got_tree_object *tree = NULL;
2131 struct got_tree_entry *te = NULL;
2133 *link_target = NULL;
2135 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2136 return got_error(GOT_ERR_NO_SPACE);
2138 name = basename(buf);
2140 return got_error_from_errno2("basename", path);
2142 err = got_path_dirname(&parent_path, path);
2146 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2149 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2150 /* Display the complete path in error message. */
2151 err = got_error_path(path, err->code);
2156 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2160 te = got_object_tree_find_entry(tree, name);
2162 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2166 if (got_object_tree_entry_is_symlink(te)) {
2167 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2170 if (!got_path_is_absolute(*link_target)) {
2172 if (asprintf(&abspath, "%s/%s", parent_path,
2173 *link_target) == -1) {
2174 err = got_error_from_errno("asprintf");
2178 *link_target = malloc(PATH_MAX);
2179 if (*link_target == NULL) {
2180 err = got_error_from_errno("malloc");
2183 err = got_canonpath(abspath, *link_target, PATH_MAX);
2192 got_object_tree_close(tree);
2195 *link_target = NULL;
2200 const struct got_error *
2201 got_object_resolve_symlinks(char **link_target, const char *path,
2202 struct got_object_id *commit_id, struct got_repository *repo)
2204 const struct got_error *err = NULL;
2205 char *next_target = NULL;
2206 int max_recursion = 40; /* matches Git */
2208 *link_target = NULL;
2211 err = resolve_symlink(&next_target,
2212 *link_target ? *link_target : path, commit_id, repo);
2217 if (--max_recursion == 0) {
2218 err = got_error_path(path, GOT_ERR_RECURSION);
2219 *link_target = NULL;
2222 *link_target = next_target;
2224 } while (next_target);
2229 const struct got_error *
2230 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2231 struct got_object_id *commit_id, const char *path,
2232 struct got_repository *repo)
2234 const struct got_error *err = NULL;
2235 struct got_pack *pack = NULL;
2236 struct got_packidx *packidx = NULL;
2237 char *path_packfile = NULL;
2238 struct got_commit_object *changed_commit = NULL;
2239 struct got_object_id *changed_commit_id = NULL;
2242 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2244 if (err->code != GOT_ERR_NO_OBJ)
2249 err = got_packidx_get_packfile_path(&path_packfile,
2250 packidx->path_packidx);
2254 pack = got_repo_get_cached_pack(repo, path_packfile);
2256 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2261 if (pack->privsep_child == NULL) {
2262 err = start_pack_privsep_child(pack, packidx);
2267 err = got_privsep_send_commit_traversal_request(
2268 pack->privsep_child->ibuf, commit_id, idx, path);
2272 err = got_privsep_recv_traversed_commits(&changed_commit,
2273 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2277 if (changed_commit) {
2279 * Cache the commit in which the path was changed.
2280 * This commit might be opened again soon.
2282 changed_commit->refcnt++;
2283 err = got_repo_cache_commit(repo, changed_commit_id,
2285 got_object_commit_close(changed_commit);
2288 free(path_packfile);
2289 free(changed_commit_id);