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 const struct got_error *
532 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
533 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;
542 char *path_packfile = NULL;
546 outfd = got_opentempfd();
548 return got_error_from_errno("got_opentempfd");
550 err = got_repo_search_packidx(&packidx, &idx, repo, id);
552 struct got_pack *pack = NULL;
554 err = got_packidx_get_packfile_path(&path_packfile,
555 packidx->path_packidx);
559 pack = got_repo_get_cached_pack(repo, path_packfile);
561 err = got_repo_cache_pack(&pack, repo, path_packfile,
566 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
567 outfd, pack, packidx, idx, id);
570 } else if (err->code == GOT_ERR_NO_OBJ) {
573 err = got_object_open_loose_fd(&fd, id, repo);
576 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
582 *obj = calloc(1, sizeof(**obj));
584 err = got_error_from_errno("calloc");
588 (*obj)->read_buf = malloc(blocksize);
589 if ((*obj)->read_buf == NULL) {
590 err = got_error_from_errno("malloc");
595 if (close(outfd) == -1) {
596 err = got_error_from_errno("close");
600 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
601 if ((*obj)->f == NULL) {
602 err = got_error_from_errno("fdopen");
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);
618 (*obj)->f = fdopen(outfd, "r");
619 if ((*obj)->f == NULL) {
620 err = got_error_from_errno("fdopen");
626 (*obj)->hdrlen = hdrlen;
628 (*obj)->blocksize = blocksize;
633 got_object_raw_close(*obj);
644 got_object_raw_rewind(struct got_raw_object *obj)
651 got_object_raw_get_hdrlen(struct got_raw_object *obj)
657 got_object_raw_get_read_buf(struct got_raw_object *obj)
659 return obj->read_buf;
662 const struct got_error *
663 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
667 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
668 if (n == 0 && ferror(obj->f))
669 return got_ferror(obj->f, GOT_ERR_IO);
674 const struct got_error *
675 got_object_raw_close(struct got_raw_object *obj)
677 const struct got_error *err = NULL;
680 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
681 err = got_error_from_errno("fclose");
687 const struct got_error *
688 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
691 struct got_object_id id;
693 if (!got_parse_sha1_digest(id.sha1, id_str))
694 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
696 return got_object_open(obj, repo, &id);
699 const struct got_error *
700 got_object_resolve_id_str(struct got_object_id **id,
701 struct got_repository *repo, const char *id_str)
703 const struct got_error *err = NULL;
704 struct got_object *obj;
706 err = got_object_open_by_id_str(&obj, repo, id_str);
710 *id = got_object_id_dup(got_object_get_id(obj));
711 got_object_close(obj);
713 return got_error_from_errno("got_object_id_dup");
718 static const struct got_error *
719 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
720 int pack_idx, struct got_object_id *id)
722 const struct got_error *err = NULL;
724 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
729 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
733 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
737 static const struct got_error *
738 read_packed_commit_privsep(struct got_commit_object **commit,
739 struct got_pack *pack, struct got_packidx *packidx, int idx,
740 struct got_object_id *id)
742 const struct got_error *err = NULL;
744 if (pack->privsep_child)
745 return request_packed_commit(commit, pack, idx, id);
747 err = start_pack_privsep_child(pack, packidx);
751 return request_packed_commit(commit, pack, idx, id);
754 static const struct got_error *
755 request_commit(struct got_commit_object **commit, struct got_repository *repo,
756 int fd, struct got_object_id *id)
758 const struct got_error *err = NULL;
759 struct imsgbuf *ibuf;
761 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
763 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
767 return got_privsep_recv_commit(commit, ibuf);
770 static const struct got_error *
771 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
772 struct got_object_id *id, struct got_repository *repo)
774 const struct got_error *err;
777 struct imsgbuf *ibuf;
779 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
780 return request_commit(commit, repo, obj_fd, id);
782 ibuf = calloc(1, sizeof(*ibuf));
784 return got_error_from_errno("calloc");
786 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
787 err = got_error_from_errno("socketpair");
794 err = got_error_from_errno("fork");
799 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
804 if (close(imsg_fds[1]) == -1) {
805 err = got_error_from_errno("close");
809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
812 imsg_init(ibuf, imsg_fds[0]);
813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
815 return request_commit(commit, repo, obj_fd, id);
819 static const struct got_error *
820 open_commit(struct got_commit_object **commit,
821 struct got_repository *repo, struct got_object_id *id, int check_cache)
823 const struct got_error *err = NULL;
824 struct got_packidx *packidx = NULL;
826 char *path_packfile = NULL;
829 *commit = got_repo_get_cached_commit(repo, id);
830 if (*commit != NULL) {
837 err = got_repo_search_packidx(&packidx, &idx, repo, id);
839 struct got_pack *pack = NULL;
841 err = got_packidx_get_packfile_path(&path_packfile,
842 packidx->path_packidx);
846 pack = got_repo_get_cached_pack(repo, path_packfile);
848 err = got_repo_cache_pack(&pack, repo, path_packfile,
853 err = read_packed_commit_privsep(commit, pack,
855 } else if (err->code == GOT_ERR_NO_OBJ) {
858 err = got_object_open_loose_fd(&fd, id, repo);
861 err = read_commit_privsep(commit, fd, id, repo);
866 err = got_repo_cache_commit(repo, id, *commit);
873 const struct got_error *
874 got_object_open_as_commit(struct got_commit_object **commit,
875 struct got_repository *repo, struct got_object_id *id)
877 *commit = got_repo_get_cached_commit(repo, id);
878 if (*commit != NULL) {
883 return open_commit(commit, repo, id, 0);
886 const struct got_error *
887 got_object_commit_open(struct got_commit_object **commit,
888 struct got_repository *repo, struct got_object *obj)
890 return open_commit(commit, repo, got_object_get_id(obj), 1);
893 const struct got_error *
894 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
896 const struct got_error *err = NULL;
898 *qid = calloc(1, sizeof(**qid));
900 return got_error_from_errno("calloc");
902 (*qid)->id = got_object_id_dup(id);
903 if ((*qid)->id == NULL) {
904 err = got_error_from_errno("got_object_id_dup");
905 got_object_qid_free(*qid);
913 const struct got_error *
914 got_object_id_queue_copy(const struct got_object_id_queue *src,
915 struct got_object_id_queue *dest)
917 const struct got_error *err;
918 struct got_object_qid *qid;
920 STAILQ_FOREACH(qid, src, entry) {
921 struct got_object_qid *new;
923 * Deep-copy the object ID only. Let the caller deal
924 * with setting up the new->data pointer if needed.
926 err = got_object_qid_alloc(&new, qid->id);
928 got_object_id_queue_free(dest);
931 STAILQ_INSERT_TAIL(dest, new, entry);
937 static const struct got_error *
938 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
939 int pack_idx, struct got_object_id *id)
941 const struct got_error *err = NULL;
943 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
948 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
951 static const struct got_error *
952 read_packed_tree_privsep(struct got_tree_object **tree,
953 struct got_pack *pack, struct got_packidx *packidx, int idx,
954 struct got_object_id *id)
956 const struct got_error *err = NULL;
958 if (pack->privsep_child)
959 return request_packed_tree(tree, pack, idx, id);
961 err = start_pack_privsep_child(pack, packidx);
965 return request_packed_tree(tree, pack, idx, id);
968 static const struct got_error *
969 request_tree(struct got_tree_object **tree, struct got_repository *repo,
970 int fd, struct got_object_id *id)
972 const struct got_error *err = NULL;
973 struct imsgbuf *ibuf;
975 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
977 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
981 return got_privsep_recv_tree(tree, ibuf);
984 const struct got_error *
985 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
986 struct got_object_id *id, struct got_repository *repo)
988 const struct got_error *err;
991 struct imsgbuf *ibuf;
993 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
994 return request_tree(tree, repo, obj_fd, id);
996 ibuf = calloc(1, sizeof(*ibuf));
998 return got_error_from_errno("calloc");
1000 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1001 err = got_error_from_errno("socketpair");
1008 err = got_error_from_errno("fork");
1012 else if (pid == 0) {
1013 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1018 if (close(imsg_fds[1]) == -1) {
1019 err = got_error_from_errno("close");
1023 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1025 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1026 imsg_init(ibuf, imsg_fds[0]);
1027 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1030 return request_tree(tree, repo, obj_fd, id);
1033 static const struct got_error *
1034 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1035 struct got_object_id *id, int check_cache)
1037 const struct got_error *err = NULL;
1038 struct got_packidx *packidx = NULL;
1040 char *path_packfile = NULL;
1043 *tree = got_repo_get_cached_tree(repo, id);
1044 if (*tree != NULL) {
1051 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1053 struct got_pack *pack = NULL;
1055 err = got_packidx_get_packfile_path(&path_packfile,
1056 packidx->path_packidx);
1060 pack = got_repo_get_cached_pack(repo, path_packfile);
1062 err = got_repo_cache_pack(&pack, repo, path_packfile,
1067 err = read_packed_tree_privsep(tree, pack,
1069 } else if (err->code == GOT_ERR_NO_OBJ) {
1072 err = got_object_open_loose_fd(&fd, id, repo);
1075 err = read_tree_privsep(tree, fd, id, repo);
1080 err = got_repo_cache_tree(repo, id, *tree);
1083 free(path_packfile);
1087 const struct got_error *
1088 got_object_open_as_tree(struct got_tree_object **tree,
1089 struct got_repository *repo, struct got_object_id *id)
1091 *tree = got_repo_get_cached_tree(repo, id);
1092 if (*tree != NULL) {
1097 return open_tree(tree, repo, id, 0);
1100 const struct got_error *
1101 got_object_tree_open(struct got_tree_object **tree,
1102 struct got_repository *repo, struct got_object *obj)
1104 return open_tree(tree, repo, got_object_get_id(obj), 1);
1108 got_object_tree_get_nentries(struct got_tree_object *tree)
1110 return tree->nentries;
1113 struct got_tree_entry *
1114 got_object_tree_get_first_entry(struct got_tree_object *tree)
1116 return got_object_tree_get_entry(tree, 0);
1119 struct got_tree_entry *
1120 got_object_tree_get_last_entry(struct got_tree_object *tree)
1122 return got_object_tree_get_entry(tree, tree->nentries - 1);
1125 struct got_tree_entry *
1126 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1128 if (i < 0 || i >= tree->nentries)
1130 return &tree->entries[i];
1134 got_tree_entry_get_mode(struct got_tree_entry *te)
1140 got_tree_entry_get_name(struct got_tree_entry *te)
1142 return &te->name[0];
1145 struct got_object_id *
1146 got_tree_entry_get_id(struct got_tree_entry *te)
1151 const struct got_error *
1152 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1154 const struct got_error *err = NULL;
1155 size_t len, totlen, hdrlen, offset;
1159 hdrlen = got_object_blob_get_hdrlen(blob);
1165 err = got_object_blob_read_block(&len, blob);
1172 totlen += len - hdrlen;
1173 p = realloc(*s, totlen + 1);
1175 err = got_error_from_errno("realloc");
1181 /* Skip blob object header first time around. */
1183 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1188 (*s)[totlen] = '\0';
1192 const struct got_error *
1193 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1194 struct got_repository *repo)
1196 const struct got_error *err = NULL;
1197 struct got_blob_object *blob = NULL;
1199 *link_target = NULL;
1201 if (!got_object_tree_entry_is_symlink(te))
1202 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1204 err = got_object_open_as_blob(&blob, repo,
1205 got_tree_entry_get_id(te), PATH_MAX);
1209 err = got_object_blob_read_to_str(link_target, blob);
1210 got_object_blob_close(blob);
1213 *link_target = NULL;
1219 got_tree_entry_get_index(struct got_tree_entry *te)
1224 struct got_tree_entry *
1225 got_tree_entry_get_next(struct got_tree_object *tree,
1226 struct got_tree_entry *te)
1228 return got_object_tree_get_entry(tree, te->idx + 1);
1231 struct got_tree_entry *
1232 got_tree_entry_get_prev(struct got_tree_object *tree,
1233 struct got_tree_entry *te)
1235 return got_object_tree_get_entry(tree, te->idx - 1);
1238 static const struct got_error *
1239 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1240 struct got_pack *pack, struct got_packidx *packidx, int idx,
1241 struct got_object_id *id)
1243 const struct got_error *err = NULL;
1245 int basefd, accumfd; /* temporary files for delta application */
1247 basefd = got_opentempfd();
1249 return got_error_from_errno("got_opentempfd");
1250 accumfd = got_opentempfd();
1252 return got_error_from_errno("got_opentempfd");
1254 outfd_child = dup(outfd);
1255 if (outfd_child == -1)
1256 return got_error_from_errno("dup");
1258 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1262 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1270 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1277 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1282 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1283 pack->privsep_child->ibuf);
1287 if (lseek(outfd, SEEK_SET, 0) == -1)
1288 err = got_error_from_errno("lseek");
1293 static const struct got_error *
1294 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1295 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1296 struct got_object_id *id)
1298 const struct got_error *err = NULL;
1300 if (pack->privsep_child == NULL) {
1301 err = start_pack_privsep_child(pack, packidx);
1306 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1310 static const struct got_error *
1311 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1312 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1314 const struct got_error *err = NULL;
1317 outfd_child = dup(outfd);
1318 if (outfd_child == -1)
1319 return got_error_from_errno("dup");
1321 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1325 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1329 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1333 if (lseek(outfd, SEEK_SET, 0) == -1)
1334 return got_error_from_errno("lseek");
1339 static const struct got_error *
1340 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1341 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1343 const struct got_error *err;
1346 struct imsgbuf *ibuf;
1348 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1349 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1350 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1354 ibuf = calloc(1, sizeof(*ibuf));
1356 return got_error_from_errno("calloc");
1358 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1359 err = got_error_from_errno("socketpair");
1366 err = got_error_from_errno("fork");
1370 else if (pid == 0) {
1371 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1376 if (close(imsg_fds[1]) == -1) {
1377 err = got_error_from_errno("close");
1381 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1384 imsg_init(ibuf, imsg_fds[0]);
1385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1387 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1390 static const struct got_error *
1391 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1392 struct got_object_id *id, size_t blocksize)
1394 const struct got_error *err = NULL;
1395 struct got_packidx *packidx = NULL;
1397 char *path_packfile = NULL;
1400 size_t size, hdrlen;
1403 *blob = calloc(1, sizeof(**blob));
1405 return got_error_from_errno("calloc");
1407 outfd = got_opentempfd();
1409 return got_error_from_errno("got_opentempfd");
1411 (*blob)->read_buf = malloc(blocksize);
1412 if ((*blob)->read_buf == NULL) {
1413 err = got_error_from_errno("malloc");
1417 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1419 struct got_pack *pack = NULL;
1421 err = got_packidx_get_packfile_path(&path_packfile,
1422 packidx->path_packidx);
1426 pack = got_repo_get_cached_pack(repo, path_packfile);
1428 err = got_repo_cache_pack(&pack, repo, path_packfile,
1433 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1434 pack, packidx, idx, id);
1435 } else if (err->code == GOT_ERR_NO_OBJ) {
1438 err = got_object_open_loose_fd(&infd, id, repo);
1441 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1447 if (hdrlen > size) {
1448 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1453 if (close(outfd) == -1 && err == NULL)
1454 err = got_error_from_errno("close");
1456 (*blob)->f = fmemopen(outbuf, size, "rb");
1457 if ((*blob)->f == NULL) {
1458 err = got_error_from_errno("fmemopen");
1462 (*blob)->data = outbuf;
1464 if (fstat(outfd, &sb) == -1) {
1465 err = got_error_from_errno("fstat");
1469 if (sb.st_size != size) {
1470 err = got_error(GOT_ERR_PRIVSEP_LEN);
1474 (*blob)->f = fdopen(outfd, "rb");
1475 if ((*blob)->f == NULL) {
1476 err = got_error_from_errno("fdopen");
1483 (*blob)->hdrlen = hdrlen;
1484 (*blob)->blocksize = blocksize;
1485 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1488 free(path_packfile);
1491 got_object_blob_close(*blob);
1493 } else if (outfd != -1)
1499 const struct got_error *
1500 got_object_open_as_blob(struct got_blob_object **blob,
1501 struct got_repository *repo, struct got_object_id *id,
1504 return open_blob(blob, repo, id, blocksize);
1507 const struct got_error *
1508 got_object_blob_open(struct got_blob_object **blob,
1509 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1511 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1514 const struct got_error *
1515 got_object_blob_close(struct got_blob_object *blob)
1517 const struct got_error *err = NULL;
1518 free(blob->read_buf);
1519 if (blob->f && fclose(blob->f) == EOF)
1520 err = got_error_from_errno("fclose");
1527 got_object_blob_rewind(struct got_blob_object *blob)
1534 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1536 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1540 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1542 return blob->hdrlen;
1546 got_object_blob_get_read_buf(struct got_blob_object *blob)
1548 return blob->read_buf;
1551 const struct got_error *
1552 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1556 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1557 if (n == 0 && ferror(blob->f))
1558 return got_ferror(blob->f, GOT_ERR_IO);
1563 const struct got_error *
1564 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1565 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1567 const struct got_error *err = NULL;
1568 size_t n, len, hdrlen;
1571 const int alloc_chunksz = 512;
1573 off_t off = 0, total_len = 0;
1576 *line_offsets = NULL;
1582 hdrlen = got_object_blob_get_hdrlen(blob);
1584 err = got_object_blob_read_block(&len, blob);
1589 buf = got_object_blob_get_read_buf(blob);
1592 if (line_offsets && *line_offsets == NULL) {
1593 /* Have some data but perhaps no '\n'. */
1595 nalloc = alloc_chunksz;
1596 *line_offsets = calloc(nalloc,
1597 sizeof(**line_offsets));
1598 if (*line_offsets == NULL)
1599 return got_error_from_errno("calloc");
1601 /* Skip forward over end of first line. */
1608 /* Scan '\n' offsets in remaining chunk of data. */
1610 if (buf[i] != '\n') {
1615 if (line_offsets && nalloc < *nlines) {
1616 size_t n = *nlines + alloc_chunksz;
1617 off_t *o = recallocarray(*line_offsets,
1618 nalloc, n, sizeof(**line_offsets));
1620 free(*line_offsets);
1621 *line_offsets = NULL;
1622 return got_error_from_errno(
1629 off = total_len + i - hdrlen + 1;
1630 (*line_offsets)[*nlines - 1] = off;
1635 /* Skip blob object header first time around. */
1636 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1637 if (n != len - hdrlen)
1638 return got_ferror(outfile, GOT_ERR_IO);
1639 total_len += len - hdrlen;
1643 if (fflush(outfile) != 0)
1644 return got_error_from_errno("fflush");
1648 *filesize = total_len;
1653 static const struct got_error *
1654 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1655 int pack_idx, struct got_object_id *id)
1657 const struct got_error *err = NULL;
1659 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1664 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1667 static const struct got_error *
1668 read_packed_tag_privsep(struct got_tag_object **tag,
1669 struct got_pack *pack, struct got_packidx *packidx, int idx,
1670 struct got_object_id *id)
1672 const struct got_error *err = NULL;
1674 if (pack->privsep_child)
1675 return request_packed_tag(tag, pack, idx, id);
1677 err = start_pack_privsep_child(pack, packidx);
1681 return request_packed_tag(tag, pack, idx, id);
1684 static const struct got_error *
1685 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1686 int fd, struct got_object_id *id)
1688 const struct got_error *err = NULL;
1689 struct imsgbuf *ibuf;
1691 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1693 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1697 return got_privsep_recv_tag(tag, ibuf);
1700 static const struct got_error *
1701 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1702 struct got_object_id *id, struct got_repository *repo)
1704 const struct got_error *err;
1707 struct imsgbuf *ibuf;
1709 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1710 return request_tag(tag, repo, obj_fd, id);
1712 ibuf = calloc(1, sizeof(*ibuf));
1714 return got_error_from_errno("calloc");
1716 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1717 err = got_error_from_errno("socketpair");
1724 err = got_error_from_errno("fork");
1728 else if (pid == 0) {
1729 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1734 if (close(imsg_fds[1]) == -1) {
1735 err = got_error_from_errno("close");
1739 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1741 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1742 imsg_init(ibuf, imsg_fds[0]);
1743 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1745 return request_tag(tag, repo, obj_fd, id);
1748 static const struct got_error *
1749 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1750 struct got_object_id *id, int check_cache)
1752 const struct got_error *err = NULL;
1753 struct got_packidx *packidx = NULL;
1755 char *path_packfile = NULL;
1756 struct got_object *obj = NULL;
1757 int obj_type = GOT_OBJ_TYPE_ANY;
1760 *tag = got_repo_get_cached_tag(repo, id);
1768 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1770 struct got_pack *pack = NULL;
1772 err = got_packidx_get_packfile_path(&path_packfile,
1773 packidx->path_packidx);
1777 pack = got_repo_get_cached_pack(repo, path_packfile);
1779 err = got_repo_cache_pack(&pack, repo, path_packfile,
1785 /* Beware of "lightweight" tags: Check object type first. */
1786 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1790 obj_type = obj->type;
1791 got_object_close(obj);
1792 if (obj_type != GOT_OBJ_TYPE_TAG) {
1793 err = got_error(GOT_ERR_OBJ_TYPE);
1796 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1797 } else if (err->code == GOT_ERR_NO_OBJ) {
1800 err = got_object_open_loose_fd(&fd, id, repo);
1803 err = got_object_read_header_privsep(&obj, id, repo, fd);
1806 obj_type = obj->type;
1807 got_object_close(obj);
1808 if (obj_type != GOT_OBJ_TYPE_TAG)
1809 return got_error(GOT_ERR_OBJ_TYPE);
1811 err = got_object_open_loose_fd(&fd, id, repo);
1814 err = read_tag_privsep(tag, fd, id, repo);
1819 err = got_repo_cache_tag(repo, id, *tag);
1822 free(path_packfile);
1826 const struct got_error *
1827 got_object_open_as_tag(struct got_tag_object **tag,
1828 struct got_repository *repo, struct got_object_id *id)
1830 *tag = got_repo_get_cached_tag(repo, id);
1836 return open_tag(tag, repo, id, 0);
1839 const struct got_error *
1840 got_object_tag_open(struct got_tag_object **tag,
1841 struct got_repository *repo, struct got_object *obj)
1843 return open_tag(tag, repo, got_object_get_id(obj), 1);
1847 got_object_tag_get_name(struct got_tag_object *tag)
1853 got_object_tag_get_object_type(struct got_tag_object *tag)
1855 return tag->obj_type;
1858 struct got_object_id *
1859 got_object_tag_get_object_id(struct got_tag_object *tag)
1865 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1867 return tag->tagger_time;
1871 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1873 return tag->tagger_gmtoff;
1877 got_object_tag_get_tagger(struct got_tag_object *tag)
1883 got_object_tag_get_message(struct got_tag_object *tag)
1888 static struct got_tree_entry *
1889 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1893 /* Note that tree entries are sorted in strncmp() order. */
1894 for (i = 0; i < tree->nentries; i++) {
1895 struct got_tree_entry *te = &tree->entries[i];
1896 int cmp = strncmp(te->name, name, len);
1901 if (te->name[len] == '\0')
1907 struct got_tree_entry *
1908 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1910 return find_entry_by_name(tree, name, strlen(name));
1913 const struct got_error *
1914 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1915 struct got_repository *repo, struct got_tree_object *tree,
1918 const struct got_error *err = NULL;
1919 struct got_tree_object *subtree = NULL;
1920 struct got_tree_entry *te = NULL;
1921 const char *seg, *s;
1933 struct got_tree_object *next_tree;
1942 te = find_entry_by_name(subtree, seg, seglen);
1944 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1955 err = got_object_open_as_tree(&next_tree, repo,
1960 if (subtree != tree)
1961 got_object_tree_close(subtree);
1962 subtree = next_tree;
1967 *id = got_object_id_dup(&te->id);
1969 return got_error_from_errno("got_object_id_dup");
1973 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1975 if (subtree && subtree != tree)
1976 got_object_tree_close(subtree);
1979 const struct got_error *
1980 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1981 struct got_object_id *commit_id, const char *path)
1983 const struct got_error *err = NULL;
1984 struct got_commit_object *commit = NULL;
1985 struct got_tree_object *tree = NULL;
1989 err = got_object_open_as_commit(&commit, repo, commit_id);
1993 /* Handle opening of root of commit's tree. */
1994 if (got_path_is_root_dir(path)) {
1995 *id = got_object_id_dup(commit->tree_id);
1997 err = got_error_from_errno("got_object_id_dup");
1999 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2002 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2006 got_object_commit_close(commit);
2008 got_object_tree_close(tree);
2013 * Normalize file mode bits to avoid false positive tree entry differences
2014 * in case tree entries have unexpected mode bits set.
2017 normalize_mode_for_comparison(mode_t mode)
2020 * For directories, the only relevant bit is the IFDIR bit.
2021 * This allows us to detect paths changing from a directory
2022 * to a file and vice versa.
2025 return mode & S_IFDIR;
2028 * For symlinks, the only relevant bit is the IFLNK bit.
2029 * This allows us to detect paths changing from a symlinks
2030 * to a file or directory and vice versa.
2033 return mode & S_IFLNK;
2035 /* For files, the only change we care about is the executable bit. */
2036 return mode & S_IXUSR;
2039 const struct got_error *
2040 got_object_tree_path_changed(int *changed,
2041 struct got_tree_object *tree01, struct got_tree_object *tree02,
2042 const char *path, struct got_repository *repo)
2044 const struct got_error *err = NULL;
2045 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2046 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2047 const char *seg, *s;
2052 /* We not do support comparing the root path. */
2053 if (got_path_is_root_dir(path))
2054 return got_error_path(path, GOT_ERR_BAD_PATH);
2064 struct got_tree_object *next_tree1, *next_tree2;
2065 mode_t mode1, mode2;
2074 te1 = find_entry_by_name(tree1, seg, seglen);
2076 err = got_error(GOT_ERR_NO_OBJ);
2081 te2 = find_entry_by_name(tree2, seg, seglen);
2084 mode1 = normalize_mode_for_comparison(te1->mode);
2085 mode2 = normalize_mode_for_comparison(te2->mode);
2086 if (mode1 != mode2) {
2091 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2097 if (*s == '\0') { /* final path element */
2106 err = got_object_open_as_tree(&next_tree1, repo,
2111 if (tree1 != tree01)
2112 got_object_tree_close(tree1);
2116 err = got_object_open_as_tree(&next_tree2, repo,
2121 if (tree2 != tree02)
2122 got_object_tree_close(tree2);
2125 if (tree2 != tree02)
2126 got_object_tree_close(tree2);
2132 if (tree1 && tree1 != tree01)
2133 got_object_tree_close(tree1);
2134 if (tree2 && tree2 != tree02)
2135 got_object_tree_close(tree2);
2139 const struct got_error *
2140 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2141 struct got_tree_entry *te)
2143 const struct got_error *err = NULL;
2145 *new_te = calloc(1, sizeof(**new_te));
2146 if (*new_te == NULL)
2147 return got_error_from_errno("calloc");
2149 (*new_te)->mode = te->mode;
2150 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2151 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2156 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2158 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2162 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2164 /* S_IFDIR check avoids confusing symlinks with submodules. */
2165 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2168 static const struct got_error *
2169 resolve_symlink(char **link_target, const char *path,
2170 struct got_object_id *commit_id, struct got_repository *repo)
2172 const struct got_error *err = NULL;
2174 char *name, *parent_path = NULL;
2175 struct got_object_id *tree_obj_id = NULL;
2176 struct got_tree_object *tree = NULL;
2177 struct got_tree_entry *te = NULL;
2179 *link_target = NULL;
2181 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2182 return got_error(GOT_ERR_NO_SPACE);
2184 name = basename(buf);
2186 return got_error_from_errno2("basename", path);
2188 err = got_path_dirname(&parent_path, path);
2192 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2195 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2196 /* Display the complete path in error message. */
2197 err = got_error_path(path, err->code);
2202 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2206 te = got_object_tree_find_entry(tree, name);
2208 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2212 if (got_object_tree_entry_is_symlink(te)) {
2213 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2216 if (!got_path_is_absolute(*link_target)) {
2218 if (asprintf(&abspath, "%s/%s", parent_path,
2219 *link_target) == -1) {
2220 err = got_error_from_errno("asprintf");
2224 *link_target = malloc(PATH_MAX);
2225 if (*link_target == NULL) {
2226 err = got_error_from_errno("malloc");
2229 err = got_canonpath(abspath, *link_target, PATH_MAX);
2238 got_object_tree_close(tree);
2241 *link_target = NULL;
2246 const struct got_error *
2247 got_object_resolve_symlinks(char **link_target, const char *path,
2248 struct got_object_id *commit_id, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 char *next_target = NULL;
2252 int max_recursion = 40; /* matches Git */
2254 *link_target = NULL;
2257 err = resolve_symlink(&next_target,
2258 *link_target ? *link_target : path, commit_id, repo);
2263 if (--max_recursion == 0) {
2264 err = got_error_path(path, GOT_ERR_RECURSION);
2265 *link_target = NULL;
2268 *link_target = next_target;
2270 } while (next_target);
2275 const struct got_error *
2276 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2277 struct got_object_id *commit_id, const char *path,
2278 struct got_repository *repo)
2280 const struct got_error *err = NULL;
2281 struct got_pack *pack = NULL;
2282 struct got_packidx *packidx = NULL;
2283 char *path_packfile = NULL;
2284 struct got_commit_object *changed_commit = NULL;
2285 struct got_object_id *changed_commit_id = NULL;
2288 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2290 if (err->code != GOT_ERR_NO_OBJ)
2295 err = got_packidx_get_packfile_path(&path_packfile,
2296 packidx->path_packidx);
2300 pack = got_repo_get_cached_pack(repo, path_packfile);
2302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2307 if (pack->privsep_child == NULL) {
2308 err = start_pack_privsep_child(pack, packidx);
2313 err = got_privsep_send_commit_traversal_request(
2314 pack->privsep_child->ibuf, commit_id, idx, path);
2318 err = got_privsep_recv_traversed_commits(&changed_commit,
2319 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2323 if (changed_commit) {
2325 * Cache the commit in which the path was changed.
2326 * This commit might be opened again soon.
2328 changed_commit->refcnt++;
2329 err = got_repo_cache_commit(repo, changed_commit_id,
2331 got_object_commit_close(changed_commit);
2334 free(path_packfile);
2335 free(changed_commit_id);