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);
568 } else if (err->code == GOT_ERR_NO_OBJ) {
571 err = got_object_open_loose_fd(&fd, id, repo);
574 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
578 *obj = calloc(1, sizeof(**obj));
580 err = got_error_from_errno("calloc");
584 (*obj)->read_buf = malloc(blocksize);
585 if ((*obj)->read_buf == NULL) {
586 err = got_error_from_errno("malloc");
591 if (close(outfd) == -1) {
592 err = got_error_from_errno("close");
596 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
597 if ((*obj)->f == NULL) {
598 err = got_error_from_errno("fdopen");
601 (*obj)->data = outbuf;
604 if (fstat(outfd, &sb) == -1) {
605 err = got_error_from_errno("fstat");
609 if (sb.st_size != hdrlen + size) {
610 err = got_error(GOT_ERR_PRIVSEP_LEN);
614 (*obj)->f = fdopen(outfd, "r");
615 if ((*obj)->f == NULL) {
616 err = got_error_from_errno("fdopen");
622 (*obj)->hdrlen = hdrlen;
624 (*obj)->blocksize = blocksize;
629 got_object_raw_close(*obj);
640 got_object_raw_rewind(struct got_raw_object *obj)
647 got_object_raw_get_hdrlen(struct got_raw_object *obj)
653 got_object_raw_get_read_buf(struct got_raw_object *obj)
655 return obj->read_buf;
658 const struct got_error *
659 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
663 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
664 if (n == 0 && ferror(obj->f))
665 return got_ferror(obj->f, GOT_ERR_IO);
670 const struct got_error *
671 got_object_raw_close(struct got_raw_object *obj)
673 const struct got_error *err = NULL;
676 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
677 err = got_error_from_errno("fclose");
683 const struct got_error *
684 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
687 struct got_object_id id;
689 if (!got_parse_sha1_digest(id.sha1, id_str))
690 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
692 return got_object_open(obj, repo, &id);
695 const struct got_error *
696 got_object_resolve_id_str(struct got_object_id **id,
697 struct got_repository *repo, const char *id_str)
699 const struct got_error *err = NULL;
700 struct got_object *obj;
702 err = got_object_open_by_id_str(&obj, repo, id_str);
706 *id = got_object_id_dup(got_object_get_id(obj));
707 got_object_close(obj);
709 return got_error_from_errno("got_object_id_dup");
714 static const struct got_error *
715 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
716 int pack_idx, struct got_object_id *id)
718 const struct got_error *err = NULL;
720 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
725 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
729 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
733 static const struct got_error *
734 read_packed_commit_privsep(struct got_commit_object **commit,
735 struct got_pack *pack, struct got_packidx *packidx, int idx,
736 struct got_object_id *id)
738 const struct got_error *err = NULL;
740 if (pack->privsep_child)
741 return request_packed_commit(commit, pack, idx, id);
743 err = start_pack_privsep_child(pack, packidx);
747 return request_packed_commit(commit, pack, idx, id);
750 static const struct got_error *
751 request_commit(struct got_commit_object **commit, struct got_repository *repo,
752 int fd, struct got_object_id *id)
754 const struct got_error *err = NULL;
755 struct imsgbuf *ibuf;
757 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
759 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
763 return got_privsep_recv_commit(commit, ibuf);
766 static const struct got_error *
767 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
768 struct got_object_id *id, struct got_repository *repo)
770 const struct got_error *err;
773 struct imsgbuf *ibuf;
775 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
776 return request_commit(commit, repo, obj_fd, id);
778 ibuf = calloc(1, sizeof(*ibuf));
780 return got_error_from_errno("calloc");
782 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
783 err = got_error_from_errno("socketpair");
790 err = got_error_from_errno("fork");
795 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
800 if (close(imsg_fds[1]) == -1) {
801 err = got_error_from_errno("close");
805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
807 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
808 imsg_init(ibuf, imsg_fds[0]);
809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
811 return request_commit(commit, repo, obj_fd, id);
815 static const struct got_error *
816 open_commit(struct got_commit_object **commit,
817 struct got_repository *repo, struct got_object_id *id, int check_cache)
819 const struct got_error *err = NULL;
820 struct got_packidx *packidx = NULL;
822 char *path_packfile = NULL;
825 *commit = got_repo_get_cached_commit(repo, id);
826 if (*commit != NULL) {
833 err = got_repo_search_packidx(&packidx, &idx, repo, id);
835 struct got_pack *pack = NULL;
837 err = got_packidx_get_packfile_path(&path_packfile,
838 packidx->path_packidx);
842 pack = got_repo_get_cached_pack(repo, path_packfile);
844 err = got_repo_cache_pack(&pack, repo, path_packfile,
849 err = read_packed_commit_privsep(commit, pack,
851 } else if (err->code == GOT_ERR_NO_OBJ) {
854 err = got_object_open_loose_fd(&fd, id, repo);
857 err = read_commit_privsep(commit, fd, id, repo);
862 err = got_repo_cache_commit(repo, id, *commit);
869 const struct got_error *
870 got_object_open_as_commit(struct got_commit_object **commit,
871 struct got_repository *repo, struct got_object_id *id)
873 *commit = got_repo_get_cached_commit(repo, id);
874 if (*commit != NULL) {
879 return open_commit(commit, repo, id, 0);
882 const struct got_error *
883 got_object_commit_open(struct got_commit_object **commit,
884 struct got_repository *repo, struct got_object *obj)
886 return open_commit(commit, repo, got_object_get_id(obj), 1);
889 const struct got_error *
890 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
892 const struct got_error *err = NULL;
894 *qid = calloc(1, sizeof(**qid));
896 return got_error_from_errno("calloc");
898 (*qid)->id = got_object_id_dup(id);
899 if ((*qid)->id == NULL) {
900 err = got_error_from_errno("got_object_id_dup");
901 got_object_qid_free(*qid);
909 const struct got_error *
910 got_object_id_queue_copy(const struct got_object_id_queue *src,
911 struct got_object_id_queue *dest)
913 const struct got_error *err;
914 struct got_object_qid *qid;
916 STAILQ_FOREACH(qid, src, entry) {
917 struct got_object_qid *new;
919 * Deep-copy the object ID only. Let the caller deal
920 * with setting up the new->data pointer if needed.
922 err = got_object_qid_alloc(&new, qid->id);
924 got_object_id_queue_free(dest);
927 STAILQ_INSERT_TAIL(dest, new, entry);
933 static const struct got_error *
934 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
935 int pack_idx, struct got_object_id *id)
937 const struct got_error *err = NULL;
939 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
944 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
947 static const struct got_error *
948 read_packed_tree_privsep(struct got_tree_object **tree,
949 struct got_pack *pack, struct got_packidx *packidx, int idx,
950 struct got_object_id *id)
952 const struct got_error *err = NULL;
954 if (pack->privsep_child)
955 return request_packed_tree(tree, pack, idx, id);
957 err = start_pack_privsep_child(pack, packidx);
961 return request_packed_tree(tree, pack, idx, id);
964 static const struct got_error *
965 request_tree(struct got_tree_object **tree, struct got_repository *repo,
966 int fd, struct got_object_id *id)
968 const struct got_error *err = NULL;
969 struct imsgbuf *ibuf;
971 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
973 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
977 return got_privsep_recv_tree(tree, ibuf);
980 const struct got_error *
981 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
982 struct got_object_id *id, struct got_repository *repo)
984 const struct got_error *err;
987 struct imsgbuf *ibuf;
989 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
990 return request_tree(tree, repo, obj_fd, id);
992 ibuf = calloc(1, sizeof(*ibuf));
994 return got_error_from_errno("calloc");
996 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
997 err = got_error_from_errno("socketpair");
1004 err = got_error_from_errno("fork");
1008 else if (pid == 0) {
1009 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1014 if (close(imsg_fds[1]) == -1) {
1015 err = got_error_from_errno("close");
1019 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1021 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1022 imsg_init(ibuf, imsg_fds[0]);
1023 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1026 return request_tree(tree, repo, obj_fd, id);
1029 static const struct got_error *
1030 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1031 struct got_object_id *id, int check_cache)
1033 const struct got_error *err = NULL;
1034 struct got_packidx *packidx = NULL;
1036 char *path_packfile = NULL;
1039 *tree = got_repo_get_cached_tree(repo, id);
1040 if (*tree != NULL) {
1047 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1049 struct got_pack *pack = NULL;
1051 err = got_packidx_get_packfile_path(&path_packfile,
1052 packidx->path_packidx);
1056 pack = got_repo_get_cached_pack(repo, path_packfile);
1058 err = got_repo_cache_pack(&pack, repo, path_packfile,
1063 err = read_packed_tree_privsep(tree, pack,
1065 } else if (err->code == GOT_ERR_NO_OBJ) {
1068 err = got_object_open_loose_fd(&fd, id, repo);
1071 err = read_tree_privsep(tree, fd, id, repo);
1076 err = got_repo_cache_tree(repo, id, *tree);
1079 free(path_packfile);
1083 const struct got_error *
1084 got_object_open_as_tree(struct got_tree_object **tree,
1085 struct got_repository *repo, struct got_object_id *id)
1087 *tree = got_repo_get_cached_tree(repo, id);
1088 if (*tree != NULL) {
1093 return open_tree(tree, repo, id, 0);
1096 const struct got_error *
1097 got_object_tree_open(struct got_tree_object **tree,
1098 struct got_repository *repo, struct got_object *obj)
1100 return open_tree(tree, repo, got_object_get_id(obj), 1);
1104 got_object_tree_get_nentries(struct got_tree_object *tree)
1106 return tree->nentries;
1109 struct got_tree_entry *
1110 got_object_tree_get_first_entry(struct got_tree_object *tree)
1112 return got_object_tree_get_entry(tree, 0);
1115 struct got_tree_entry *
1116 got_object_tree_get_last_entry(struct got_tree_object *tree)
1118 return got_object_tree_get_entry(tree, tree->nentries - 1);
1121 struct got_tree_entry *
1122 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1124 if (i < 0 || i >= tree->nentries)
1126 return &tree->entries[i];
1130 got_tree_entry_get_mode(struct got_tree_entry *te)
1136 got_tree_entry_get_name(struct got_tree_entry *te)
1138 return &te->name[0];
1141 struct got_object_id *
1142 got_tree_entry_get_id(struct got_tree_entry *te)
1147 const struct got_error *
1148 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1150 const struct got_error *err = NULL;
1151 size_t len, totlen, hdrlen, offset;
1155 hdrlen = got_object_blob_get_hdrlen(blob);
1161 err = got_object_blob_read_block(&len, blob);
1168 totlen += len - hdrlen;
1169 p = realloc(*s, totlen + 1);
1171 err = got_error_from_errno("realloc");
1177 /* Skip blob object header first time around. */
1179 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1184 (*s)[totlen] = '\0';
1188 const struct got_error *
1189 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1190 struct got_repository *repo)
1192 const struct got_error *err = NULL;
1193 struct got_blob_object *blob = NULL;
1195 *link_target = NULL;
1197 if (!got_object_tree_entry_is_symlink(te))
1198 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1200 err = got_object_open_as_blob(&blob, repo,
1201 got_tree_entry_get_id(te), PATH_MAX);
1205 err = got_object_blob_read_to_str(link_target, blob);
1206 got_object_blob_close(blob);
1209 *link_target = NULL;
1215 got_tree_entry_get_index(struct got_tree_entry *te)
1220 struct got_tree_entry *
1221 got_tree_entry_get_next(struct got_tree_object *tree,
1222 struct got_tree_entry *te)
1224 return got_object_tree_get_entry(tree, te->idx + 1);
1227 struct got_tree_entry *
1228 got_tree_entry_get_prev(struct got_tree_object *tree,
1229 struct got_tree_entry *te)
1231 return got_object_tree_get_entry(tree, te->idx - 1);
1234 static const struct got_error *
1235 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1236 struct got_pack *pack, struct got_packidx *packidx, int idx,
1237 struct got_object_id *id)
1239 const struct got_error *err = NULL;
1241 int basefd, accumfd; /* temporary files for delta application */
1243 basefd = got_opentempfd();
1245 return got_error_from_errno("got_opentempfd");
1246 accumfd = got_opentempfd();
1248 return got_error_from_errno("got_opentempfd");
1250 outfd_child = dup(outfd);
1251 if (outfd_child == -1)
1252 return got_error_from_errno("dup");
1254 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1258 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1266 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1273 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1278 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1279 pack->privsep_child->ibuf);
1283 if (lseek(outfd, SEEK_SET, 0) == -1)
1284 err = got_error_from_errno("lseek");
1289 static const struct got_error *
1290 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1291 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1292 struct got_object_id *id)
1294 const struct got_error *err = NULL;
1296 if (pack->privsep_child == NULL) {
1297 err = start_pack_privsep_child(pack, packidx);
1302 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1306 static const struct got_error *
1307 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1308 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1310 const struct got_error *err = NULL;
1313 outfd_child = dup(outfd);
1314 if (outfd_child == -1)
1315 return got_error_from_errno("dup");
1317 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1321 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1325 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1329 if (lseek(outfd, SEEK_SET, 0) == -1)
1330 return got_error_from_errno("lseek");
1335 static const struct got_error *
1336 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1337 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1339 const struct got_error *err;
1342 struct imsgbuf *ibuf;
1344 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1345 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1346 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1350 ibuf = calloc(1, sizeof(*ibuf));
1352 return got_error_from_errno("calloc");
1354 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1355 err = got_error_from_errno("socketpair");
1362 err = got_error_from_errno("fork");
1366 else if (pid == 0) {
1367 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1372 if (close(imsg_fds[1]) == -1) {
1373 err = got_error_from_errno("close");
1377 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1379 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1380 imsg_init(ibuf, imsg_fds[0]);
1381 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1383 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1386 static const struct got_error *
1387 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1388 struct got_object_id *id, size_t blocksize)
1390 const struct got_error *err = NULL;
1391 struct got_packidx *packidx = NULL;
1393 char *path_packfile = NULL;
1396 size_t size, hdrlen;
1399 *blob = calloc(1, sizeof(**blob));
1401 return got_error_from_errno("calloc");
1403 outfd = got_opentempfd();
1405 return got_error_from_errno("got_opentempfd");
1407 (*blob)->read_buf = malloc(blocksize);
1408 if ((*blob)->read_buf == NULL) {
1409 err = got_error_from_errno("malloc");
1413 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1415 struct got_pack *pack = NULL;
1417 err = got_packidx_get_packfile_path(&path_packfile,
1418 packidx->path_packidx);
1422 pack = got_repo_get_cached_pack(repo, path_packfile);
1424 err = got_repo_cache_pack(&pack, repo, path_packfile,
1429 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1430 pack, packidx, idx, id);
1431 } else if (err->code == GOT_ERR_NO_OBJ) {
1434 err = got_object_open_loose_fd(&infd, id, repo);
1437 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1443 if (hdrlen > size) {
1444 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1449 if (close(outfd) == -1 && err == NULL)
1450 err = got_error_from_errno("close");
1452 (*blob)->f = fmemopen(outbuf, size, "rb");
1453 if ((*blob)->f == NULL) {
1454 err = got_error_from_errno("fmemopen");
1458 (*blob)->data = outbuf;
1460 if (fstat(outfd, &sb) == -1) {
1461 err = got_error_from_errno("fstat");
1465 if (sb.st_size != size) {
1466 err = got_error(GOT_ERR_PRIVSEP_LEN);
1470 (*blob)->f = fdopen(outfd, "rb");
1471 if ((*blob)->f == NULL) {
1472 err = got_error_from_errno("fdopen");
1479 (*blob)->hdrlen = hdrlen;
1480 (*blob)->blocksize = blocksize;
1481 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1484 free(path_packfile);
1487 got_object_blob_close(*blob);
1489 } else if (outfd != -1)
1495 const struct got_error *
1496 got_object_open_as_blob(struct got_blob_object **blob,
1497 struct got_repository *repo, struct got_object_id *id,
1500 return open_blob(blob, repo, id, blocksize);
1503 const struct got_error *
1504 got_object_blob_open(struct got_blob_object **blob,
1505 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1507 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1510 const struct got_error *
1511 got_object_blob_close(struct got_blob_object *blob)
1513 const struct got_error *err = NULL;
1514 free(blob->read_buf);
1515 if (blob->f && fclose(blob->f) == EOF)
1516 err = got_error_from_errno("fclose");
1523 got_object_blob_rewind(struct got_blob_object *blob)
1530 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1532 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1536 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1538 return blob->hdrlen;
1542 got_object_blob_get_read_buf(struct got_blob_object *blob)
1544 return blob->read_buf;
1547 const struct got_error *
1548 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1552 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1553 if (n == 0 && ferror(blob->f))
1554 return got_ferror(blob->f, GOT_ERR_IO);
1559 const struct got_error *
1560 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1561 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1563 const struct got_error *err = NULL;
1564 size_t n, len, hdrlen;
1567 const int alloc_chunksz = 512;
1569 off_t off = 0, total_len = 0;
1572 *line_offsets = NULL;
1578 hdrlen = got_object_blob_get_hdrlen(blob);
1580 err = got_object_blob_read_block(&len, blob);
1585 buf = got_object_blob_get_read_buf(blob);
1588 if (line_offsets && *line_offsets == NULL) {
1589 /* Have some data but perhaps no '\n'. */
1591 nalloc = alloc_chunksz;
1592 *line_offsets = calloc(nalloc,
1593 sizeof(**line_offsets));
1594 if (*line_offsets == NULL)
1595 return got_error_from_errno("calloc");
1597 /* Skip forward over end of first line. */
1604 /* Scan '\n' offsets in remaining chunk of data. */
1606 if (buf[i] != '\n') {
1611 if (line_offsets && nalloc < *nlines) {
1612 size_t n = *nlines + alloc_chunksz;
1613 off_t *o = recallocarray(*line_offsets,
1614 nalloc, n, sizeof(**line_offsets));
1616 free(*line_offsets);
1617 *line_offsets = NULL;
1618 return got_error_from_errno(
1625 off = total_len + i - hdrlen + 1;
1626 (*line_offsets)[*nlines - 1] = off;
1631 /* Skip blob object header first time around. */
1632 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1633 if (n != len - hdrlen)
1634 return got_ferror(outfile, GOT_ERR_IO);
1635 total_len += len - hdrlen;
1639 if (fflush(outfile) != 0)
1640 return got_error_from_errno("fflush");
1644 *filesize = total_len;
1649 static const struct got_error *
1650 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1651 int pack_idx, struct got_object_id *id)
1653 const struct got_error *err = NULL;
1655 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1660 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1663 static const struct got_error *
1664 read_packed_tag_privsep(struct got_tag_object **tag,
1665 struct got_pack *pack, struct got_packidx *packidx, int idx,
1666 struct got_object_id *id)
1668 const struct got_error *err = NULL;
1670 if (pack->privsep_child)
1671 return request_packed_tag(tag, pack, idx, id);
1673 err = start_pack_privsep_child(pack, packidx);
1677 return request_packed_tag(tag, pack, idx, id);
1680 static const struct got_error *
1681 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1682 int fd, struct got_object_id *id)
1684 const struct got_error *err = NULL;
1685 struct imsgbuf *ibuf;
1687 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1689 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1693 return got_privsep_recv_tag(tag, ibuf);
1696 static const struct got_error *
1697 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1698 struct got_object_id *id, struct got_repository *repo)
1700 const struct got_error *err;
1703 struct imsgbuf *ibuf;
1705 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1706 return request_tag(tag, repo, obj_fd, id);
1708 ibuf = calloc(1, sizeof(*ibuf));
1710 return got_error_from_errno("calloc");
1712 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1713 err = got_error_from_errno("socketpair");
1720 err = got_error_from_errno("fork");
1724 else if (pid == 0) {
1725 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1730 if (close(imsg_fds[1]) == -1) {
1731 err = got_error_from_errno("close");
1735 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1737 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1738 imsg_init(ibuf, imsg_fds[0]);
1739 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1741 return request_tag(tag, repo, obj_fd, id);
1744 static const struct got_error *
1745 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1746 struct got_object_id *id, int check_cache)
1748 const struct got_error *err = NULL;
1749 struct got_packidx *packidx = NULL;
1751 char *path_packfile = NULL;
1752 struct got_object *obj = NULL;
1753 int obj_type = GOT_OBJ_TYPE_ANY;
1756 *tag = got_repo_get_cached_tag(repo, id);
1764 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1766 struct got_pack *pack = NULL;
1768 err = got_packidx_get_packfile_path(&path_packfile,
1769 packidx->path_packidx);
1773 pack = got_repo_get_cached_pack(repo, path_packfile);
1775 err = got_repo_cache_pack(&pack, repo, path_packfile,
1781 /* Beware of "lightweight" tags: Check object type first. */
1782 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1786 obj_type = obj->type;
1787 got_object_close(obj);
1788 if (obj_type != GOT_OBJ_TYPE_TAG) {
1789 err = got_error(GOT_ERR_OBJ_TYPE);
1792 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1793 } else if (err->code == GOT_ERR_NO_OBJ) {
1796 err = got_object_open_loose_fd(&fd, id, repo);
1799 err = got_object_read_header_privsep(&obj, id, repo, fd);
1802 obj_type = obj->type;
1803 got_object_close(obj);
1804 if (obj_type != GOT_OBJ_TYPE_TAG)
1805 return got_error(GOT_ERR_OBJ_TYPE);
1807 err = got_object_open_loose_fd(&fd, id, repo);
1810 err = read_tag_privsep(tag, fd, id, repo);
1815 err = got_repo_cache_tag(repo, id, *tag);
1818 free(path_packfile);
1822 const struct got_error *
1823 got_object_open_as_tag(struct got_tag_object **tag,
1824 struct got_repository *repo, struct got_object_id *id)
1826 *tag = got_repo_get_cached_tag(repo, id);
1832 return open_tag(tag, repo, id, 0);
1835 const struct got_error *
1836 got_object_tag_open(struct got_tag_object **tag,
1837 struct got_repository *repo, struct got_object *obj)
1839 return open_tag(tag, repo, got_object_get_id(obj), 1);
1843 got_object_tag_get_name(struct got_tag_object *tag)
1849 got_object_tag_get_object_type(struct got_tag_object *tag)
1851 return tag->obj_type;
1854 struct got_object_id *
1855 got_object_tag_get_object_id(struct got_tag_object *tag)
1861 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1863 return tag->tagger_time;
1867 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1869 return tag->tagger_gmtoff;
1873 got_object_tag_get_tagger(struct got_tag_object *tag)
1879 got_object_tag_get_message(struct got_tag_object *tag)
1884 static struct got_tree_entry *
1885 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1889 /* Note that tree entries are sorted in strncmp() order. */
1890 for (i = 0; i < tree->nentries; i++) {
1891 struct got_tree_entry *te = &tree->entries[i];
1892 int cmp = strncmp(te->name, name, len);
1897 if (te->name[len] == '\0')
1903 struct got_tree_entry *
1904 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1906 return find_entry_by_name(tree, name, strlen(name));
1909 const struct got_error *
1910 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1911 struct got_repository *repo, struct got_tree_object *tree,
1914 const struct got_error *err = NULL;
1915 struct got_tree_object *subtree = NULL;
1916 struct got_tree_entry *te = NULL;
1917 const char *seg, *s;
1929 struct got_tree_object *next_tree;
1938 te = find_entry_by_name(subtree, seg, seglen);
1940 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1951 err = got_object_open_as_tree(&next_tree, repo,
1956 if (subtree != tree)
1957 got_object_tree_close(subtree);
1958 subtree = next_tree;
1963 *id = got_object_id_dup(&te->id);
1965 return got_error_from_errno("got_object_id_dup");
1969 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1971 if (subtree && subtree != tree)
1972 got_object_tree_close(subtree);
1975 const struct got_error *
1976 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1977 struct got_object_id *commit_id, const char *path)
1979 const struct got_error *err = NULL;
1980 struct got_commit_object *commit = NULL;
1981 struct got_tree_object *tree = NULL;
1985 err = got_object_open_as_commit(&commit, repo, commit_id);
1989 /* Handle opening of root of commit's tree. */
1990 if (got_path_is_root_dir(path)) {
1991 *id = got_object_id_dup(commit->tree_id);
1993 err = got_error_from_errno("got_object_id_dup");
1995 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1998 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2002 got_object_commit_close(commit);
2004 got_object_tree_close(tree);
2009 * Normalize file mode bits to avoid false positive tree entry differences
2010 * in case tree entries have unexpected mode bits set.
2013 normalize_mode_for_comparison(mode_t mode)
2016 * For directories, the only relevant bit is the IFDIR bit.
2017 * This allows us to detect paths changing from a directory
2018 * to a file and vice versa.
2021 return mode & S_IFDIR;
2024 * For symlinks, the only relevant bit is the IFLNK bit.
2025 * This allows us to detect paths changing from a symlinks
2026 * to a file or directory and vice versa.
2029 return mode & S_IFLNK;
2031 /* For files, the only change we care about is the executable bit. */
2032 return mode & S_IXUSR;
2035 const struct got_error *
2036 got_object_tree_path_changed(int *changed,
2037 struct got_tree_object *tree01, struct got_tree_object *tree02,
2038 const char *path, struct got_repository *repo)
2040 const struct got_error *err = NULL;
2041 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2042 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2043 const char *seg, *s;
2048 /* We not do support comparing the root path. */
2049 if (got_path_is_root_dir(path))
2050 return got_error_path(path, GOT_ERR_BAD_PATH);
2060 struct got_tree_object *next_tree1, *next_tree2;
2061 mode_t mode1, mode2;
2070 te1 = find_entry_by_name(tree1, seg, seglen);
2072 err = got_error(GOT_ERR_NO_OBJ);
2077 te2 = find_entry_by_name(tree2, seg, seglen);
2080 mode1 = normalize_mode_for_comparison(te1->mode);
2081 mode2 = normalize_mode_for_comparison(te2->mode);
2082 if (mode1 != mode2) {
2087 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2093 if (*s == '\0') { /* final path element */
2102 err = got_object_open_as_tree(&next_tree1, repo,
2107 if (tree1 != tree01)
2108 got_object_tree_close(tree1);
2112 err = got_object_open_as_tree(&next_tree2, repo,
2117 if (tree2 != tree02)
2118 got_object_tree_close(tree2);
2121 if (tree2 != tree02)
2122 got_object_tree_close(tree2);
2128 if (tree1 && tree1 != tree01)
2129 got_object_tree_close(tree1);
2130 if (tree2 && tree2 != tree02)
2131 got_object_tree_close(tree2);
2135 const struct got_error *
2136 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2137 struct got_tree_entry *te)
2139 const struct got_error *err = NULL;
2141 *new_te = calloc(1, sizeof(**new_te));
2142 if (*new_te == NULL)
2143 return got_error_from_errno("calloc");
2145 (*new_te)->mode = te->mode;
2146 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2147 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2152 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2154 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2158 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2160 /* S_IFDIR check avoids confusing symlinks with submodules. */
2161 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2164 static const struct got_error *
2165 resolve_symlink(char **link_target, const char *path,
2166 struct got_object_id *commit_id, struct got_repository *repo)
2168 const struct got_error *err = NULL;
2170 char *name, *parent_path = NULL;
2171 struct got_object_id *tree_obj_id = NULL;
2172 struct got_tree_object *tree = NULL;
2173 struct got_tree_entry *te = NULL;
2175 *link_target = NULL;
2177 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2178 return got_error(GOT_ERR_NO_SPACE);
2180 name = basename(buf);
2182 return got_error_from_errno2("basename", path);
2184 err = got_path_dirname(&parent_path, path);
2188 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2191 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2192 /* Display the complete path in error message. */
2193 err = got_error_path(path, err->code);
2198 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2202 te = got_object_tree_find_entry(tree, name);
2204 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2208 if (got_object_tree_entry_is_symlink(te)) {
2209 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2212 if (!got_path_is_absolute(*link_target)) {
2214 if (asprintf(&abspath, "%s/%s", parent_path,
2215 *link_target) == -1) {
2216 err = got_error_from_errno("asprintf");
2220 *link_target = malloc(PATH_MAX);
2221 if (*link_target == NULL) {
2222 err = got_error_from_errno("malloc");
2225 err = got_canonpath(abspath, *link_target, PATH_MAX);
2234 got_object_tree_close(tree);
2237 *link_target = NULL;
2242 const struct got_error *
2243 got_object_resolve_symlinks(char **link_target, const char *path,
2244 struct got_object_id *commit_id, struct got_repository *repo)
2246 const struct got_error *err = NULL;
2247 char *next_target = NULL;
2248 int max_recursion = 40; /* matches Git */
2250 *link_target = NULL;
2253 err = resolve_symlink(&next_target,
2254 *link_target ? *link_target : path, commit_id, repo);
2259 if (--max_recursion == 0) {
2260 err = got_error_path(path, GOT_ERR_RECURSION);
2261 *link_target = NULL;
2264 *link_target = next_target;
2266 } while (next_target);
2271 const struct got_error *
2272 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2273 struct got_object_id *commit_id, const char *path,
2274 struct got_repository *repo)
2276 const struct got_error *err = NULL;
2277 struct got_pack *pack = NULL;
2278 struct got_packidx *packidx = NULL;
2279 char *path_packfile = NULL;
2280 struct got_commit_object *changed_commit = NULL;
2281 struct got_object_id *changed_commit_id = NULL;
2284 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2286 if (err->code != GOT_ERR_NO_OBJ)
2291 err = got_packidx_get_packfile_path(&path_packfile,
2292 packidx->path_packidx);
2296 pack = got_repo_get_cached_pack(repo, path_packfile);
2298 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2303 if (pack->privsep_child == NULL) {
2304 err = start_pack_privsep_child(pack, packidx);
2309 err = got_privsep_send_commit_traversal_request(
2310 pack->privsep_child->ibuf, commit_id, idx, path);
2314 err = got_privsep_recv_traversed_commits(&changed_commit,
2315 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2319 if (changed_commit) {
2321 * Cache the commit in which the path was changed.
2322 * This commit might be opened again soon.
2324 changed_commit->refcnt++;
2325 err = got_repo_cache_commit(repo, changed_commit_id,
2327 got_object_commit_close(changed_commit);
2330 free(path_packfile);
2331 free(changed_commit_id);