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>
39 #include "got_compat.h"
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
71 return got_object_id_str(outbuf, &obj->id);
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
93 err = got_error(GOT_ERR_OBJ_TYPE);
97 got_object_close(obj);
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
129 const struct got_error *
130 got_object_open_loose_fd(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
136 err = got_object_get_path(&path, id, repo);
139 *fd = open(path, O_RDONLY | O_NOFOLLOW);
141 err = got_error_from_errno2("open", path);
149 static const struct got_error *
150 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
151 struct got_object_id *id)
153 const struct got_error *err = NULL;
154 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
156 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
160 err = got_privsep_recv_obj(obj, ibuf);
164 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
169 static const struct got_error *
170 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
171 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
173 const struct got_error *err = NULL;
174 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
176 int basefd, accumfd; /* temporary files for delta application */
178 basefd = got_opentempfd();
180 return got_error_from_errno("got_opentempfd");
182 accumfd = got_opentempfd();
185 return got_error_from_errno("got_opentempfd");
188 outfd_child = dup(outfd);
189 if (outfd_child == -1) {
190 err = got_error_from_errno("dup");
196 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
204 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
212 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
219 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
224 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
232 set_max_datasize(void)
236 if (getrlimit(RLIMIT_DATA, &rl) != 0)
239 rl.rlim_cur = rl.rlim_max;
240 setrlimit(RLIMIT_DATA, &rl);
243 static const struct got_error *
244 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
246 const struct got_error *err = NULL;
249 struct imsgbuf *ibuf;
251 ibuf = calloc(1, sizeof(*ibuf));
253 return got_error_from_errno("calloc");
255 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
256 if (pack->privsep_child == NULL) {
257 err = got_error_from_errno("calloc");
262 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
263 err = got_error_from_errno("socketpair");
269 err = got_error_from_errno("fork");
271 } else if (pid == 0) {
273 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
274 pack->path_packfile);
278 if (close(imsg_fds[1]) == -1)
279 return got_error_from_errno("close");
280 pack->privsep_child->imsg_fd = imsg_fds[0];
281 pack->privsep_child->pid = pid;
282 imsg_init(ibuf, imsg_fds[0]);
283 pack->privsep_child->ibuf = ibuf;
285 err = got_privsep_init_pack_child(ibuf, pack, packidx);
287 const struct got_error *child_err;
288 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
289 child_err = got_privsep_wait_for_child(
290 pack->privsep_child->pid);
291 if (child_err && err == NULL)
297 free(pack->privsep_child);
298 pack->privsep_child = NULL;
303 static const struct got_error *
304 read_packed_object_privsep(struct got_object **obj,
305 struct got_repository *repo, struct got_pack *pack,
306 struct got_packidx *packidx, int idx, struct got_object_id *id)
308 const struct got_error *err = NULL;
310 if (pack->privsep_child == NULL) {
311 err = start_pack_privsep_child(pack, packidx);
316 return request_packed_object(obj, pack, idx, id);
319 static const struct got_error *
320 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
321 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
322 struct got_object_id *id)
324 const struct got_error *err = NULL;
326 if (pack->privsep_child == NULL) {
327 err = start_pack_privsep_child(pack, packidx);
332 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
336 const struct got_error *
337 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
338 struct got_repository *repo)
340 const struct got_error *err = NULL;
341 struct got_pack *pack = NULL;
342 struct got_packidx *packidx = NULL;
346 err = got_repo_search_packidx(&packidx, &idx, repo, id);
350 err = got_packidx_get_packfile_path(&path_packfile,
351 packidx->path_packidx);
355 pack = got_repo_get_cached_pack(repo, path_packfile);
357 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
362 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
370 static const struct got_error *
371 request_object(struct got_object **obj, struct got_object_id *id,
372 struct got_repository *repo, int fd)
374 const struct got_error *err = NULL;
375 struct imsgbuf *ibuf;
377 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
379 err = got_privsep_send_obj_req(ibuf, fd, id);
383 return got_privsep_recv_obj(obj, ibuf);
386 static const struct got_error *
387 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
388 struct got_object_id *id, struct got_repository *repo, int infd)
390 const struct got_error *err = NULL;
391 struct imsgbuf *ibuf;
394 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
396 outfd_child = dup(outfd);
397 if (outfd_child == -1)
398 return got_error_from_errno("dup");
400 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
404 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
408 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
411 static const struct got_error *
412 start_read_object_child(struct got_repository *repo)
414 const struct got_error *err = NULL;
417 struct imsgbuf *ibuf;
419 ibuf = calloc(1, sizeof(*ibuf));
421 return got_error_from_errno("calloc");
423 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
424 err = got_error_from_errno("socketpair");
431 err = got_error_from_errno("fork");
436 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
441 if (close(imsg_fds[1]) == -1) {
442 err = got_error_from_errno("close");
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
450 imsg_init(ibuf, imsg_fds[0]);
451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
456 const struct got_error *
457 got_object_read_header_privsep(struct got_object **obj,
458 struct got_object_id *id, struct got_repository *repo, int obj_fd)
460 const struct got_error *err;
462 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
463 return request_object(obj, id, repo, obj_fd);
465 err = start_read_object_child(repo);
471 return request_object(obj, id, repo, obj_fd);
474 static const struct got_error *
475 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
476 int outfd, struct got_object_id *id, struct got_repository *repo,
479 const struct got_error *err;
481 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
482 return request_raw_object(outbuf, size, hdrlen, outfd, id,
485 err = start_read_object_child(repo);
489 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
493 const struct got_error *
494 got_object_open(struct got_object **obj, struct got_repository *repo,
495 struct got_object_id *id)
497 const struct got_error *err = NULL;
500 *obj = got_repo_get_cached_object(repo, id);
506 err = got_object_open_packed(obj, id, repo);
507 if (err && err->code != GOT_ERR_NO_OBJ)
511 return got_repo_cache_object(repo, id, *obj);
514 err = got_object_open_loose_fd(&fd, id, repo);
516 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
517 err = got_error_no_obj(id);
521 err = got_object_read_header_privsep(obj, id, repo, fd);
525 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
528 return got_repo_cache_object(repo, id, *obj);
531 const struct got_error *
532 got_object_raw_open(struct got_raw_object **obj, int outfd,
533 struct got_repository *repo, struct got_object_id *id, size_t blocksize)
535 const struct got_error *err = NULL;
536 struct got_packidx *packidx = NULL;
538 uint8_t *outbuf = NULL;
541 char *path_packfile = NULL;
545 err = got_repo_search_packidx(&packidx, &idx, repo, id);
547 struct got_pack *pack = NULL;
549 err = got_packidx_get_packfile_path(&path_packfile,
550 packidx->path_packidx);
554 pack = got_repo_get_cached_pack(repo, path_packfile);
556 err = got_repo_cache_pack(&pack, repo, path_packfile,
561 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
562 outfd, pack, packidx, idx, id);
565 } else if (err->code == GOT_ERR_NO_OBJ) {
568 err = got_object_open_loose_fd(&fd, id, repo);
571 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
577 *obj = calloc(1, sizeof(**obj));
579 err = got_error_from_errno("calloc");
583 (*obj)->read_buf = malloc(blocksize);
584 if ((*obj)->read_buf == NULL) {
585 err = got_error_from_errno("malloc");
590 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
591 if ((*obj)->f == NULL) {
592 err = got_error_from_errno("fdopen");
595 (*obj)->data = outbuf;
598 if (fstat(outfd, &sb) == -1) {
599 err = got_error_from_errno("fstat");
603 if (sb.st_size != hdrlen + size) {
604 err = got_error(GOT_ERR_PRIVSEP_LEN);
608 (*obj)->f = fdopen(outfd, "r");
609 if ((*obj)->f == NULL) {
610 err = got_error_from_errno("fdopen");
615 (*obj)->hdrlen = hdrlen;
617 (*obj)->blocksize = blocksize;
622 got_object_raw_close(*obj);
631 got_object_raw_rewind(struct got_raw_object *obj)
638 got_object_raw_get_hdrlen(struct got_raw_object *obj)
644 got_object_raw_get_read_buf(struct got_raw_object *obj)
646 return obj->read_buf;
649 const struct got_error *
650 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
654 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
655 if (n == 0 && ferror(obj->f))
656 return got_ferror(obj->f, GOT_ERR_IO);
661 const struct got_error *
662 got_object_raw_close(struct got_raw_object *obj)
664 const struct got_error *err = NULL;
667 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
668 err = got_error_from_errno("fclose");
674 const struct got_error *
675 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
678 struct got_object_id id;
680 if (!got_parse_sha1_digest(id.sha1, id_str))
681 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
683 return got_object_open(obj, repo, &id);
686 const struct got_error *
687 got_object_resolve_id_str(struct got_object_id **id,
688 struct got_repository *repo, const char *id_str)
690 const struct got_error *err = NULL;
691 struct got_object *obj;
693 err = got_object_open_by_id_str(&obj, repo, id_str);
697 *id = got_object_id_dup(got_object_get_id(obj));
698 got_object_close(obj);
700 return got_error_from_errno("got_object_id_dup");
705 static const struct got_error *
706 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
707 int pack_idx, struct got_object_id *id)
709 const struct got_error *err = NULL;
711 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
716 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
720 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
724 static const struct got_error *
725 read_packed_commit_privsep(struct got_commit_object **commit,
726 struct got_pack *pack, struct got_packidx *packidx, int idx,
727 struct got_object_id *id)
729 const struct got_error *err = NULL;
731 if (pack->privsep_child)
732 return request_packed_commit(commit, pack, idx, id);
734 err = start_pack_privsep_child(pack, packidx);
738 return request_packed_commit(commit, pack, idx, id);
741 static const struct got_error *
742 request_commit(struct got_commit_object **commit, struct got_repository *repo,
743 int fd, struct got_object_id *id)
745 const struct got_error *err = NULL;
746 struct imsgbuf *ibuf;
748 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
750 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
754 return got_privsep_recv_commit(commit, ibuf);
757 static const struct got_error *
758 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
759 struct got_object_id *id, struct got_repository *repo)
761 const struct got_error *err;
764 struct imsgbuf *ibuf;
766 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
767 return request_commit(commit, repo, obj_fd, id);
769 ibuf = calloc(1, sizeof(*ibuf));
771 return got_error_from_errno("calloc");
773 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
774 err = got_error_from_errno("socketpair");
781 err = got_error_from_errno("fork");
786 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
791 if (close(imsg_fds[1]) == -1) {
792 err = got_error_from_errno("close");
796 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
798 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
799 imsg_init(ibuf, imsg_fds[0]);
800 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
802 return request_commit(commit, repo, obj_fd, id);
806 static const struct got_error *
807 open_commit(struct got_commit_object **commit,
808 struct got_repository *repo, struct got_object_id *id, int check_cache)
810 const struct got_error *err = NULL;
811 struct got_packidx *packidx = NULL;
813 char *path_packfile = NULL;
816 *commit = got_repo_get_cached_commit(repo, id);
817 if (*commit != NULL) {
824 err = got_repo_search_packidx(&packidx, &idx, repo, id);
826 struct got_pack *pack = NULL;
828 err = got_packidx_get_packfile_path(&path_packfile,
829 packidx->path_packidx);
833 pack = got_repo_get_cached_pack(repo, path_packfile);
835 err = got_repo_cache_pack(&pack, repo, path_packfile,
840 err = read_packed_commit_privsep(commit, pack,
842 } else if (err->code == GOT_ERR_NO_OBJ) {
845 err = got_object_open_loose_fd(&fd, id, repo);
848 err = read_commit_privsep(commit, fd, id, repo);
853 err = got_repo_cache_commit(repo, id, *commit);
860 const struct got_error *
861 got_object_open_as_commit(struct got_commit_object **commit,
862 struct got_repository *repo, struct got_object_id *id)
864 *commit = got_repo_get_cached_commit(repo, id);
865 if (*commit != NULL) {
870 return open_commit(commit, repo, id, 0);
873 const struct got_error *
874 got_object_commit_open(struct got_commit_object **commit,
875 struct got_repository *repo, struct got_object *obj)
877 return open_commit(commit, repo, got_object_get_id(obj), 1);
880 const struct got_error *
881 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
883 const struct got_error *err = NULL;
885 *qid = calloc(1, sizeof(**qid));
887 return got_error_from_errno("calloc");
889 (*qid)->id = got_object_id_dup(id);
890 if ((*qid)->id == NULL) {
891 err = got_error_from_errno("got_object_id_dup");
892 got_object_qid_free(*qid);
900 const struct got_error *
901 got_object_id_queue_copy(const struct got_object_id_queue *src,
902 struct got_object_id_queue *dest)
904 const struct got_error *err;
905 struct got_object_qid *qid;
907 STAILQ_FOREACH(qid, src, entry) {
908 struct got_object_qid *new;
910 * Deep-copy the object ID only. Let the caller deal
911 * with setting up the new->data pointer if needed.
913 err = got_object_qid_alloc(&new, qid->id);
915 got_object_id_queue_free(dest);
918 STAILQ_INSERT_TAIL(dest, new, entry);
924 static const struct got_error *
925 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
926 int pack_idx, struct got_object_id *id)
928 const struct got_error *err = NULL;
930 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
935 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
938 static const struct got_error *
939 read_packed_tree_privsep(struct got_tree_object **tree,
940 struct got_pack *pack, struct got_packidx *packidx, int idx,
941 struct got_object_id *id)
943 const struct got_error *err = NULL;
945 if (pack->privsep_child)
946 return request_packed_tree(tree, pack, idx, id);
948 err = start_pack_privsep_child(pack, packidx);
952 return request_packed_tree(tree, pack, idx, id);
955 static const struct got_error *
956 request_tree(struct got_tree_object **tree, struct got_repository *repo,
957 int fd, struct got_object_id *id)
959 const struct got_error *err = NULL;
960 struct imsgbuf *ibuf;
962 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
964 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
968 return got_privsep_recv_tree(tree, ibuf);
971 const struct got_error *
972 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
973 struct got_object_id *id, struct got_repository *repo)
975 const struct got_error *err;
978 struct imsgbuf *ibuf;
980 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
981 return request_tree(tree, repo, obj_fd, id);
983 ibuf = calloc(1, sizeof(*ibuf));
985 return got_error_from_errno("calloc");
987 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
988 err = got_error_from_errno("socketpair");
995 err = got_error_from_errno("fork");
1000 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1005 if (close(imsg_fds[1]) == -1) {
1006 err = got_error_from_errno("close");
1010 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1012 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1013 imsg_init(ibuf, imsg_fds[0]);
1014 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1017 return request_tree(tree, repo, obj_fd, id);
1020 static const struct got_error *
1021 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1022 struct got_object_id *id, int check_cache)
1024 const struct got_error *err = NULL;
1025 struct got_packidx *packidx = NULL;
1027 char *path_packfile = NULL;
1030 *tree = got_repo_get_cached_tree(repo, id);
1031 if (*tree != NULL) {
1038 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1040 struct got_pack *pack = NULL;
1042 err = got_packidx_get_packfile_path(&path_packfile,
1043 packidx->path_packidx);
1047 pack = got_repo_get_cached_pack(repo, path_packfile);
1049 err = got_repo_cache_pack(&pack, repo, path_packfile,
1054 err = read_packed_tree_privsep(tree, pack,
1056 } else if (err->code == GOT_ERR_NO_OBJ) {
1059 err = got_object_open_loose_fd(&fd, id, repo);
1062 err = read_tree_privsep(tree, fd, id, repo);
1067 err = got_repo_cache_tree(repo, id, *tree);
1070 free(path_packfile);
1074 const struct got_error *
1075 got_object_open_as_tree(struct got_tree_object **tree,
1076 struct got_repository *repo, struct got_object_id *id)
1078 *tree = got_repo_get_cached_tree(repo, id);
1079 if (*tree != NULL) {
1084 return open_tree(tree, repo, id, 0);
1087 const struct got_error *
1088 got_object_tree_open(struct got_tree_object **tree,
1089 struct got_repository *repo, struct got_object *obj)
1091 return open_tree(tree, repo, got_object_get_id(obj), 1);
1095 got_object_tree_get_nentries(struct got_tree_object *tree)
1097 return tree->nentries;
1100 struct got_tree_entry *
1101 got_object_tree_get_first_entry(struct got_tree_object *tree)
1103 return got_object_tree_get_entry(tree, 0);
1106 struct got_tree_entry *
1107 got_object_tree_get_last_entry(struct got_tree_object *tree)
1109 return got_object_tree_get_entry(tree, tree->nentries - 1);
1112 struct got_tree_entry *
1113 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1115 if (i < 0 || i >= tree->nentries)
1117 return &tree->entries[i];
1121 got_tree_entry_get_mode(struct got_tree_entry *te)
1127 got_tree_entry_get_name(struct got_tree_entry *te)
1129 return &te->name[0];
1132 struct got_object_id *
1133 got_tree_entry_get_id(struct got_tree_entry *te)
1138 const struct got_error *
1139 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1141 const struct got_error *err = NULL;
1142 size_t len, totlen, hdrlen, offset;
1146 hdrlen = got_object_blob_get_hdrlen(blob);
1152 err = got_object_blob_read_block(&len, blob);
1159 totlen += len - hdrlen;
1160 p = realloc(*s, totlen + 1);
1162 err = got_error_from_errno("realloc");
1168 /* Skip blob object header first time around. */
1170 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1175 (*s)[totlen] = '\0';
1179 const struct got_error *
1180 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1181 struct got_repository *repo)
1183 const struct got_error *err = NULL;
1184 struct got_blob_object *blob = NULL;
1186 *link_target = NULL;
1188 if (!got_object_tree_entry_is_symlink(te))
1189 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1191 err = got_object_open_as_blob(&blob, repo,
1192 got_tree_entry_get_id(te), PATH_MAX);
1196 err = got_object_blob_read_to_str(link_target, blob);
1197 got_object_blob_close(blob);
1200 *link_target = NULL;
1206 got_tree_entry_get_index(struct got_tree_entry *te)
1211 struct got_tree_entry *
1212 got_tree_entry_get_next(struct got_tree_object *tree,
1213 struct got_tree_entry *te)
1215 return got_object_tree_get_entry(tree, te->idx + 1);
1218 struct got_tree_entry *
1219 got_tree_entry_get_prev(struct got_tree_object *tree,
1220 struct got_tree_entry *te)
1222 return got_object_tree_get_entry(tree, te->idx - 1);
1225 static const struct got_error *
1226 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1227 struct got_pack *pack, struct got_packidx *packidx, int idx,
1228 struct got_object_id *id)
1230 const struct got_error *err = NULL;
1232 int basefd, accumfd; /* temporary files for delta application */
1234 basefd = got_opentempfd();
1236 return got_error_from_errno("got_opentempfd");
1237 accumfd = got_opentempfd();
1239 return got_error_from_errno("got_opentempfd");
1241 outfd_child = dup(outfd);
1242 if (outfd_child == -1)
1243 return got_error_from_errno("dup");
1245 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1249 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1257 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1264 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1269 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1270 pack->privsep_child->ibuf);
1274 if (lseek(outfd, SEEK_SET, 0) == -1)
1275 err = got_error_from_errno("lseek");
1280 static const struct got_error *
1281 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1282 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1283 struct got_object_id *id)
1285 const struct got_error *err = NULL;
1287 if (pack->privsep_child == NULL) {
1288 err = start_pack_privsep_child(pack, packidx);
1293 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1297 static const struct got_error *
1298 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1299 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1301 const struct got_error *err = NULL;
1304 outfd_child = dup(outfd);
1305 if (outfd_child == -1)
1306 return got_error_from_errno("dup");
1308 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1312 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1316 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1320 if (lseek(outfd, SEEK_SET, 0) == -1)
1321 return got_error_from_errno("lseek");
1326 static const struct got_error *
1327 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1328 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1330 const struct got_error *err;
1333 struct imsgbuf *ibuf;
1335 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1336 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1337 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1341 ibuf = calloc(1, sizeof(*ibuf));
1343 return got_error_from_errno("calloc");
1345 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1346 err = got_error_from_errno("socketpair");
1353 err = got_error_from_errno("fork");
1357 else if (pid == 0) {
1358 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1363 if (close(imsg_fds[1]) == -1) {
1364 err = got_error_from_errno("close");
1368 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1370 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1371 imsg_init(ibuf, imsg_fds[0]);
1372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1374 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1377 static const struct got_error *
1378 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1379 struct got_object_id *id, size_t blocksize)
1381 const struct got_error *err = NULL;
1382 struct got_packidx *packidx = NULL;
1384 char *path_packfile = NULL;
1387 size_t size, hdrlen;
1390 *blob = calloc(1, sizeof(**blob));
1392 return got_error_from_errno("calloc");
1394 outfd = got_opentempfd();
1396 return got_error_from_errno("got_opentempfd");
1398 (*blob)->read_buf = malloc(blocksize);
1399 if ((*blob)->read_buf == NULL) {
1400 err = got_error_from_errno("malloc");
1404 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1406 struct got_pack *pack = NULL;
1408 err = got_packidx_get_packfile_path(&path_packfile,
1409 packidx->path_packidx);
1413 pack = got_repo_get_cached_pack(repo, path_packfile);
1415 err = got_repo_cache_pack(&pack, repo, path_packfile,
1420 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1421 pack, packidx, idx, id);
1422 } else if (err->code == GOT_ERR_NO_OBJ) {
1425 err = got_object_open_loose_fd(&infd, id, repo);
1428 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1434 if (hdrlen > size) {
1435 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1440 if (close(outfd) == -1 && err == NULL)
1441 err = got_error_from_errno("close");
1443 (*blob)->f = fmemopen(outbuf, size, "rb");
1444 if ((*blob)->f == NULL) {
1445 err = got_error_from_errno("fmemopen");
1449 (*blob)->data = outbuf;
1451 if (fstat(outfd, &sb) == -1) {
1452 err = got_error_from_errno("fstat");
1456 if (sb.st_size != size) {
1457 err = got_error(GOT_ERR_PRIVSEP_LEN);
1461 (*blob)->f = fdopen(outfd, "rb");
1462 if ((*blob)->f == NULL) {
1463 err = got_error_from_errno("fdopen");
1470 (*blob)->hdrlen = hdrlen;
1471 (*blob)->blocksize = blocksize;
1472 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1475 free(path_packfile);
1478 got_object_blob_close(*blob);
1480 } else if (outfd != -1)
1486 const struct got_error *
1487 got_object_open_as_blob(struct got_blob_object **blob,
1488 struct got_repository *repo, struct got_object_id *id,
1491 return open_blob(blob, repo, id, blocksize);
1494 const struct got_error *
1495 got_object_blob_open(struct got_blob_object **blob,
1496 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1498 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1501 const struct got_error *
1502 got_object_blob_close(struct got_blob_object *blob)
1504 const struct got_error *err = NULL;
1505 free(blob->read_buf);
1506 if (blob->f && fclose(blob->f) == EOF)
1507 err = got_error_from_errno("fclose");
1514 got_object_blob_rewind(struct got_blob_object *blob)
1521 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1523 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1527 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1529 return blob->hdrlen;
1533 got_object_blob_get_read_buf(struct got_blob_object *blob)
1535 return blob->read_buf;
1538 const struct got_error *
1539 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1543 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1544 if (n == 0 && ferror(blob->f))
1545 return got_ferror(blob->f, GOT_ERR_IO);
1550 const struct got_error *
1551 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1552 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1554 const struct got_error *err = NULL;
1555 size_t n, len, hdrlen;
1558 const int alloc_chunksz = 512;
1560 off_t off = 0, total_len = 0;
1563 *line_offsets = NULL;
1569 hdrlen = got_object_blob_get_hdrlen(blob);
1571 err = got_object_blob_read_block(&len, blob);
1576 buf = got_object_blob_get_read_buf(blob);
1579 if (line_offsets && *line_offsets == NULL) {
1580 /* Have some data but perhaps no '\n'. */
1582 nalloc = alloc_chunksz;
1583 *line_offsets = calloc(nalloc,
1584 sizeof(**line_offsets));
1585 if (*line_offsets == NULL)
1586 return got_error_from_errno("calloc");
1588 /* Skip forward over end of first line. */
1595 /* Scan '\n' offsets in remaining chunk of data. */
1597 if (buf[i] != '\n') {
1602 if (line_offsets && nalloc < *nlines) {
1603 size_t n = *nlines + alloc_chunksz;
1604 off_t *o = recallocarray(*line_offsets,
1605 nalloc, n, sizeof(**line_offsets));
1607 free(*line_offsets);
1608 *line_offsets = NULL;
1609 return got_error_from_errno(
1616 off = total_len + i - hdrlen + 1;
1617 (*line_offsets)[*nlines - 1] = off;
1622 /* Skip blob object header first time around. */
1623 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1624 if (n != len - hdrlen)
1625 return got_ferror(outfile, GOT_ERR_IO);
1626 total_len += len - hdrlen;
1630 if (fflush(outfile) != 0)
1631 return got_error_from_errno("fflush");
1635 *filesize = total_len;
1640 static const struct got_error *
1641 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1642 int pack_idx, struct got_object_id *id)
1644 const struct got_error *err = NULL;
1646 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1651 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1654 static const struct got_error *
1655 read_packed_tag_privsep(struct got_tag_object **tag,
1656 struct got_pack *pack, struct got_packidx *packidx, int idx,
1657 struct got_object_id *id)
1659 const struct got_error *err = NULL;
1661 if (pack->privsep_child)
1662 return request_packed_tag(tag, pack, idx, id);
1664 err = start_pack_privsep_child(pack, packidx);
1668 return request_packed_tag(tag, pack, idx, id);
1671 static const struct got_error *
1672 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1673 int fd, struct got_object_id *id)
1675 const struct got_error *err = NULL;
1676 struct imsgbuf *ibuf;
1678 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1680 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1684 return got_privsep_recv_tag(tag, ibuf);
1687 static const struct got_error *
1688 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1689 struct got_object_id *id, struct got_repository *repo)
1691 const struct got_error *err;
1694 struct imsgbuf *ibuf;
1696 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1697 return request_tag(tag, repo, obj_fd, id);
1699 ibuf = calloc(1, sizeof(*ibuf));
1701 return got_error_from_errno("calloc");
1703 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1704 err = got_error_from_errno("socketpair");
1711 err = got_error_from_errno("fork");
1715 else if (pid == 0) {
1716 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1721 if (close(imsg_fds[1]) == -1) {
1722 err = got_error_from_errno("close");
1726 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1728 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1729 imsg_init(ibuf, imsg_fds[0]);
1730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1732 return request_tag(tag, repo, obj_fd, id);
1735 static const struct got_error *
1736 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1737 struct got_object_id *id, int check_cache)
1739 const struct got_error *err = NULL;
1740 struct got_packidx *packidx = NULL;
1742 char *path_packfile = NULL;
1743 struct got_object *obj = NULL;
1744 int obj_type = GOT_OBJ_TYPE_ANY;
1747 *tag = got_repo_get_cached_tag(repo, id);
1755 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1757 struct got_pack *pack = NULL;
1759 err = got_packidx_get_packfile_path(&path_packfile,
1760 packidx->path_packidx);
1764 pack = got_repo_get_cached_pack(repo, path_packfile);
1766 err = got_repo_cache_pack(&pack, repo, path_packfile,
1772 /* Beware of "lightweight" tags: Check object type first. */
1773 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1777 obj_type = obj->type;
1778 got_object_close(obj);
1779 if (obj_type != GOT_OBJ_TYPE_TAG) {
1780 err = got_error(GOT_ERR_OBJ_TYPE);
1783 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1784 } else if (err->code == GOT_ERR_NO_OBJ) {
1787 err = got_object_open_loose_fd(&fd, id, repo);
1790 err = got_object_read_header_privsep(&obj, id, repo, fd);
1793 obj_type = obj->type;
1794 got_object_close(obj);
1795 if (obj_type != GOT_OBJ_TYPE_TAG)
1796 return got_error(GOT_ERR_OBJ_TYPE);
1798 err = got_object_open_loose_fd(&fd, id, repo);
1801 err = read_tag_privsep(tag, fd, id, repo);
1806 err = got_repo_cache_tag(repo, id, *tag);
1809 free(path_packfile);
1813 const struct got_error *
1814 got_object_open_as_tag(struct got_tag_object **tag,
1815 struct got_repository *repo, struct got_object_id *id)
1817 *tag = got_repo_get_cached_tag(repo, id);
1823 return open_tag(tag, repo, id, 0);
1826 const struct got_error *
1827 got_object_tag_open(struct got_tag_object **tag,
1828 struct got_repository *repo, struct got_object *obj)
1830 return open_tag(tag, repo, got_object_get_id(obj), 1);
1834 got_object_tag_get_name(struct got_tag_object *tag)
1840 got_object_tag_get_object_type(struct got_tag_object *tag)
1842 return tag->obj_type;
1845 struct got_object_id *
1846 got_object_tag_get_object_id(struct got_tag_object *tag)
1852 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1854 return tag->tagger_time;
1858 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1860 return tag->tagger_gmtoff;
1864 got_object_tag_get_tagger(struct got_tag_object *tag)
1870 got_object_tag_get_message(struct got_tag_object *tag)
1875 static struct got_tree_entry *
1876 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1880 /* Note that tree entries are sorted in strncmp() order. */
1881 for (i = 0; i < tree->nentries; i++) {
1882 struct got_tree_entry *te = &tree->entries[i];
1883 int cmp = strncmp(te->name, name, len);
1888 if (te->name[len] == '\0')
1894 struct got_tree_entry *
1895 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1897 return find_entry_by_name(tree, name, strlen(name));
1900 const struct got_error *
1901 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1902 struct got_repository *repo, struct got_tree_object *tree,
1905 const struct got_error *err = NULL;
1906 struct got_tree_object *subtree = NULL;
1907 struct got_tree_entry *te = NULL;
1908 const char *seg, *s;
1920 struct got_tree_object *next_tree;
1929 te = find_entry_by_name(subtree, seg, seglen);
1931 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1942 err = got_object_open_as_tree(&next_tree, repo,
1947 if (subtree != tree)
1948 got_object_tree_close(subtree);
1949 subtree = next_tree;
1954 *id = got_object_id_dup(&te->id);
1956 return got_error_from_errno("got_object_id_dup");
1960 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1962 if (subtree && subtree != tree)
1963 got_object_tree_close(subtree);
1966 const struct got_error *
1967 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1968 struct got_object_id *commit_id, const char *path)
1970 const struct got_error *err = NULL;
1971 struct got_commit_object *commit = NULL;
1972 struct got_tree_object *tree = NULL;
1976 err = got_object_open_as_commit(&commit, repo, commit_id);
1980 /* Handle opening of root of commit's tree. */
1981 if (got_path_is_root_dir(path)) {
1982 *id = got_object_id_dup(commit->tree_id);
1984 err = got_error_from_errno("got_object_id_dup");
1986 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1989 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1993 got_object_commit_close(commit);
1995 got_object_tree_close(tree);
2000 * Normalize file mode bits to avoid false positive tree entry differences
2001 * in case tree entries have unexpected mode bits set.
2004 normalize_mode_for_comparison(mode_t mode)
2007 * For directories, the only relevant bit is the IFDIR bit.
2008 * This allows us to detect paths changing from a directory
2009 * to a file and vice versa.
2012 return mode & S_IFDIR;
2015 * For symlinks, the only relevant bit is the IFLNK bit.
2016 * This allows us to detect paths changing from a symlinks
2017 * to a file or directory and vice versa.
2020 return mode & S_IFLNK;
2022 /* For files, the only change we care about is the executable bit. */
2023 return mode & S_IXUSR;
2026 const struct got_error *
2027 got_object_tree_path_changed(int *changed,
2028 struct got_tree_object *tree01, struct got_tree_object *tree02,
2029 const char *path, struct got_repository *repo)
2031 const struct got_error *err = NULL;
2032 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2033 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2034 const char *seg, *s;
2039 /* We not do support comparing the root path. */
2040 if (got_path_is_root_dir(path))
2041 return got_error_path(path, GOT_ERR_BAD_PATH);
2051 struct got_tree_object *next_tree1, *next_tree2;
2052 mode_t mode1, mode2;
2061 te1 = find_entry_by_name(tree1, seg, seglen);
2063 err = got_error(GOT_ERR_NO_OBJ);
2068 te2 = find_entry_by_name(tree2, seg, seglen);
2071 mode1 = normalize_mode_for_comparison(te1->mode);
2072 mode2 = normalize_mode_for_comparison(te2->mode);
2073 if (mode1 != mode2) {
2078 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2084 if (*s == '\0') { /* final path element */
2093 err = got_object_open_as_tree(&next_tree1, repo,
2098 if (tree1 != tree01)
2099 got_object_tree_close(tree1);
2103 err = got_object_open_as_tree(&next_tree2, repo,
2108 if (tree2 != tree02)
2109 got_object_tree_close(tree2);
2112 if (tree2 != tree02)
2113 got_object_tree_close(tree2);
2119 if (tree1 && tree1 != tree01)
2120 got_object_tree_close(tree1);
2121 if (tree2 && tree2 != tree02)
2122 got_object_tree_close(tree2);
2126 const struct got_error *
2127 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2128 struct got_tree_entry *te)
2130 const struct got_error *err = NULL;
2132 *new_te = calloc(1, sizeof(**new_te));
2133 if (*new_te == NULL)
2134 return got_error_from_errno("calloc");
2136 (*new_te)->mode = te->mode;
2137 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2138 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2143 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2145 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2149 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2151 /* S_IFDIR check avoids confusing symlinks with submodules. */
2152 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2155 static const struct got_error *
2156 resolve_symlink(char **link_target, const char *path,
2157 struct got_object_id *commit_id, struct got_repository *repo)
2159 const struct got_error *err = NULL;
2161 char *name, *parent_path = NULL;
2162 struct got_object_id *tree_obj_id = NULL;
2163 struct got_tree_object *tree = NULL;
2164 struct got_tree_entry *te = NULL;
2166 *link_target = NULL;
2168 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2169 return got_error(GOT_ERR_NO_SPACE);
2171 name = basename(buf);
2173 return got_error_from_errno2("basename", path);
2175 err = got_path_dirname(&parent_path, path);
2179 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2182 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2183 /* Display the complete path in error message. */
2184 err = got_error_path(path, err->code);
2189 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2193 te = got_object_tree_find_entry(tree, name);
2195 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2199 if (got_object_tree_entry_is_symlink(te)) {
2200 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2203 if (!got_path_is_absolute(*link_target)) {
2205 if (asprintf(&abspath, "%s/%s", parent_path,
2206 *link_target) == -1) {
2207 err = got_error_from_errno("asprintf");
2211 *link_target = malloc(PATH_MAX);
2212 if (*link_target == NULL) {
2213 err = got_error_from_errno("malloc");
2216 err = got_canonpath(abspath, *link_target, PATH_MAX);
2225 got_object_tree_close(tree);
2228 *link_target = NULL;
2233 const struct got_error *
2234 got_object_resolve_symlinks(char **link_target, const char *path,
2235 struct got_object_id *commit_id, struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 char *next_target = NULL;
2239 int max_recursion = 40; /* matches Git */
2241 *link_target = NULL;
2244 err = resolve_symlink(&next_target,
2245 *link_target ? *link_target : path, commit_id, repo);
2250 if (--max_recursion == 0) {
2251 err = got_error_path(path, GOT_ERR_RECURSION);
2252 *link_target = NULL;
2255 *link_target = next_target;
2257 } while (next_target);
2262 const struct got_error *
2263 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2264 struct got_object_id *commit_id, const char *path,
2265 struct got_repository *repo)
2267 const struct got_error *err = NULL;
2268 struct got_pack *pack = NULL;
2269 struct got_packidx *packidx = NULL;
2270 char *path_packfile = NULL;
2271 struct got_commit_object *changed_commit = NULL;
2272 struct got_object_id *changed_commit_id = NULL;
2275 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2277 if (err->code != GOT_ERR_NO_OBJ)
2282 err = got_packidx_get_packfile_path(&path_packfile,
2283 packidx->path_packidx);
2287 pack = got_repo_get_cached_pack(repo, path_packfile);
2289 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2294 if (pack->privsep_child == NULL) {
2295 err = start_pack_privsep_child(pack, packidx);
2300 err = got_privsep_send_commit_traversal_request(
2301 pack->privsep_child->ibuf, commit_id, idx, path);
2305 err = got_privsep_recv_traversed_commits(&changed_commit,
2306 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2310 if (changed_commit) {
2312 * Cache the commit in which the path was changed.
2313 * This commit might be opened again soon.
2315 changed_commit->refcnt++;
2316 err = got_repo_cache_commit(repo, changed_commit_id,
2318 got_object_commit_close(changed_commit);
2321 free(path_packfile);
2322 free(changed_commit_id);