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>
21 #include <sys/socket.h>
23 #include <sys/resource.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_from_errno2("open", path);
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
159 err = got_privsep_recv_obj(obj, ibuf);
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
168 static const struct got_error *
169 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
170 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
172 const struct got_error *err = NULL;
173 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 int basefd, accumfd; /* temporary files for delta application */
177 basefd = got_opentempfd();
179 return got_error_from_errno("got_opentempfd");
181 accumfd = got_opentempfd();
184 return got_error_from_errno("got_opentempfd");
187 outfd_child = dup(outfd);
188 if (outfd_child == -1) {
189 err = got_error_from_errno("dup");
195 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
203 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
211 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
218 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
223 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
231 set_max_datasize(void)
235 if (getrlimit(RLIMIT_DATA, &rl) != 0)
238 rl.rlim_cur = rl.rlim_max;
239 setrlimit(RLIMIT_DATA, &rl);
242 static const struct got_error *
243 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
245 const struct got_error *err = NULL;
248 struct imsgbuf *ibuf;
250 ibuf = calloc(1, sizeof(*ibuf));
252 return got_error_from_errno("calloc");
254 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
255 if (pack->privsep_child == NULL) {
256 err = got_error_from_errno("calloc");
261 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
262 err = got_error_from_errno("socketpair");
268 err = got_error_from_errno("fork");
270 } else if (pid == 0) {
272 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
273 pack->path_packfile);
277 if (close(imsg_fds[1]) == -1)
278 return got_error_from_errno("close");
279 pack->privsep_child->imsg_fd = imsg_fds[0];
280 pack->privsep_child->pid = pid;
281 imsg_init(ibuf, imsg_fds[0]);
282 pack->privsep_child->ibuf = ibuf;
284 err = got_privsep_init_pack_child(ibuf, pack, packidx);
286 const struct got_error *child_err;
287 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
288 child_err = got_privsep_wait_for_child(
289 pack->privsep_child->pid);
290 if (child_err && err == NULL)
296 free(pack->privsep_child);
297 pack->privsep_child = NULL;
302 static const struct got_error *
303 read_packed_object_privsep(struct got_object **obj,
304 struct got_repository *repo, struct got_pack *pack,
305 struct got_packidx *packidx, int idx, struct got_object_id *id)
307 const struct got_error *err = NULL;
309 if (pack->privsep_child == NULL) {
310 err = start_pack_privsep_child(pack, packidx);
315 return request_packed_object(obj, pack, idx, id);
318 static const struct got_error *
319 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
320 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
321 struct got_object_id *id)
323 const struct got_error *err = NULL;
325 if (pack->privsep_child == NULL) {
326 err = start_pack_privsep_child(pack, packidx);
331 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
335 const struct got_error *
336 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
337 struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_pack *pack = NULL;
341 struct got_packidx *packidx = NULL;
345 err = got_repo_search_packidx(&packidx, &idx, repo, id);
349 err = got_packidx_get_packfile_path(&path_packfile,
350 packidx->path_packidx);
354 pack = got_repo_get_cached_pack(repo, path_packfile);
356 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
361 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
369 static const struct got_error *
370 request_object(struct got_object **obj, struct got_repository *repo, int fd)
372 const struct got_error *err = NULL;
373 struct imsgbuf *ibuf;
375 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
377 err = got_privsep_send_obj_req(ibuf, fd);
381 return got_privsep_recv_obj(obj, ibuf);
384 static const struct got_error *
385 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
386 struct got_repository *repo, int infd)
388 const struct got_error *err = NULL;
389 struct imsgbuf *ibuf;
392 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
394 outfd_child = dup(outfd);
395 if (outfd_child == -1)
396 return got_error_from_errno("dup");
398 err = got_privsep_send_raw_obj_req(ibuf, infd);
402 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
406 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
409 static const struct got_error *
410 start_read_object_child(struct got_repository *repo)
412 const struct got_error *err = NULL;
415 struct imsgbuf *ibuf;
417 ibuf = calloc(1, sizeof(*ibuf));
419 return got_error_from_errno("calloc");
421 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
422 err = got_error_from_errno("socketpair");
429 err = got_error_from_errno("fork");
434 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
439 if (close(imsg_fds[1]) == -1) {
440 err = got_error_from_errno("close");
445 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
448 imsg_init(ibuf, imsg_fds[0]);
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
454 const struct got_error *
455 got_object_read_header_privsep(struct got_object **obj,
456 struct got_repository *repo, int obj_fd)
458 const struct got_error *err;
460 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
461 return request_object(obj, repo, obj_fd);
463 err = start_read_object_child(repo);
469 return request_object(obj, repo, obj_fd);
472 static const struct got_error *
473 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
474 int outfd, struct got_repository *repo, int obj_fd)
476 const struct got_error *err;
478 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
479 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
482 err = start_read_object_child(repo);
486 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
489 const struct got_error *
490 got_object_open(struct got_object **obj, struct got_repository *repo,
491 struct got_object_id *id)
493 const struct got_error *err = NULL;
496 *obj = got_repo_get_cached_object(repo, id);
502 err = got_object_open_packed(obj, id, repo);
503 if (err && err->code != GOT_ERR_NO_OBJ)
507 return got_repo_cache_object(repo, id, *obj);
510 err = got_object_open_loose_fd(&fd, id, repo);
512 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
513 err = got_error_no_obj(id);
517 err = got_object_read_header_privsep(obj, repo, fd);
521 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
524 return got_repo_cache_object(repo, id, *obj);
527 const struct got_error *
528 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
529 struct got_object_id *id, size_t blocksize)
531 const struct got_error *err = NULL;
532 struct got_packidx *packidx = NULL;
534 uint8_t *outbuf = NULL;
538 char *path_packfile = NULL;
542 outfd = got_opentempfd();
544 return got_error_from_errno("got_opentempfd");
546 err = got_repo_search_packidx(&packidx, &idx, repo, id);
548 struct got_pack *pack = NULL;
550 err = got_packidx_get_packfile_path(&path_packfile,
551 packidx->path_packidx);
555 pack = got_repo_get_cached_pack(repo, path_packfile);
557 err = got_repo_cache_pack(&pack, repo, path_packfile,
562 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
563 outfd, pack, packidx, idx, id);
564 } else if (err->code == GOT_ERR_NO_OBJ) {
567 err = got_object_open_loose_fd(&fd, id, repo);
570 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
574 *obj = calloc(1, sizeof(**obj));
576 err = got_error_from_errno("calloc");
580 (*obj)->read_buf = malloc(blocksize);
581 if ((*obj)->read_buf == NULL) {
582 err = got_error_from_errno("malloc");
587 if (close(outfd) == -1) {
588 err = got_error_from_errno("close");
592 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
593 if ((*obj)->f == NULL) {
594 err = got_error_from_errno("fdopen");
597 (*obj)->data = outbuf;
600 if (fstat(outfd, &sb) == -1) {
601 err = got_error_from_errno("fstat");
605 if (sb.st_size != hdrlen + size) {
606 err = got_error(GOT_ERR_PRIVSEP_LEN);
610 (*obj)->f = fdopen(outfd, "r");
611 if ((*obj)->f == NULL) {
612 err = got_error_from_errno("fdopen");
618 (*obj)->hdrlen = hdrlen;
620 (*obj)->blocksize = blocksize;
625 got_object_raw_close(*obj);
636 got_object_raw_rewind(struct got_raw_object *obj)
643 got_object_raw_get_hdrlen(struct got_raw_object *obj)
649 got_object_raw_get_read_buf(struct got_raw_object *obj)
651 return obj->read_buf;
654 const struct got_error *
655 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
659 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
660 if (n == 0 && ferror(obj->f))
661 return got_ferror(obj->f, GOT_ERR_IO);
666 const struct got_error *
667 got_object_raw_close(struct got_raw_object *obj)
669 const struct got_error *err = NULL;
672 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
673 err = got_error_from_errno("fclose");
679 const struct got_error *
680 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
683 struct got_object_id id;
685 if (!got_parse_sha1_digest(id.sha1, id_str))
686 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
688 return got_object_open(obj, repo, &id);
691 const struct got_error *
692 got_object_resolve_id_str(struct got_object_id **id,
693 struct got_repository *repo, const char *id_str)
695 const struct got_error *err = NULL;
696 struct got_object *obj;
698 err = got_object_open_by_id_str(&obj, repo, id_str);
702 *id = got_object_id_dup(got_object_get_id(obj));
703 got_object_close(obj);
705 return got_error_from_errno("got_object_id_dup");
710 static const struct got_error *
711 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
712 int pack_idx, struct got_object_id *id)
714 const struct got_error *err = NULL;
716 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
721 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
725 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
729 static const struct got_error *
730 read_packed_commit_privsep(struct got_commit_object **commit,
731 struct got_pack *pack, struct got_packidx *packidx, int idx,
732 struct got_object_id *id)
734 const struct got_error *err = NULL;
736 if (pack->privsep_child)
737 return request_packed_commit(commit, pack, idx, id);
739 err = start_pack_privsep_child(pack, packidx);
743 return request_packed_commit(commit, pack, idx, id);
746 static const struct got_error *
747 request_commit(struct got_commit_object **commit, struct got_repository *repo,
750 const struct got_error *err = NULL;
751 struct imsgbuf *ibuf;
753 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
755 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
759 return got_privsep_recv_commit(commit, ibuf);
762 static const struct got_error *
763 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
764 struct got_repository *repo)
766 const struct got_error *err;
769 struct imsgbuf *ibuf;
771 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
772 return request_commit(commit, repo, obj_fd);
774 ibuf = calloc(1, sizeof(*ibuf));
776 return got_error_from_errno("calloc");
778 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
779 err = got_error_from_errno("socketpair");
786 err = got_error_from_errno("fork");
791 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
796 if (close(imsg_fds[1]) == -1) {
797 err = got_error_from_errno("close");
801 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
803 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
804 imsg_init(ibuf, imsg_fds[0]);
805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
807 return request_commit(commit, repo, obj_fd);
811 static const struct got_error *
812 open_commit(struct got_commit_object **commit,
813 struct got_repository *repo, struct got_object_id *id, int check_cache)
815 const struct got_error *err = NULL;
816 struct got_packidx *packidx = NULL;
818 char *path_packfile = NULL;
821 *commit = got_repo_get_cached_commit(repo, id);
822 if (*commit != NULL) {
829 err = got_repo_search_packidx(&packidx, &idx, repo, id);
831 struct got_pack *pack = NULL;
833 err = got_packidx_get_packfile_path(&path_packfile,
834 packidx->path_packidx);
838 pack = got_repo_get_cached_pack(repo, path_packfile);
840 err = got_repo_cache_pack(&pack, repo, path_packfile,
845 err = read_packed_commit_privsep(commit, pack,
847 } else if (err->code == GOT_ERR_NO_OBJ) {
850 err = got_object_open_loose_fd(&fd, id, repo);
853 err = read_commit_privsep(commit, fd, repo);
858 err = got_repo_cache_commit(repo, id, *commit);
865 const struct got_error *
866 got_object_open_as_commit(struct got_commit_object **commit,
867 struct got_repository *repo, struct got_object_id *id)
869 *commit = got_repo_get_cached_commit(repo, id);
870 if (*commit != NULL) {
875 return open_commit(commit, repo, id, 0);
878 const struct got_error *
879 got_object_commit_open(struct got_commit_object **commit,
880 struct got_repository *repo, struct got_object *obj)
882 return open_commit(commit, repo, got_object_get_id(obj), 1);
885 const struct got_error *
886 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
888 const struct got_error *err = NULL;
890 *qid = calloc(1, sizeof(**qid));
892 return got_error_from_errno("calloc");
894 (*qid)->id = got_object_id_dup(id);
895 if ((*qid)->id == NULL) {
896 err = got_error_from_errno("got_object_id_dup");
897 got_object_qid_free(*qid);
905 const struct got_error *
906 got_object_id_queue_copy(const struct got_object_id_queue *src,
907 struct got_object_id_queue *dest)
909 const struct got_error *err;
910 struct got_object_qid *qid;
912 STAILQ_FOREACH(qid, src, entry) {
913 struct got_object_qid *new;
915 * Deep-copy the object ID only. Let the caller deal
916 * with setting up the new->data pointer if needed.
918 err = got_object_qid_alloc(&new, qid->id);
920 got_object_id_queue_free(dest);
923 STAILQ_INSERT_TAIL(dest, new, entry);
929 static const struct got_error *
930 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
931 int pack_idx, struct got_object_id *id)
933 const struct got_error *err = NULL;
935 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
940 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
943 static const struct got_error *
944 read_packed_tree_privsep(struct got_tree_object **tree,
945 struct got_pack *pack, struct got_packidx *packidx, int idx,
946 struct got_object_id *id)
948 const struct got_error *err = NULL;
950 if (pack->privsep_child)
951 return request_packed_tree(tree, pack, idx, id);
953 err = start_pack_privsep_child(pack, packidx);
957 return request_packed_tree(tree, pack, idx, id);
960 static const struct got_error *
961 request_tree(struct got_tree_object **tree, struct got_repository *repo,
964 const struct got_error *err = NULL;
965 struct imsgbuf *ibuf;
967 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
969 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
973 return got_privsep_recv_tree(tree, ibuf);
976 const struct got_error *
977 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
978 struct got_repository *repo)
980 const struct got_error *err;
983 struct imsgbuf *ibuf;
985 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
986 return request_tree(tree, repo, obj_fd);
988 ibuf = calloc(1, sizeof(*ibuf));
990 return got_error_from_errno("calloc");
992 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
993 err = got_error_from_errno("socketpair");
1000 err = got_error_from_errno("fork");
1004 else if (pid == 0) {
1005 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1010 if (close(imsg_fds[1]) == -1) {
1011 err = got_error_from_errno("close");
1015 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1017 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1018 imsg_init(ibuf, imsg_fds[0]);
1019 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1022 return request_tree(tree, repo, obj_fd);
1025 static const struct got_error *
1026 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1027 struct got_object_id *id, int check_cache)
1029 const struct got_error *err = NULL;
1030 struct got_packidx *packidx = NULL;
1032 char *path_packfile = NULL;
1035 *tree = got_repo_get_cached_tree(repo, id);
1036 if (*tree != NULL) {
1043 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1045 struct got_pack *pack = NULL;
1047 err = got_packidx_get_packfile_path(&path_packfile,
1048 packidx->path_packidx);
1052 pack = got_repo_get_cached_pack(repo, path_packfile);
1054 err = got_repo_cache_pack(&pack, repo, path_packfile,
1059 err = read_packed_tree_privsep(tree, pack,
1061 } else if (err->code == GOT_ERR_NO_OBJ) {
1064 err = got_object_open_loose_fd(&fd, id, repo);
1067 err = read_tree_privsep(tree, fd, repo);
1072 err = got_repo_cache_tree(repo, id, *tree);
1075 free(path_packfile);
1079 const struct got_error *
1080 got_object_open_as_tree(struct got_tree_object **tree,
1081 struct got_repository *repo, struct got_object_id *id)
1083 *tree = got_repo_get_cached_tree(repo, id);
1084 if (*tree != NULL) {
1089 return open_tree(tree, repo, id, 0);
1092 const struct got_error *
1093 got_object_tree_open(struct got_tree_object **tree,
1094 struct got_repository *repo, struct got_object *obj)
1096 return open_tree(tree, repo, got_object_get_id(obj), 1);
1100 got_object_tree_get_nentries(struct got_tree_object *tree)
1102 return tree->nentries;
1105 struct got_tree_entry *
1106 got_object_tree_get_first_entry(struct got_tree_object *tree)
1108 return got_object_tree_get_entry(tree, 0);
1111 struct got_tree_entry *
1112 got_object_tree_get_last_entry(struct got_tree_object *tree)
1114 return got_object_tree_get_entry(tree, tree->nentries - 1);
1117 struct got_tree_entry *
1118 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1120 if (i < 0 || i >= tree->nentries)
1122 return &tree->entries[i];
1126 got_tree_entry_get_mode(struct got_tree_entry *te)
1132 got_tree_entry_get_name(struct got_tree_entry *te)
1134 return &te->name[0];
1137 struct got_object_id *
1138 got_tree_entry_get_id(struct got_tree_entry *te)
1143 const struct got_error *
1144 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1146 const struct got_error *err = NULL;
1147 size_t len, totlen, hdrlen, offset;
1151 hdrlen = got_object_blob_get_hdrlen(blob);
1157 err = got_object_blob_read_block(&len, blob);
1164 totlen += len - hdrlen;
1165 p = realloc(*s, totlen + 1);
1167 err = got_error_from_errno("realloc");
1173 /* Skip blob object header first time around. */
1175 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1180 (*s)[totlen] = '\0';
1184 const struct got_error *
1185 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1186 struct got_repository *repo)
1188 const struct got_error *err = NULL;
1189 struct got_blob_object *blob = NULL;
1191 *link_target = NULL;
1193 if (!got_object_tree_entry_is_symlink(te))
1194 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1196 err = got_object_open_as_blob(&blob, repo,
1197 got_tree_entry_get_id(te), PATH_MAX);
1201 err = got_object_blob_read_to_str(link_target, blob);
1202 got_object_blob_close(blob);
1205 *link_target = NULL;
1211 got_tree_entry_get_index(struct got_tree_entry *te)
1216 struct got_tree_entry *
1217 got_tree_entry_get_next(struct got_tree_object *tree,
1218 struct got_tree_entry *te)
1220 return got_object_tree_get_entry(tree, te->idx + 1);
1223 struct got_tree_entry *
1224 got_tree_entry_get_prev(struct got_tree_object *tree,
1225 struct got_tree_entry *te)
1227 return got_object_tree_get_entry(tree, te->idx - 1);
1230 static const struct got_error *
1231 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1232 struct got_pack *pack, struct got_packidx *packidx, int idx,
1233 struct got_object_id *id)
1235 const struct got_error *err = NULL;
1237 int basefd, accumfd; /* temporary files for delta application */
1239 basefd = got_opentempfd();
1241 return got_error_from_errno("got_opentempfd");
1242 accumfd = got_opentempfd();
1244 return got_error_from_errno("got_opentempfd");
1246 outfd_child = dup(outfd);
1247 if (outfd_child == -1)
1248 return got_error_from_errno("dup");
1250 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1254 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1262 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1269 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1274 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1275 pack->privsep_child->ibuf);
1279 if (lseek(outfd, SEEK_SET, 0) == -1)
1280 err = got_error_from_errno("lseek");
1285 static const struct got_error *
1286 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1287 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1288 struct got_object_id *id)
1290 const struct got_error *err = NULL;
1292 if (pack->privsep_child == NULL) {
1293 err = start_pack_privsep_child(pack, packidx);
1298 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1302 static const struct got_error *
1303 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1304 int infd, struct imsgbuf *ibuf)
1306 const struct got_error *err = NULL;
1309 outfd_child = dup(outfd);
1310 if (outfd_child == -1)
1311 return got_error_from_errno("dup");
1313 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1317 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1321 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1325 if (lseek(outfd, SEEK_SET, 0) == -1)
1326 return got_error_from_errno("lseek");
1331 static const struct got_error *
1332 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1333 int outfd, int infd, struct got_repository *repo)
1335 const struct got_error *err;
1338 struct imsgbuf *ibuf;
1340 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1341 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1342 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1345 ibuf = calloc(1, sizeof(*ibuf));
1347 return got_error_from_errno("calloc");
1349 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1350 err = got_error_from_errno("socketpair");
1357 err = got_error_from_errno("fork");
1361 else if (pid == 0) {
1362 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1367 if (close(imsg_fds[1]) == -1) {
1368 err = got_error_from_errno("close");
1372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1374 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1375 imsg_init(ibuf, imsg_fds[0]);
1376 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1378 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1381 static const struct got_error *
1382 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1383 struct got_object_id *id, size_t blocksize)
1385 const struct got_error *err = NULL;
1386 struct got_packidx *packidx = NULL;
1388 char *path_packfile = NULL;
1391 size_t size, hdrlen;
1394 *blob = calloc(1, sizeof(**blob));
1396 return got_error_from_errno("calloc");
1398 outfd = got_opentempfd();
1400 return got_error_from_errno("got_opentempfd");
1402 (*blob)->read_buf = malloc(blocksize);
1403 if ((*blob)->read_buf == NULL) {
1404 err = got_error_from_errno("malloc");
1408 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1410 struct got_pack *pack = NULL;
1412 err = got_packidx_get_packfile_path(&path_packfile,
1413 packidx->path_packidx);
1417 pack = got_repo_get_cached_pack(repo, path_packfile);
1419 err = got_repo_cache_pack(&pack, repo, path_packfile,
1424 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1425 pack, packidx, idx, id);
1426 } else if (err->code == GOT_ERR_NO_OBJ) {
1429 err = got_object_open_loose_fd(&infd, id, repo);
1432 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1438 if (hdrlen > size) {
1439 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1444 if (close(outfd) == -1 && err == NULL)
1445 err = got_error_from_errno("close");
1447 (*blob)->f = fmemopen(outbuf, size, "rb");
1448 if ((*blob)->f == NULL) {
1449 err = got_error_from_errno("fmemopen");
1453 (*blob)->data = outbuf;
1455 if (fstat(outfd, &sb) == -1) {
1456 err = got_error_from_errno("fstat");
1460 if (sb.st_size != size) {
1461 err = got_error(GOT_ERR_PRIVSEP_LEN);
1465 (*blob)->f = fdopen(outfd, "rb");
1466 if ((*blob)->f == NULL) {
1467 err = got_error_from_errno("fdopen");
1474 (*blob)->hdrlen = hdrlen;
1475 (*blob)->blocksize = blocksize;
1476 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1479 free(path_packfile);
1482 got_object_blob_close(*blob);
1484 } else if (outfd != -1)
1490 const struct got_error *
1491 got_object_open_as_blob(struct got_blob_object **blob,
1492 struct got_repository *repo, struct got_object_id *id,
1495 return open_blob(blob, repo, id, blocksize);
1498 const struct got_error *
1499 got_object_blob_open(struct got_blob_object **blob,
1500 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1502 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1505 const struct got_error *
1506 got_object_blob_close(struct got_blob_object *blob)
1508 const struct got_error *err = NULL;
1509 free(blob->read_buf);
1510 if (blob->f && fclose(blob->f) == EOF)
1511 err = got_error_from_errno("fclose");
1518 got_object_blob_rewind(struct got_blob_object *blob)
1525 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1527 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1531 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1533 return blob->hdrlen;
1537 got_object_blob_get_read_buf(struct got_blob_object *blob)
1539 return blob->read_buf;
1542 const struct got_error *
1543 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1547 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1548 if (n == 0 && ferror(blob->f))
1549 return got_ferror(blob->f, GOT_ERR_IO);
1554 const struct got_error *
1555 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1556 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1558 const struct got_error *err = NULL;
1559 size_t n, len, hdrlen;
1562 const int alloc_chunksz = 512;
1564 off_t off = 0, total_len = 0;
1567 *line_offsets = NULL;
1573 hdrlen = got_object_blob_get_hdrlen(blob);
1575 err = got_object_blob_read_block(&len, blob);
1580 buf = got_object_blob_get_read_buf(blob);
1583 if (line_offsets && *line_offsets == NULL) {
1584 /* Have some data but perhaps no '\n'. */
1586 nalloc = alloc_chunksz;
1587 *line_offsets = calloc(nalloc,
1588 sizeof(**line_offsets));
1589 if (*line_offsets == NULL)
1590 return got_error_from_errno("calloc");
1592 /* Skip forward over end of first line. */
1599 /* Scan '\n' offsets in remaining chunk of data. */
1601 if (buf[i] != '\n') {
1606 if (line_offsets && nalloc < *nlines) {
1607 size_t n = *nlines + alloc_chunksz;
1608 off_t *o = recallocarray(*line_offsets,
1609 nalloc, n, sizeof(**line_offsets));
1611 free(*line_offsets);
1612 *line_offsets = NULL;
1613 return got_error_from_errno(
1620 off = total_len + i - hdrlen + 1;
1621 (*line_offsets)[*nlines - 1] = off;
1626 /* Skip blob object header first time around. */
1627 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1628 if (n != len - hdrlen)
1629 return got_ferror(outfile, GOT_ERR_IO);
1630 total_len += len - hdrlen;
1634 if (fflush(outfile) != 0)
1635 return got_error_from_errno("fflush");
1639 *filesize = total_len;
1644 static const struct got_error *
1645 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1646 int pack_idx, struct got_object_id *id)
1648 const struct got_error *err = NULL;
1650 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1655 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1658 static const struct got_error *
1659 read_packed_tag_privsep(struct got_tag_object **tag,
1660 struct got_pack *pack, struct got_packidx *packidx, int idx,
1661 struct got_object_id *id)
1663 const struct got_error *err = NULL;
1665 if (pack->privsep_child)
1666 return request_packed_tag(tag, pack, idx, id);
1668 err = start_pack_privsep_child(pack, packidx);
1672 return request_packed_tag(tag, pack, idx, id);
1675 static const struct got_error *
1676 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1679 const struct got_error *err = NULL;
1680 struct imsgbuf *ibuf;
1682 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1684 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1688 return got_privsep_recv_tag(tag, ibuf);
1691 static const struct got_error *
1692 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1693 struct got_repository *repo)
1695 const struct got_error *err;
1698 struct imsgbuf *ibuf;
1700 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1701 return request_tag(tag, repo, obj_fd);
1703 ibuf = calloc(1, sizeof(*ibuf));
1705 return got_error_from_errno("calloc");
1707 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1708 err = got_error_from_errno("socketpair");
1715 err = got_error_from_errno("fork");
1719 else if (pid == 0) {
1720 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1725 if (close(imsg_fds[1]) == -1) {
1726 err = got_error_from_errno("close");
1730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1733 imsg_init(ibuf, imsg_fds[0]);
1734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1736 return request_tag(tag, repo, obj_fd);
1739 static const struct got_error *
1740 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1741 struct got_object_id *id, int check_cache)
1743 const struct got_error *err = NULL;
1744 struct got_packidx *packidx = NULL;
1746 char *path_packfile = NULL;
1747 struct got_object *obj = NULL;
1748 int obj_type = GOT_OBJ_TYPE_ANY;
1751 *tag = got_repo_get_cached_tag(repo, id);
1759 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1761 struct got_pack *pack = NULL;
1763 err = got_packidx_get_packfile_path(&path_packfile,
1764 packidx->path_packidx);
1768 pack = got_repo_get_cached_pack(repo, path_packfile);
1770 err = got_repo_cache_pack(&pack, repo, path_packfile,
1776 /* Beware of "lightweight" tags: Check object type first. */
1777 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1781 obj_type = obj->type;
1782 got_object_close(obj);
1783 if (obj_type != GOT_OBJ_TYPE_TAG) {
1784 err = got_error(GOT_ERR_OBJ_TYPE);
1787 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1788 } else if (err->code == GOT_ERR_NO_OBJ) {
1791 err = got_object_open_loose_fd(&fd, id, repo);
1794 err = got_object_read_header_privsep(&obj, repo, fd);
1797 obj_type = obj->type;
1798 got_object_close(obj);
1799 if (obj_type != GOT_OBJ_TYPE_TAG)
1800 return got_error(GOT_ERR_OBJ_TYPE);
1802 err = got_object_open_loose_fd(&fd, id, repo);
1805 err = read_tag_privsep(tag, fd, repo);
1810 err = got_repo_cache_tag(repo, id, *tag);
1813 free(path_packfile);
1817 const struct got_error *
1818 got_object_open_as_tag(struct got_tag_object **tag,
1819 struct got_repository *repo, struct got_object_id *id)
1821 *tag = got_repo_get_cached_tag(repo, id);
1827 return open_tag(tag, repo, id, 0);
1830 const struct got_error *
1831 got_object_tag_open(struct got_tag_object **tag,
1832 struct got_repository *repo, struct got_object *obj)
1834 return open_tag(tag, repo, got_object_get_id(obj), 1);
1838 got_object_tag_get_name(struct got_tag_object *tag)
1844 got_object_tag_get_object_type(struct got_tag_object *tag)
1846 return tag->obj_type;
1849 struct got_object_id *
1850 got_object_tag_get_object_id(struct got_tag_object *tag)
1856 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1858 return tag->tagger_time;
1862 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1864 return tag->tagger_gmtoff;
1868 got_object_tag_get_tagger(struct got_tag_object *tag)
1874 got_object_tag_get_message(struct got_tag_object *tag)
1879 static struct got_tree_entry *
1880 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1884 /* Note that tree entries are sorted in strncmp() order. */
1885 for (i = 0; i < tree->nentries; i++) {
1886 struct got_tree_entry *te = &tree->entries[i];
1887 int cmp = strncmp(te->name, name, len);
1892 if (te->name[len] == '\0')
1898 struct got_tree_entry *
1899 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1901 return find_entry_by_name(tree, name, strlen(name));
1904 const struct got_error *
1905 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1906 struct got_object_id *commit_id, const char *path)
1908 const struct got_error *err = NULL;
1909 struct got_commit_object *commit = NULL;
1910 struct got_tree_object *tree = NULL;
1911 struct got_tree_entry *te = NULL;
1912 const char *seg, *s;
1917 err = got_object_open_as_commit(&commit, repo, commit_id);
1921 /* Handle opening of root of commit's tree. */
1922 if (got_path_is_root_dir(path)) {
1923 *id = got_object_id_dup(commit->tree_id);
1925 err = got_error_from_errno("got_object_id_dup");
1929 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1939 struct got_tree_object *next_tree;
1948 te = find_entry_by_name(tree, seg, seglen);
1950 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1961 err = got_object_open_as_tree(&next_tree, repo,
1966 got_object_tree_close(tree);
1972 *id = got_object_id_dup(&te->id);
1974 return got_error_from_errno("got_object_id_dup");
1976 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1979 got_object_commit_close(commit);
1981 got_object_tree_close(tree);
1986 * Normalize file mode bits to avoid false positive tree entry differences
1987 * in case tree entries have unexpected mode bits set.
1990 normalize_mode_for_comparison(mode_t mode)
1993 * For directories, the only relevant bit is the IFDIR bit.
1994 * This allows us to detect paths changing from a directory
1995 * to a file and vice versa.
1998 return mode & S_IFDIR;
2001 * For symlinks, the only relevant bit is the IFLNK bit.
2002 * This allows us to detect paths changing from a symlinks
2003 * to a file or directory and vice versa.
2006 return mode & S_IFLNK;
2008 /* For files, the only change we care about is the executable bit. */
2009 return mode & S_IXUSR;
2012 const struct got_error *
2013 got_object_tree_path_changed(int *changed,
2014 struct got_tree_object *tree01, struct got_tree_object *tree02,
2015 const char *path, struct got_repository *repo)
2017 const struct got_error *err = NULL;
2018 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2019 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2020 const char *seg, *s;
2025 /* We not do support comparing the root path. */
2026 if (got_path_is_root_dir(path))
2027 return got_error_path(path, GOT_ERR_BAD_PATH);
2037 struct got_tree_object *next_tree1, *next_tree2;
2038 mode_t mode1, mode2;
2047 te1 = find_entry_by_name(tree1, seg, seglen);
2049 err = got_error(GOT_ERR_NO_OBJ);
2054 te2 = find_entry_by_name(tree2, seg, seglen);
2057 mode1 = normalize_mode_for_comparison(te1->mode);
2058 mode2 = normalize_mode_for_comparison(te2->mode);
2059 if (mode1 != mode2) {
2064 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2070 if (*s == '\0') { /* final path element */
2079 err = got_object_open_as_tree(&next_tree1, repo,
2084 if (tree1 != tree01)
2085 got_object_tree_close(tree1);
2089 err = got_object_open_as_tree(&next_tree2, repo,
2094 if (tree2 != tree02)
2095 got_object_tree_close(tree2);
2098 if (tree2 != tree02)
2099 got_object_tree_close(tree2);
2105 if (tree1 && tree1 != tree01)
2106 got_object_tree_close(tree1);
2107 if (tree2 && tree2 != tree02)
2108 got_object_tree_close(tree2);
2112 const struct got_error *
2113 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2114 struct got_tree_entry *te)
2116 const struct got_error *err = NULL;
2118 *new_te = calloc(1, sizeof(**new_te));
2119 if (*new_te == NULL)
2120 return got_error_from_errno("calloc");
2122 (*new_te)->mode = te->mode;
2123 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2124 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2129 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2131 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2135 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2137 /* S_IFDIR check avoids confusing symlinks with submodules. */
2138 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2141 static const struct got_error *
2142 resolve_symlink(char **link_target, const char *path,
2143 struct got_object_id *commit_id, struct got_repository *repo)
2145 const struct got_error *err = NULL;
2147 char *name, *parent_path = NULL;
2148 struct got_object_id *tree_obj_id = NULL;
2149 struct got_tree_object *tree = NULL;
2150 struct got_tree_entry *te = NULL;
2152 *link_target = NULL;
2154 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2155 return got_error(GOT_ERR_NO_SPACE);
2157 name = basename(buf);
2159 return got_error_from_errno2("basename", path);
2161 err = got_path_dirname(&parent_path, path);
2165 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2168 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2169 /* Display the complete path in error message. */
2170 err = got_error_path(path, err->code);
2175 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2179 te = got_object_tree_find_entry(tree, name);
2181 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2185 if (got_object_tree_entry_is_symlink(te)) {
2186 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2189 if (!got_path_is_absolute(*link_target)) {
2191 if (asprintf(&abspath, "%s/%s", parent_path,
2192 *link_target) == -1) {
2193 err = got_error_from_errno("asprintf");
2197 *link_target = malloc(PATH_MAX);
2198 if (*link_target == NULL) {
2199 err = got_error_from_errno("malloc");
2202 err = got_canonpath(abspath, *link_target, PATH_MAX);
2211 got_object_tree_close(tree);
2214 *link_target = NULL;
2219 const struct got_error *
2220 got_object_resolve_symlinks(char **link_target, const char *path,
2221 struct got_object_id *commit_id, struct got_repository *repo)
2223 const struct got_error *err = NULL;
2224 char *next_target = NULL;
2225 int max_recursion = 40; /* matches Git */
2227 *link_target = NULL;
2230 err = resolve_symlink(&next_target,
2231 *link_target ? *link_target : path, commit_id, repo);
2236 if (--max_recursion == 0) {
2237 err = got_error_path(path, GOT_ERR_RECURSION);
2238 *link_target = NULL;
2241 *link_target = next_target;
2243 } while (next_target);
2248 const struct got_error *
2249 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2250 struct got_object_id *commit_id, const char *path,
2251 struct got_repository *repo)
2253 const struct got_error *err = NULL;
2254 struct got_pack *pack = NULL;
2255 struct got_packidx *packidx = NULL;
2256 char *path_packfile = NULL;
2257 struct got_commit_object *changed_commit = NULL;
2258 struct got_object_id *changed_commit_id = NULL;
2261 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2263 if (err->code != GOT_ERR_NO_OBJ)
2268 err = got_packidx_get_packfile_path(&path_packfile,
2269 packidx->path_packidx);
2273 pack = got_repo_get_cached_pack(repo, path_packfile);
2275 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2280 if (pack->privsep_child == NULL) {
2281 err = start_pack_privsep_child(pack, packidx);
2286 err = got_privsep_send_commit_traversal_request(
2287 pack->privsep_child->ibuf, commit_id, idx, path);
2291 err = got_privsep_recv_traversed_commits(&changed_commit,
2292 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2296 if (changed_commit) {
2298 * Cache the commit in which the path was changed.
2299 * This commit might be opened again soon.
2301 changed_commit->refcnt++;
2302 err = got_repo_cache_commit(repo, changed_commit_id,
2304 got_object_commit_close(changed_commit);
2307 free(path_packfile);
2308 free(changed_commit_id);