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>
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 /* *outfd must be initialized to -1 by caller */
532 const struct got_error *
533 got_object_raw_open(struct got_raw_object **obj, int *outfd,
534 struct got_repository *repo, struct got_object_id *id, size_t blocksize)
536 const struct got_error *err = NULL;
537 struct got_packidx *packidx = NULL;
539 uint8_t *outbuf = NULL;
542 char *path_packfile = NULL;
544 *obj = got_repo_get_cached_raw_object(repo, id);
551 *outfd = got_opentempfd();
553 return got_error_from_errno("got_opentempfd");
556 err = got_repo_search_packidx(&packidx, &idx, repo, id);
558 struct got_pack *pack = NULL;
560 err = got_packidx_get_packfile_path(&path_packfile,
561 packidx->path_packidx);
565 pack = got_repo_get_cached_pack(repo, path_packfile);
567 err = got_repo_cache_pack(&pack, repo, path_packfile,
572 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
573 *outfd, pack, packidx, idx, id);
576 } else if (err->code == GOT_ERR_NO_OBJ) {
579 err = got_object_open_loose_fd(&fd, id, repo);
582 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
588 *obj = calloc(1, sizeof(**obj));
590 err = got_error_from_errno("calloc");
594 (*obj)->read_buf = malloc(blocksize);
595 if ((*obj)->read_buf == NULL) {
596 err = got_error_from_errno("malloc");
601 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
602 if ((*obj)->f == NULL) {
603 err = got_error_from_errno("fdopen");
606 (*obj)->data = outbuf;
609 if (fstat(*outfd, &sb) == -1) {
610 err = got_error_from_errno("fstat");
614 if (sb.st_size != hdrlen + size) {
615 err = got_error(GOT_ERR_PRIVSEP_LEN);
619 (*obj)->f = fdopen(*outfd, "r");
620 if ((*obj)->f == NULL) {
621 err = got_error_from_errno("fdopen");
627 (*obj)->hdrlen = hdrlen;
629 (*obj)->blocksize = blocksize;
630 err = got_repo_cache_raw_object(repo, id, *obj);
635 got_object_raw_close(*obj);
645 got_object_raw_rewind(struct got_raw_object *obj)
652 got_object_raw_get_hdrlen(struct got_raw_object *obj)
658 got_object_raw_get_read_buf(struct got_raw_object *obj)
660 return obj->read_buf;
663 const struct got_error *
664 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
668 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
669 if (n == 0 && ferror(obj->f))
670 return got_ferror(obj->f, GOT_ERR_IO);
675 const struct got_error *
676 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
679 struct got_object_id id;
681 if (!got_parse_sha1_digest(id.sha1, id_str))
682 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
684 return got_object_open(obj, repo, &id);
687 const struct got_error *
688 got_object_resolve_id_str(struct got_object_id **id,
689 struct got_repository *repo, const char *id_str)
691 const struct got_error *err = NULL;
692 struct got_object *obj;
694 err = got_object_open_by_id_str(&obj, repo, id_str);
698 *id = got_object_id_dup(got_object_get_id(obj));
699 got_object_close(obj);
701 return got_error_from_errno("got_object_id_dup");
706 static const struct got_error *
707 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
708 int pack_idx, struct got_object_id *id)
710 const struct got_error *err = NULL;
712 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
717 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
721 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
725 static const struct got_error *
726 read_packed_commit_privsep(struct got_commit_object **commit,
727 struct got_pack *pack, struct got_packidx *packidx, int idx,
728 struct got_object_id *id)
730 const struct got_error *err = NULL;
732 if (pack->privsep_child)
733 return request_packed_commit(commit, pack, idx, id);
735 err = start_pack_privsep_child(pack, packidx);
739 return request_packed_commit(commit, pack, idx, id);
742 static const struct got_error *
743 request_commit(struct got_commit_object **commit, struct got_repository *repo,
744 int fd, struct got_object_id *id)
746 const struct got_error *err = NULL;
747 struct imsgbuf *ibuf;
749 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
751 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
755 return got_privsep_recv_commit(commit, ibuf);
758 static const struct got_error *
759 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
760 struct got_object_id *id, struct got_repository *repo)
762 const struct got_error *err;
765 struct imsgbuf *ibuf;
767 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
768 return request_commit(commit, repo, obj_fd, id);
770 ibuf = calloc(1, sizeof(*ibuf));
772 return got_error_from_errno("calloc");
774 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
775 err = got_error_from_errno("socketpair");
782 err = got_error_from_errno("fork");
787 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
792 if (close(imsg_fds[1]) == -1) {
793 err = got_error_from_errno("close");
797 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
799 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
800 imsg_init(ibuf, imsg_fds[0]);
801 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
803 return request_commit(commit, repo, obj_fd, id);
807 static const struct got_error *
808 open_commit(struct got_commit_object **commit,
809 struct got_repository *repo, struct got_object_id *id, int check_cache)
811 const struct got_error *err = NULL;
812 struct got_packidx *packidx = NULL;
814 char *path_packfile = NULL;
817 *commit = got_repo_get_cached_commit(repo, id);
818 if (*commit != NULL) {
825 err = got_repo_search_packidx(&packidx, &idx, repo, id);
827 struct got_pack *pack = NULL;
829 err = got_packidx_get_packfile_path(&path_packfile,
830 packidx->path_packidx);
834 pack = got_repo_get_cached_pack(repo, path_packfile);
836 err = got_repo_cache_pack(&pack, repo, path_packfile,
841 err = read_packed_commit_privsep(commit, pack,
843 } else if (err->code == GOT_ERR_NO_OBJ) {
846 err = got_object_open_loose_fd(&fd, id, repo);
849 err = read_commit_privsep(commit, fd, id, repo);
854 err = got_repo_cache_commit(repo, id, *commit);
861 const struct got_error *
862 got_object_open_as_commit(struct got_commit_object **commit,
863 struct got_repository *repo, struct got_object_id *id)
865 *commit = got_repo_get_cached_commit(repo, id);
866 if (*commit != NULL) {
871 return open_commit(commit, repo, id, 0);
874 const struct got_error *
875 got_object_commit_open(struct got_commit_object **commit,
876 struct got_repository *repo, struct got_object *obj)
878 return open_commit(commit, repo, got_object_get_id(obj), 1);
881 const struct got_error *
882 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
884 const struct got_error *err = NULL;
886 *qid = calloc(1, sizeof(**qid));
888 return got_error_from_errno("calloc");
890 (*qid)->id = got_object_id_dup(id);
891 if ((*qid)->id == NULL) {
892 err = got_error_from_errno("got_object_id_dup");
893 got_object_qid_free(*qid);
901 const struct got_error *
902 got_object_id_queue_copy(const struct got_object_id_queue *src,
903 struct got_object_id_queue *dest)
905 const struct got_error *err;
906 struct got_object_qid *qid;
908 STAILQ_FOREACH(qid, src, entry) {
909 struct got_object_qid *new;
911 * Deep-copy the object ID only. Let the caller deal
912 * with setting up the new->data pointer if needed.
914 err = got_object_qid_alloc(&new, qid->id);
916 got_object_id_queue_free(dest);
919 STAILQ_INSERT_TAIL(dest, new, entry);
925 static const struct got_error *
926 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
927 int pack_idx, struct got_object_id *id)
929 const struct got_error *err = NULL;
931 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
936 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
939 static const struct got_error *
940 read_packed_tree_privsep(struct got_tree_object **tree,
941 struct got_pack *pack, struct got_packidx *packidx, int idx,
942 struct got_object_id *id)
944 const struct got_error *err = NULL;
946 if (pack->privsep_child)
947 return request_packed_tree(tree, pack, idx, id);
949 err = start_pack_privsep_child(pack, packidx);
953 return request_packed_tree(tree, pack, idx, id);
956 static const struct got_error *
957 request_tree(struct got_tree_object **tree, struct got_repository *repo,
958 int fd, struct got_object_id *id)
960 const struct got_error *err = NULL;
961 struct imsgbuf *ibuf;
963 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
965 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
969 return got_privsep_recv_tree(tree, ibuf);
972 const struct got_error *
973 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
974 struct got_object_id *id, struct got_repository *repo)
976 const struct got_error *err;
979 struct imsgbuf *ibuf;
981 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
982 return request_tree(tree, repo, obj_fd, id);
984 ibuf = calloc(1, sizeof(*ibuf));
986 return got_error_from_errno("calloc");
988 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
989 err = got_error_from_errno("socketpair");
996 err = got_error_from_errno("fork");
1000 else if (pid == 0) {
1001 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1006 if (close(imsg_fds[1]) == -1) {
1007 err = got_error_from_errno("close");
1011 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1013 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1014 imsg_init(ibuf, imsg_fds[0]);
1015 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1018 return request_tree(tree, repo, obj_fd, id);
1021 static const struct got_error *
1022 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1023 struct got_object_id *id, int check_cache)
1025 const struct got_error *err = NULL;
1026 struct got_packidx *packidx = NULL;
1028 char *path_packfile = NULL;
1031 *tree = got_repo_get_cached_tree(repo, id);
1032 if (*tree != NULL) {
1039 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1041 struct got_pack *pack = NULL;
1043 err = got_packidx_get_packfile_path(&path_packfile,
1044 packidx->path_packidx);
1048 pack = got_repo_get_cached_pack(repo, path_packfile);
1050 err = got_repo_cache_pack(&pack, repo, path_packfile,
1055 err = read_packed_tree_privsep(tree, pack,
1057 } else if (err->code == GOT_ERR_NO_OBJ) {
1060 err = got_object_open_loose_fd(&fd, id, repo);
1063 err = read_tree_privsep(tree, fd, id, repo);
1068 err = got_repo_cache_tree(repo, id, *tree);
1071 free(path_packfile);
1075 const struct got_error *
1076 got_object_open_as_tree(struct got_tree_object **tree,
1077 struct got_repository *repo, struct got_object_id *id)
1079 *tree = got_repo_get_cached_tree(repo, id);
1080 if (*tree != NULL) {
1085 return open_tree(tree, repo, id, 0);
1088 const struct got_error *
1089 got_object_tree_open(struct got_tree_object **tree,
1090 struct got_repository *repo, struct got_object *obj)
1092 return open_tree(tree, repo, got_object_get_id(obj), 1);
1096 got_object_tree_get_nentries(struct got_tree_object *tree)
1098 return tree->nentries;
1101 struct got_tree_entry *
1102 got_object_tree_get_first_entry(struct got_tree_object *tree)
1104 return got_object_tree_get_entry(tree, 0);
1107 struct got_tree_entry *
1108 got_object_tree_get_last_entry(struct got_tree_object *tree)
1110 return got_object_tree_get_entry(tree, tree->nentries - 1);
1113 struct got_tree_entry *
1114 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1116 if (i < 0 || i >= tree->nentries)
1118 return &tree->entries[i];
1122 got_tree_entry_get_mode(struct got_tree_entry *te)
1128 got_tree_entry_get_name(struct got_tree_entry *te)
1130 return &te->name[0];
1133 struct got_object_id *
1134 got_tree_entry_get_id(struct got_tree_entry *te)
1139 const struct got_error *
1140 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1142 const struct got_error *err = NULL;
1143 size_t len, totlen, hdrlen, offset;
1147 hdrlen = got_object_blob_get_hdrlen(blob);
1153 err = got_object_blob_read_block(&len, blob);
1160 totlen += len - hdrlen;
1161 p = realloc(*s, totlen + 1);
1163 err = got_error_from_errno("realloc");
1169 /* Skip blob object header first time around. */
1171 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1176 (*s)[totlen] = '\0';
1180 const struct got_error *
1181 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 struct got_blob_object *blob = NULL;
1187 *link_target = NULL;
1189 if (!got_object_tree_entry_is_symlink(te))
1190 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1192 err = got_object_open_as_blob(&blob, repo,
1193 got_tree_entry_get_id(te), PATH_MAX);
1197 err = got_object_blob_read_to_str(link_target, blob);
1198 got_object_blob_close(blob);
1201 *link_target = NULL;
1207 got_tree_entry_get_index(struct got_tree_entry *te)
1212 struct got_tree_entry *
1213 got_tree_entry_get_next(struct got_tree_object *tree,
1214 struct got_tree_entry *te)
1216 return got_object_tree_get_entry(tree, te->idx + 1);
1219 struct got_tree_entry *
1220 got_tree_entry_get_prev(struct got_tree_object *tree,
1221 struct got_tree_entry *te)
1223 return got_object_tree_get_entry(tree, te->idx - 1);
1226 static const struct got_error *
1227 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1228 struct got_pack *pack, struct got_packidx *packidx, int idx,
1229 struct got_object_id *id)
1231 const struct got_error *err = NULL;
1233 int basefd, accumfd; /* temporary files for delta application */
1235 basefd = got_opentempfd();
1237 return got_error_from_errno("got_opentempfd");
1238 accumfd = got_opentempfd();
1240 return got_error_from_errno("got_opentempfd");
1242 outfd_child = dup(outfd);
1243 if (outfd_child == -1)
1244 return got_error_from_errno("dup");
1246 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1250 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1258 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1265 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1270 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1271 pack->privsep_child->ibuf);
1275 if (lseek(outfd, SEEK_SET, 0) == -1)
1276 err = got_error_from_errno("lseek");
1281 static const struct got_error *
1282 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1283 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1284 struct got_object_id *id)
1286 const struct got_error *err = NULL;
1288 if (pack->privsep_child == NULL) {
1289 err = start_pack_privsep_child(pack, packidx);
1294 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1298 static const struct got_error *
1299 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1300 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1302 const struct got_error *err = NULL;
1305 outfd_child = dup(outfd);
1306 if (outfd_child == -1)
1307 return got_error_from_errno("dup");
1309 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1313 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1317 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1321 if (lseek(outfd, SEEK_SET, 0) == -1)
1322 return got_error_from_errno("lseek");
1327 static const struct got_error *
1328 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1329 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1331 const struct got_error *err;
1334 struct imsgbuf *ibuf;
1336 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1337 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1338 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1342 ibuf = calloc(1, sizeof(*ibuf));
1344 return got_error_from_errno("calloc");
1346 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1347 err = got_error_from_errno("socketpair");
1354 err = got_error_from_errno("fork");
1358 else if (pid == 0) {
1359 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1364 if (close(imsg_fds[1]) == -1) {
1365 err = got_error_from_errno("close");
1369 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1371 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1372 imsg_init(ibuf, imsg_fds[0]);
1373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1375 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1378 static const struct got_error *
1379 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1380 struct got_object_id *id, size_t blocksize)
1382 const struct got_error *err = NULL;
1383 struct got_packidx *packidx = NULL;
1385 char *path_packfile = NULL;
1388 size_t size, hdrlen;
1391 *blob = calloc(1, sizeof(**blob));
1393 return got_error_from_errno("calloc");
1395 outfd = got_opentempfd();
1397 return got_error_from_errno("got_opentempfd");
1399 (*blob)->read_buf = malloc(blocksize);
1400 if ((*blob)->read_buf == NULL) {
1401 err = got_error_from_errno("malloc");
1405 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1407 struct got_pack *pack = NULL;
1409 err = got_packidx_get_packfile_path(&path_packfile,
1410 packidx->path_packidx);
1414 pack = got_repo_get_cached_pack(repo, path_packfile);
1416 err = got_repo_cache_pack(&pack, repo, path_packfile,
1421 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1422 pack, packidx, idx, id);
1423 } else if (err->code == GOT_ERR_NO_OBJ) {
1426 err = got_object_open_loose_fd(&infd, id, repo);
1429 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1435 if (hdrlen > size) {
1436 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1441 if (close(outfd) == -1 && err == NULL)
1442 err = got_error_from_errno("close");
1444 (*blob)->f = fmemopen(outbuf, size, "rb");
1445 if ((*blob)->f == NULL) {
1446 err = got_error_from_errno("fmemopen");
1450 (*blob)->data = outbuf;
1452 if (fstat(outfd, &sb) == -1) {
1453 err = got_error_from_errno("fstat");
1457 if (sb.st_size != size) {
1458 err = got_error(GOT_ERR_PRIVSEP_LEN);
1462 (*blob)->f = fdopen(outfd, "rb");
1463 if ((*blob)->f == NULL) {
1464 err = got_error_from_errno("fdopen");
1471 (*blob)->hdrlen = hdrlen;
1472 (*blob)->blocksize = blocksize;
1473 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1476 free(path_packfile);
1479 got_object_blob_close(*blob);
1481 } else if (outfd != -1)
1487 const struct got_error *
1488 got_object_open_as_blob(struct got_blob_object **blob,
1489 struct got_repository *repo, struct got_object_id *id,
1492 return open_blob(blob, repo, id, blocksize);
1495 const struct got_error *
1496 got_object_blob_open(struct got_blob_object **blob,
1497 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1499 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1502 const struct got_error *
1503 got_object_blob_close(struct got_blob_object *blob)
1505 const struct got_error *err = NULL;
1506 free(blob->read_buf);
1507 if (blob->f && fclose(blob->f) == EOF)
1508 err = got_error_from_errno("fclose");
1515 got_object_blob_rewind(struct got_blob_object *blob)
1522 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1524 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1528 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1530 return blob->hdrlen;
1534 got_object_blob_get_read_buf(struct got_blob_object *blob)
1536 return blob->read_buf;
1539 const struct got_error *
1540 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1544 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1545 if (n == 0 && ferror(blob->f))
1546 return got_ferror(blob->f, GOT_ERR_IO);
1551 const struct got_error *
1552 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1553 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1555 const struct got_error *err = NULL;
1556 size_t n, len, hdrlen;
1559 const int alloc_chunksz = 512;
1561 off_t off = 0, total_len = 0;
1564 *line_offsets = NULL;
1570 hdrlen = got_object_blob_get_hdrlen(blob);
1572 err = got_object_blob_read_block(&len, blob);
1577 buf = got_object_blob_get_read_buf(blob);
1580 if (line_offsets && *line_offsets == NULL) {
1581 /* Have some data but perhaps no '\n'. */
1583 nalloc = alloc_chunksz;
1584 *line_offsets = calloc(nalloc,
1585 sizeof(**line_offsets));
1586 if (*line_offsets == NULL)
1587 return got_error_from_errno("calloc");
1589 /* Skip forward over end of first line. */
1596 /* Scan '\n' offsets in remaining chunk of data. */
1598 if (buf[i] != '\n') {
1603 if (line_offsets && nalloc < *nlines) {
1604 size_t n = *nlines + alloc_chunksz;
1605 off_t *o = recallocarray(*line_offsets,
1606 nalloc, n, sizeof(**line_offsets));
1608 free(*line_offsets);
1609 *line_offsets = NULL;
1610 return got_error_from_errno(
1617 off = total_len + i - hdrlen + 1;
1618 (*line_offsets)[*nlines - 1] = off;
1623 /* Skip blob object header first time around. */
1624 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1625 if (n != len - hdrlen)
1626 return got_ferror(outfile, GOT_ERR_IO);
1627 total_len += len - hdrlen;
1631 if (fflush(outfile) != 0)
1632 return got_error_from_errno("fflush");
1636 *filesize = total_len;
1641 static const struct got_error *
1642 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1643 int pack_idx, struct got_object_id *id)
1645 const struct got_error *err = NULL;
1647 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1652 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1655 static const struct got_error *
1656 read_packed_tag_privsep(struct got_tag_object **tag,
1657 struct got_pack *pack, struct got_packidx *packidx, int idx,
1658 struct got_object_id *id)
1660 const struct got_error *err = NULL;
1662 if (pack->privsep_child)
1663 return request_packed_tag(tag, pack, idx, id);
1665 err = start_pack_privsep_child(pack, packidx);
1669 return request_packed_tag(tag, pack, idx, id);
1672 static const struct got_error *
1673 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1674 int fd, struct got_object_id *id)
1676 const struct got_error *err = NULL;
1677 struct imsgbuf *ibuf;
1679 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1681 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1685 return got_privsep_recv_tag(tag, ibuf);
1688 static const struct got_error *
1689 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1690 struct got_object_id *id, struct got_repository *repo)
1692 const struct got_error *err;
1695 struct imsgbuf *ibuf;
1697 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1698 return request_tag(tag, repo, obj_fd, id);
1700 ibuf = calloc(1, sizeof(*ibuf));
1702 return got_error_from_errno("calloc");
1704 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1705 err = got_error_from_errno("socketpair");
1712 err = got_error_from_errno("fork");
1716 else if (pid == 0) {
1717 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1722 if (close(imsg_fds[1]) == -1) {
1723 err = got_error_from_errno("close");
1727 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1729 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1730 imsg_init(ibuf, imsg_fds[0]);
1731 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1733 return request_tag(tag, repo, obj_fd, id);
1736 static const struct got_error *
1737 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1738 struct got_object_id *id, int check_cache)
1740 const struct got_error *err = NULL;
1741 struct got_packidx *packidx = NULL;
1743 char *path_packfile = NULL;
1744 struct got_object *obj = NULL;
1745 int obj_type = GOT_OBJ_TYPE_ANY;
1748 *tag = got_repo_get_cached_tag(repo, id);
1756 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1758 struct got_pack *pack = NULL;
1760 err = got_packidx_get_packfile_path(&path_packfile,
1761 packidx->path_packidx);
1765 pack = got_repo_get_cached_pack(repo, path_packfile);
1767 err = got_repo_cache_pack(&pack, repo, path_packfile,
1773 /* Beware of "lightweight" tags: Check object type first. */
1774 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1778 obj_type = obj->type;
1779 got_object_close(obj);
1780 if (obj_type != GOT_OBJ_TYPE_TAG) {
1781 err = got_error(GOT_ERR_OBJ_TYPE);
1784 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1785 } else if (err->code == GOT_ERR_NO_OBJ) {
1788 err = got_object_open_loose_fd(&fd, id, repo);
1791 err = got_object_read_header_privsep(&obj, id, repo, fd);
1794 obj_type = obj->type;
1795 got_object_close(obj);
1796 if (obj_type != GOT_OBJ_TYPE_TAG)
1797 return got_error(GOT_ERR_OBJ_TYPE);
1799 err = got_object_open_loose_fd(&fd, id, repo);
1802 err = read_tag_privsep(tag, fd, id, repo);
1807 err = got_repo_cache_tag(repo, id, *tag);
1810 free(path_packfile);
1814 const struct got_error *
1815 got_object_open_as_tag(struct got_tag_object **tag,
1816 struct got_repository *repo, struct got_object_id *id)
1818 *tag = got_repo_get_cached_tag(repo, id);
1824 return open_tag(tag, repo, id, 0);
1827 const struct got_error *
1828 got_object_tag_open(struct got_tag_object **tag,
1829 struct got_repository *repo, struct got_object *obj)
1831 return open_tag(tag, repo, got_object_get_id(obj), 1);
1835 got_object_tag_get_name(struct got_tag_object *tag)
1841 got_object_tag_get_object_type(struct got_tag_object *tag)
1843 return tag->obj_type;
1846 struct got_object_id *
1847 got_object_tag_get_object_id(struct got_tag_object *tag)
1853 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1855 return tag->tagger_time;
1859 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1861 return tag->tagger_gmtoff;
1865 got_object_tag_get_tagger(struct got_tag_object *tag)
1871 got_object_tag_get_message(struct got_tag_object *tag)
1876 static struct got_tree_entry *
1877 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1881 /* Note that tree entries are sorted in strncmp() order. */
1882 for (i = 0; i < tree->nentries; i++) {
1883 struct got_tree_entry *te = &tree->entries[i];
1884 int cmp = strncmp(te->name, name, len);
1889 if (te->name[len] == '\0')
1895 struct got_tree_entry *
1896 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1898 return find_entry_by_name(tree, name, strlen(name));
1901 const struct got_error *
1902 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1903 struct got_repository *repo, struct got_tree_object *tree,
1906 const struct got_error *err = NULL;
1907 struct got_tree_object *subtree = NULL;
1908 struct got_tree_entry *te = NULL;
1909 const char *seg, *s;
1921 struct got_tree_object *next_tree;
1930 te = find_entry_by_name(subtree, seg, seglen);
1932 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1943 err = got_object_open_as_tree(&next_tree, repo,
1948 if (subtree != tree)
1949 got_object_tree_close(subtree);
1950 subtree = next_tree;
1955 *id = got_object_id_dup(&te->id);
1957 return got_error_from_errno("got_object_id_dup");
1961 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1963 if (subtree && subtree != tree)
1964 got_object_tree_close(subtree);
1967 const struct got_error *
1968 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1969 struct got_object_id *commit_id, const char *path)
1971 const struct got_error *err = NULL;
1972 struct got_commit_object *commit = NULL;
1973 struct got_tree_object *tree = NULL;
1977 err = got_object_open_as_commit(&commit, repo, commit_id);
1981 /* Handle opening of root of commit's tree. */
1982 if (got_path_is_root_dir(path)) {
1983 *id = got_object_id_dup(commit->tree_id);
1985 err = got_error_from_errno("got_object_id_dup");
1987 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1990 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1994 got_object_commit_close(commit);
1996 got_object_tree_close(tree);
2001 * Normalize file mode bits to avoid false positive tree entry differences
2002 * in case tree entries have unexpected mode bits set.
2005 normalize_mode_for_comparison(mode_t mode)
2008 * For directories, the only relevant bit is the IFDIR bit.
2009 * This allows us to detect paths changing from a directory
2010 * to a file and vice versa.
2013 return mode & S_IFDIR;
2016 * For symlinks, the only relevant bit is the IFLNK bit.
2017 * This allows us to detect paths changing from a symlinks
2018 * to a file or directory and vice versa.
2021 return mode & S_IFLNK;
2023 /* For files, the only change we care about is the executable bit. */
2024 return mode & S_IXUSR;
2027 const struct got_error *
2028 got_object_tree_path_changed(int *changed,
2029 struct got_tree_object *tree01, struct got_tree_object *tree02,
2030 const char *path, struct got_repository *repo)
2032 const struct got_error *err = NULL;
2033 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2034 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2035 const char *seg, *s;
2040 /* We not do support comparing the root path. */
2041 if (got_path_is_root_dir(path))
2042 return got_error_path(path, GOT_ERR_BAD_PATH);
2052 struct got_tree_object *next_tree1, *next_tree2;
2053 mode_t mode1, mode2;
2062 te1 = find_entry_by_name(tree1, seg, seglen);
2064 err = got_error(GOT_ERR_NO_OBJ);
2069 te2 = find_entry_by_name(tree2, seg, seglen);
2072 mode1 = normalize_mode_for_comparison(te1->mode);
2073 mode2 = normalize_mode_for_comparison(te2->mode);
2074 if (mode1 != mode2) {
2079 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2085 if (*s == '\0') { /* final path element */
2094 err = got_object_open_as_tree(&next_tree1, repo,
2099 if (tree1 != tree01)
2100 got_object_tree_close(tree1);
2104 err = got_object_open_as_tree(&next_tree2, repo,
2109 if (tree2 != tree02)
2110 got_object_tree_close(tree2);
2113 if (tree2 != tree02)
2114 got_object_tree_close(tree2);
2120 if (tree1 && tree1 != tree01)
2121 got_object_tree_close(tree1);
2122 if (tree2 && tree2 != tree02)
2123 got_object_tree_close(tree2);
2127 const struct got_error *
2128 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2129 struct got_tree_entry *te)
2131 const struct got_error *err = NULL;
2133 *new_te = calloc(1, sizeof(**new_te));
2134 if (*new_te == NULL)
2135 return got_error_from_errno("calloc");
2137 (*new_te)->mode = te->mode;
2138 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2139 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2144 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2146 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2150 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2152 /* S_IFDIR check avoids confusing symlinks with submodules. */
2153 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2156 static const struct got_error *
2157 resolve_symlink(char **link_target, const char *path,
2158 struct got_object_id *commit_id, struct got_repository *repo)
2160 const struct got_error *err = NULL;
2162 char *name, *parent_path = NULL;
2163 struct got_object_id *tree_obj_id = NULL;
2164 struct got_tree_object *tree = NULL;
2165 struct got_tree_entry *te = NULL;
2167 *link_target = NULL;
2169 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2170 return got_error(GOT_ERR_NO_SPACE);
2172 name = basename(buf);
2174 return got_error_from_errno2("basename", path);
2176 err = got_path_dirname(&parent_path, path);
2180 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2183 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2184 /* Display the complete path in error message. */
2185 err = got_error_path(path, err->code);
2190 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2194 te = got_object_tree_find_entry(tree, name);
2196 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2200 if (got_object_tree_entry_is_symlink(te)) {
2201 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2204 if (!got_path_is_absolute(*link_target)) {
2206 if (asprintf(&abspath, "%s/%s", parent_path,
2207 *link_target) == -1) {
2208 err = got_error_from_errno("asprintf");
2212 *link_target = malloc(PATH_MAX);
2213 if (*link_target == NULL) {
2214 err = got_error_from_errno("malloc");
2217 err = got_canonpath(abspath, *link_target, PATH_MAX);
2226 got_object_tree_close(tree);
2229 *link_target = NULL;
2234 const struct got_error *
2235 got_object_resolve_symlinks(char **link_target, const char *path,
2236 struct got_object_id *commit_id, struct got_repository *repo)
2238 const struct got_error *err = NULL;
2239 char *next_target = NULL;
2240 int max_recursion = 40; /* matches Git */
2242 *link_target = NULL;
2245 err = resolve_symlink(&next_target,
2246 *link_target ? *link_target : path, commit_id, repo);
2251 if (--max_recursion == 0) {
2252 err = got_error_path(path, GOT_ERR_RECURSION);
2253 *link_target = NULL;
2256 *link_target = next_target;
2258 } while (next_target);
2263 const struct got_error *
2264 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2265 struct got_object_id *commit_id, const char *path,
2266 struct got_repository *repo)
2268 const struct got_error *err = NULL;
2269 struct got_pack *pack = NULL;
2270 struct got_packidx *packidx = NULL;
2271 char *path_packfile = NULL;
2272 struct got_commit_object *changed_commit = NULL;
2273 struct got_object_id *changed_commit_id = NULL;
2276 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2278 if (err->code != GOT_ERR_NO_OBJ)
2283 err = got_packidx_get_packfile_path(&path_packfile,
2284 packidx->path_packidx);
2288 pack = got_repo_get_cached_pack(repo, path_packfile);
2290 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2295 if (pack->privsep_child == NULL) {
2296 err = start_pack_privsep_child(pack, packidx);
2301 err = got_privsep_send_commit_traversal_request(
2302 pack->privsep_child->ibuf, commit_id, idx, path);
2306 err = got_privsep_recv_traversed_commits(&changed_commit,
2307 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2311 if (changed_commit) {
2313 * Cache the commit in which the path was changed.
2314 * This commit might be opened again soon.
2316 changed_commit->refcnt++;
2317 err = got_repo_cache_commit(repo, changed_commit_id,
2319 got_object_commit_close(changed_commit);
2322 free(path_packfile);
2323 free(changed_commit_id);