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>
20 #include <sys/socket.h>
22 #include <sys/resource.h>
38 #include "got_compat.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 | O_CLOEXEC);
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 /* Create temporary files used during delta application. */
169 static const struct got_error *
170 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
172 const struct got_error *err;
176 * For performance reasons, the child will keep reusing the
177 * same temporary files during every object request.
178 * Opening and closing new files for every object request is
179 * too expensive during operations such as 'gotadmin pack'.
181 if (pack->child_has_tempfiles)
184 basefd = got_opentempfd();
186 return got_error_from_errno("got_opentempfd");
188 err = got_privsep_send_tmpfd(ibuf, basefd);
192 accumfd = got_opentempfd();
194 return got_error_from_errno("got_opentempfd");
196 err = got_privsep_send_tmpfd(ibuf, accumfd);
200 pack->child_has_tempfiles = 1;
204 static const struct got_error *
205 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
206 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
212 err = pack_child_send_tempfiles(ibuf, pack);
216 outfd_child = dup(outfd);
217 if (outfd_child == -1)
218 return got_error_from_errno("dup");
220 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
226 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
230 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
238 set_max_datasize(void)
242 if (getrlimit(RLIMIT_DATA, &rl) != 0)
245 rl.rlim_cur = rl.rlim_max;
246 setrlimit(RLIMIT_DATA, &rl);
249 static const struct got_error *
250 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
252 const struct got_error *err = NULL;
255 struct imsgbuf *ibuf;
257 ibuf = calloc(1, sizeof(*ibuf));
259 return got_error_from_errno("calloc");
261 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
262 if (pack->privsep_child == NULL) {
263 err = got_error_from_errno("calloc");
267 pack->child_has_tempfiles = 0;
269 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
270 err = got_error_from_errno("socketpair");
276 err = got_error_from_errno("fork");
278 } else if (pid == 0) {
280 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
281 pack->path_packfile);
285 if (close(imsg_fds[1]) == -1)
286 return got_error_from_errno("close");
287 pack->privsep_child->imsg_fd = imsg_fds[0];
288 pack->privsep_child->pid = pid;
289 imsg_init(ibuf, imsg_fds[0]);
290 pack->privsep_child->ibuf = ibuf;
292 err = got_privsep_init_pack_child(ibuf, pack, packidx);
294 const struct got_error *child_err;
295 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
296 child_err = got_privsep_wait_for_child(
297 pack->privsep_child->pid);
298 if (child_err && err == NULL)
304 free(pack->privsep_child);
305 pack->privsep_child = NULL;
310 static const struct got_error *
311 read_packed_object_privsep(struct got_object **obj,
312 struct got_repository *repo, struct got_pack *pack,
313 struct got_packidx *packidx, int idx, struct got_object_id *id)
315 const struct got_error *err = NULL;
317 if (pack->privsep_child == NULL) {
318 err = start_pack_privsep_child(pack, packidx);
323 return request_packed_object(obj, pack, idx, id);
326 static const struct got_error *
327 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
328 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
329 struct got_object_id *id)
331 const struct got_error *err = NULL;
333 if (pack->privsep_child == NULL) {
334 err = start_pack_privsep_child(pack, packidx);
339 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
343 const struct got_error *
344 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
345 struct got_repository *repo)
347 const struct got_error *err = NULL;
348 struct got_pack *pack = NULL;
349 struct got_packidx *packidx = NULL;
353 err = got_repo_search_packidx(&packidx, &idx, repo, id);
357 err = got_packidx_get_packfile_path(&path_packfile,
358 packidx->path_packidx);
362 pack = got_repo_get_cached_pack(repo, path_packfile);
364 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
369 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
377 static const struct got_error *
378 request_object(struct got_object **obj, struct got_object_id *id,
379 struct got_repository *repo, int fd)
381 const struct got_error *err = NULL;
382 struct imsgbuf *ibuf;
384 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
386 err = got_privsep_send_obj_req(ibuf, fd, id);
390 return got_privsep_recv_obj(obj, ibuf);
393 static const struct got_error *
394 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
395 struct got_object_id *id, struct got_repository *repo, int infd)
397 const struct got_error *err = NULL;
398 struct imsgbuf *ibuf;
401 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
403 outfd_child = dup(outfd);
404 if (outfd_child == -1)
405 return got_error_from_errno("dup");
407 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
411 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
415 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
418 static const struct got_error *
419 start_read_object_child(struct got_repository *repo)
421 const struct got_error *err = NULL;
424 struct imsgbuf *ibuf;
426 ibuf = calloc(1, sizeof(*ibuf));
428 return got_error_from_errno("calloc");
430 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
431 err = got_error_from_errno("socketpair");
438 err = got_error_from_errno("fork");
443 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
448 if (close(imsg_fds[1]) == -1) {
449 err = got_error_from_errno("close");
454 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
456 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
457 imsg_init(ibuf, imsg_fds[0]);
458 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
463 const struct got_error *
464 got_object_read_header_privsep(struct got_object **obj,
465 struct got_object_id *id, struct got_repository *repo, int obj_fd)
467 const struct got_error *err;
469 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
470 return request_object(obj, id, repo, obj_fd);
472 err = start_read_object_child(repo);
478 return request_object(obj, id, repo, obj_fd);
481 static const struct got_error *
482 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
483 int outfd, struct got_object_id *id, struct got_repository *repo,
486 const struct got_error *err;
488 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
489 return request_raw_object(outbuf, size, hdrlen, outfd, id,
492 err = start_read_object_child(repo);
496 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
500 const struct got_error *
501 got_object_open(struct got_object **obj, struct got_repository *repo,
502 struct got_object_id *id)
504 const struct got_error *err = NULL;
507 *obj = got_repo_get_cached_object(repo, id);
513 err = got_object_open_packed(obj, id, repo);
514 if (err && err->code != GOT_ERR_NO_OBJ)
518 return got_repo_cache_object(repo, id, *obj);
521 err = got_object_open_loose_fd(&fd, id, repo);
523 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
524 err = got_error_no_obj(id);
528 err = got_object_read_header_privsep(obj, id, repo, fd);
532 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
535 return got_repo_cache_object(repo, id, *obj);
538 /* *outfd must be initialized to -1 by caller */
539 const struct got_error *
540 got_object_raw_open(struct got_raw_object **obj, int *outfd,
541 struct got_repository *repo, struct got_object_id *id)
543 const struct got_error *err = NULL;
544 struct got_packidx *packidx = NULL;
546 uint8_t *outbuf = NULL;
549 char *path_packfile = NULL;
551 *obj = got_repo_get_cached_raw_object(repo, id);
558 *outfd = got_opentempfd();
560 return got_error_from_errno("got_opentempfd");
563 err = got_repo_search_packidx(&packidx, &idx, repo, id);
565 struct got_pack *pack = NULL;
567 err = got_packidx_get_packfile_path(&path_packfile,
568 packidx->path_packidx);
572 pack = got_repo_get_cached_pack(repo, path_packfile);
574 err = got_repo_cache_pack(&pack, repo, path_packfile,
579 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
580 *outfd, pack, packidx, idx, id);
583 } else if (err->code == GOT_ERR_NO_OBJ) {
586 err = got_object_open_loose_fd(&fd, id, repo);
589 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
595 *obj = calloc(1, sizeof(**obj));
597 err = got_error_from_errno("calloc");
603 (*obj)->data = outbuf;
606 if (fstat(*outfd, &sb) == -1) {
607 err = got_error_from_errno("fstat");
611 if (sb.st_size != hdrlen + size) {
612 err = got_error(GOT_ERR_PRIVSEP_LEN);
615 #ifndef GOT_PACK_NO_MMAP
616 if (hdrlen + size > 0) {
617 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
618 MAP_PRIVATE, *outfd, 0);
619 if ((*obj)->data == MAP_FAILED) {
620 if (errno != ENOMEM) {
621 err = got_error_from_errno("mmap");
632 (*obj)->f = fdopen(*outfd, "r");
633 if ((*obj)->f == NULL) {
634 err = got_error_from_errno("fdopen");
640 (*obj)->hdrlen = hdrlen;
642 err = got_repo_cache_raw_object(repo, id, *obj);
647 got_object_raw_close(*obj);
656 const struct got_error *
657 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
660 struct got_object_id id;
662 if (!got_parse_sha1_digest(id.sha1, id_str))
663 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
665 return got_object_open(obj, repo, &id);
668 const struct got_error *
669 got_object_resolve_id_str(struct got_object_id **id,
670 struct got_repository *repo, const char *id_str)
672 const struct got_error *err = NULL;
673 struct got_object *obj;
675 err = got_object_open_by_id_str(&obj, repo, id_str);
679 *id = got_object_id_dup(got_object_get_id(obj));
680 got_object_close(obj);
682 return got_error_from_errno("got_object_id_dup");
687 static const struct got_error *
688 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
689 int pack_idx, struct got_object_id *id)
691 const struct got_error *err = NULL;
693 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
698 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
702 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
706 static const struct got_error *
707 read_packed_commit_privsep(struct got_commit_object **commit,
708 struct got_pack *pack, struct got_packidx *packidx, int idx,
709 struct got_object_id *id)
711 const struct got_error *err = NULL;
713 if (pack->privsep_child)
714 return request_packed_commit(commit, pack, idx, id);
716 err = start_pack_privsep_child(pack, packidx);
720 return request_packed_commit(commit, pack, idx, id);
723 static const struct got_error *
724 request_commit(struct got_commit_object **commit, struct got_repository *repo,
725 int fd, struct got_object_id *id)
727 const struct got_error *err = NULL;
728 struct imsgbuf *ibuf;
730 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
732 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
736 return got_privsep_recv_commit(commit, ibuf);
739 static const struct got_error *
740 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
741 struct got_object_id *id, struct got_repository *repo)
743 const struct got_error *err;
746 struct imsgbuf *ibuf;
748 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
749 return request_commit(commit, repo, obj_fd, id);
751 ibuf = calloc(1, sizeof(*ibuf));
753 return got_error_from_errno("calloc");
755 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
756 err = got_error_from_errno("socketpair");
763 err = got_error_from_errno("fork");
768 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
773 if (close(imsg_fds[1]) == -1) {
774 err = got_error_from_errno("close");
778 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
780 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
781 imsg_init(ibuf, imsg_fds[0]);
782 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
784 return request_commit(commit, repo, obj_fd, id);
788 static const struct got_error *
789 open_commit(struct got_commit_object **commit,
790 struct got_repository *repo, struct got_object_id *id, int check_cache)
792 const struct got_error *err = NULL;
793 struct got_packidx *packidx = NULL;
795 char *path_packfile = NULL;
798 *commit = got_repo_get_cached_commit(repo, id);
799 if (*commit != NULL) {
806 err = got_repo_search_packidx(&packidx, &idx, repo, id);
808 struct got_pack *pack = NULL;
810 err = got_packidx_get_packfile_path(&path_packfile,
811 packidx->path_packidx);
815 pack = got_repo_get_cached_pack(repo, path_packfile);
817 err = got_repo_cache_pack(&pack, repo, path_packfile,
822 err = read_packed_commit_privsep(commit, pack,
824 } else if (err->code == GOT_ERR_NO_OBJ) {
827 err = got_object_open_loose_fd(&fd, id, repo);
830 err = read_commit_privsep(commit, fd, id, repo);
835 err = got_repo_cache_commit(repo, id, *commit);
842 const struct got_error *
843 got_object_open_as_commit(struct got_commit_object **commit,
844 struct got_repository *repo, struct got_object_id *id)
846 *commit = got_repo_get_cached_commit(repo, id);
847 if (*commit != NULL) {
852 return open_commit(commit, repo, id, 0);
855 const struct got_error *
856 got_object_commit_open(struct got_commit_object **commit,
857 struct got_repository *repo, struct got_object *obj)
859 return open_commit(commit, repo, got_object_get_id(obj), 1);
862 const struct got_error *
863 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
865 const struct got_error *err = NULL;
867 *qid = calloc(1, sizeof(**qid));
869 return got_error_from_errno("calloc");
871 (*qid)->id = got_object_id_dup(id);
872 if ((*qid)->id == NULL) {
873 err = got_error_from_errno("got_object_id_dup");
874 got_object_qid_free(*qid);
882 const struct got_error *
883 got_object_id_queue_copy(const struct got_object_id_queue *src,
884 struct got_object_id_queue *dest)
886 const struct got_error *err;
887 struct got_object_qid *qid;
889 STAILQ_FOREACH(qid, src, entry) {
890 struct got_object_qid *new;
892 * Deep-copy the object ID only. Let the caller deal
893 * with setting up the new->data pointer if needed.
895 err = got_object_qid_alloc(&new, qid->id);
897 got_object_id_queue_free(dest);
900 STAILQ_INSERT_TAIL(dest, new, entry);
906 static const struct got_error *
907 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
908 int pack_idx, struct got_object_id *id)
910 const struct got_error *err = NULL;
912 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
917 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
920 static const struct got_error *
921 read_packed_tree_privsep(struct got_tree_object **tree,
922 struct got_pack *pack, struct got_packidx *packidx, int idx,
923 struct got_object_id *id)
925 const struct got_error *err = NULL;
927 if (pack->privsep_child)
928 return request_packed_tree(tree, pack, idx, id);
930 err = start_pack_privsep_child(pack, packidx);
934 return request_packed_tree(tree, pack, idx, id);
937 static const struct got_error *
938 request_tree(struct got_tree_object **tree, struct got_repository *repo,
939 int fd, struct got_object_id *id)
941 const struct got_error *err = NULL;
942 struct imsgbuf *ibuf;
944 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
946 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
950 return got_privsep_recv_tree(tree, ibuf);
953 const struct got_error *
954 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
955 struct got_object_id *id, struct got_repository *repo)
957 const struct got_error *err;
960 struct imsgbuf *ibuf;
962 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
963 return request_tree(tree, repo, obj_fd, id);
965 ibuf = calloc(1, sizeof(*ibuf));
967 return got_error_from_errno("calloc");
969 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
970 err = got_error_from_errno("socketpair");
977 err = got_error_from_errno("fork");
982 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
987 if (close(imsg_fds[1]) == -1) {
988 err = got_error_from_errno("close");
992 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
994 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
995 imsg_init(ibuf, imsg_fds[0]);
996 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
999 return request_tree(tree, repo, obj_fd, id);
1002 static const struct got_error *
1003 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1004 struct got_object_id *id, int check_cache)
1006 const struct got_error *err = NULL;
1007 struct got_packidx *packidx = NULL;
1009 char *path_packfile = NULL;
1012 *tree = got_repo_get_cached_tree(repo, id);
1013 if (*tree != NULL) {
1020 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1022 struct got_pack *pack = NULL;
1024 err = got_packidx_get_packfile_path(&path_packfile,
1025 packidx->path_packidx);
1029 pack = got_repo_get_cached_pack(repo, path_packfile);
1031 err = got_repo_cache_pack(&pack, repo, path_packfile,
1036 err = read_packed_tree_privsep(tree, pack,
1038 } else if (err->code == GOT_ERR_NO_OBJ) {
1041 err = got_object_open_loose_fd(&fd, id, repo);
1044 err = read_tree_privsep(tree, fd, id, repo);
1049 err = got_repo_cache_tree(repo, id, *tree);
1052 free(path_packfile);
1056 const struct got_error *
1057 got_object_open_as_tree(struct got_tree_object **tree,
1058 struct got_repository *repo, struct got_object_id *id)
1060 *tree = got_repo_get_cached_tree(repo, id);
1061 if (*tree != NULL) {
1066 return open_tree(tree, repo, id, 0);
1069 const struct got_error *
1070 got_object_tree_open(struct got_tree_object **tree,
1071 struct got_repository *repo, struct got_object *obj)
1073 return open_tree(tree, repo, got_object_get_id(obj), 1);
1077 got_object_tree_get_nentries(struct got_tree_object *tree)
1079 return tree->nentries;
1082 struct got_tree_entry *
1083 got_object_tree_get_first_entry(struct got_tree_object *tree)
1085 return got_object_tree_get_entry(tree, 0);
1088 struct got_tree_entry *
1089 got_object_tree_get_last_entry(struct got_tree_object *tree)
1091 return got_object_tree_get_entry(tree, tree->nentries - 1);
1094 struct got_tree_entry *
1095 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1097 if (i < 0 || i >= tree->nentries)
1099 return &tree->entries[i];
1103 got_tree_entry_get_mode(struct got_tree_entry *te)
1109 got_tree_entry_get_name(struct got_tree_entry *te)
1111 return &te->name[0];
1114 struct got_object_id *
1115 got_tree_entry_get_id(struct got_tree_entry *te)
1120 const struct got_error *
1121 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1123 const struct got_error *err = NULL;
1124 size_t len, totlen, hdrlen, offset;
1128 hdrlen = got_object_blob_get_hdrlen(blob);
1134 err = got_object_blob_read_block(&len, blob);
1141 totlen += len - hdrlen;
1142 p = realloc(*s, totlen + 1);
1144 err = got_error_from_errno("realloc");
1150 /* Skip blob object header first time around. */
1152 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1157 (*s)[totlen] = '\0';
1161 const struct got_error *
1162 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1163 struct got_repository *repo)
1165 const struct got_error *err = NULL;
1166 struct got_blob_object *blob = NULL;
1168 *link_target = NULL;
1170 if (!got_object_tree_entry_is_symlink(te))
1171 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1173 err = got_object_open_as_blob(&blob, repo,
1174 got_tree_entry_get_id(te), PATH_MAX);
1178 err = got_object_blob_read_to_str(link_target, blob);
1179 got_object_blob_close(blob);
1182 *link_target = NULL;
1188 got_tree_entry_get_index(struct got_tree_entry *te)
1193 struct got_tree_entry *
1194 got_tree_entry_get_next(struct got_tree_object *tree,
1195 struct got_tree_entry *te)
1197 return got_object_tree_get_entry(tree, te->idx + 1);
1200 struct got_tree_entry *
1201 got_tree_entry_get_prev(struct got_tree_object *tree,
1202 struct got_tree_entry *te)
1204 return got_object_tree_get_entry(tree, te->idx - 1);
1207 static const struct got_error *
1208 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1209 struct got_pack *pack, struct got_packidx *packidx, int idx,
1210 struct got_object_id *id)
1212 const struct got_error *err = NULL;
1213 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1216 err = pack_child_send_tempfiles(ibuf, pack);
1220 outfd_child = dup(outfd);
1221 if (outfd_child == -1)
1222 return got_error_from_errno("dup");
1224 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1228 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1234 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1235 pack->privsep_child->ibuf);
1239 if (lseek(outfd, SEEK_SET, 0) == -1)
1240 err = got_error_from_errno("lseek");
1245 static const struct got_error *
1246 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1247 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1248 struct got_object_id *id)
1250 const struct got_error *err = NULL;
1252 if (pack->privsep_child == NULL) {
1253 err = start_pack_privsep_child(pack, packidx);
1258 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1262 static const struct got_error *
1263 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1264 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1266 const struct got_error *err = NULL;
1269 outfd_child = dup(outfd);
1270 if (outfd_child == -1)
1271 return got_error_from_errno("dup");
1273 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1277 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1281 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1285 if (lseek(outfd, SEEK_SET, 0) == -1)
1286 return got_error_from_errno("lseek");
1291 static const struct got_error *
1292 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1293 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1295 const struct got_error *err;
1298 struct imsgbuf *ibuf;
1300 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1301 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1302 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1306 ibuf = calloc(1, sizeof(*ibuf));
1308 return got_error_from_errno("calloc");
1310 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1311 err = got_error_from_errno("socketpair");
1318 err = got_error_from_errno("fork");
1322 else if (pid == 0) {
1323 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1328 if (close(imsg_fds[1]) == -1) {
1329 err = got_error_from_errno("close");
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1335 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1336 imsg_init(ibuf, imsg_fds[0]);
1337 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1339 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1342 static const struct got_error *
1343 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1344 struct got_object_id *id, size_t blocksize)
1346 const struct got_error *err = NULL;
1347 struct got_packidx *packidx = NULL;
1349 char *path_packfile = NULL;
1352 size_t size, hdrlen;
1355 *blob = calloc(1, sizeof(**blob));
1357 return got_error_from_errno("calloc");
1359 outfd = got_opentempfd();
1361 return got_error_from_errno("got_opentempfd");
1363 (*blob)->read_buf = malloc(blocksize);
1364 if ((*blob)->read_buf == NULL) {
1365 err = got_error_from_errno("malloc");
1369 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1371 struct got_pack *pack = NULL;
1373 err = got_packidx_get_packfile_path(&path_packfile,
1374 packidx->path_packidx);
1378 pack = got_repo_get_cached_pack(repo, path_packfile);
1380 err = got_repo_cache_pack(&pack, repo, path_packfile,
1385 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1386 pack, packidx, idx, id);
1387 } else if (err->code == GOT_ERR_NO_OBJ) {
1390 err = got_object_open_loose_fd(&infd, id, repo);
1393 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1399 if (hdrlen > size) {
1400 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1405 if (close(outfd) == -1 && err == NULL)
1406 err = got_error_from_errno("close");
1408 (*blob)->f = fmemopen(outbuf, size, "rb");
1409 if ((*blob)->f == NULL) {
1410 err = got_error_from_errno("fmemopen");
1414 (*blob)->data = outbuf;
1416 if (fstat(outfd, &sb) == -1) {
1417 err = got_error_from_errno("fstat");
1421 if (sb.st_size != size) {
1422 err = got_error(GOT_ERR_PRIVSEP_LEN);
1426 (*blob)->f = fdopen(outfd, "rb");
1427 if ((*blob)->f == NULL) {
1428 err = got_error_from_errno("fdopen");
1435 (*blob)->hdrlen = hdrlen;
1436 (*blob)->blocksize = blocksize;
1437 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1440 free(path_packfile);
1443 got_object_blob_close(*blob);
1445 } else if (outfd != -1)
1451 const struct got_error *
1452 got_object_open_as_blob(struct got_blob_object **blob,
1453 struct got_repository *repo, struct got_object_id *id,
1456 return open_blob(blob, repo, id, blocksize);
1459 const struct got_error *
1460 got_object_blob_open(struct got_blob_object **blob,
1461 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1463 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1466 const struct got_error *
1467 got_object_blob_close(struct got_blob_object *blob)
1469 const struct got_error *err = NULL;
1470 free(blob->read_buf);
1471 if (blob->f && fclose(blob->f) == EOF)
1472 err = got_error_from_errno("fclose");
1479 got_object_blob_rewind(struct got_blob_object *blob)
1486 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1488 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1492 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1494 return blob->hdrlen;
1498 got_object_blob_get_read_buf(struct got_blob_object *blob)
1500 return blob->read_buf;
1503 const struct got_error *
1504 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1508 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1509 if (n == 0 && ferror(blob->f))
1510 return got_ferror(blob->f, GOT_ERR_IO);
1515 const struct got_error *
1516 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1517 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1519 const struct got_error *err = NULL;
1520 size_t n, len, hdrlen;
1523 const int alloc_chunksz = 512;
1525 off_t off = 0, total_len = 0;
1528 *line_offsets = NULL;
1534 hdrlen = got_object_blob_get_hdrlen(blob);
1536 err = got_object_blob_read_block(&len, blob);
1541 buf = got_object_blob_get_read_buf(blob);
1544 if (line_offsets && *line_offsets == NULL) {
1545 /* Have some data but perhaps no '\n'. */
1547 nalloc = alloc_chunksz;
1548 *line_offsets = calloc(nalloc,
1549 sizeof(**line_offsets));
1550 if (*line_offsets == NULL)
1551 return got_error_from_errno("calloc");
1553 /* Skip forward over end of first line. */
1560 /* Scan '\n' offsets in remaining chunk of data. */
1562 if (buf[i] != '\n') {
1567 if (line_offsets && nalloc < *nlines) {
1568 size_t n = *nlines + alloc_chunksz;
1569 off_t *o = recallocarray(*line_offsets,
1570 nalloc, n, sizeof(**line_offsets));
1572 free(*line_offsets);
1573 *line_offsets = NULL;
1574 return got_error_from_errno(
1581 off = total_len + i - hdrlen + 1;
1582 (*line_offsets)[*nlines - 1] = off;
1587 /* Skip blob object header first time around. */
1588 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1589 if (n != len - hdrlen)
1590 return got_ferror(outfile, GOT_ERR_IO);
1591 total_len += len - hdrlen;
1595 if (fflush(outfile) != 0)
1596 return got_error_from_errno("fflush");
1600 *filesize = total_len;
1605 static const struct got_error *
1606 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1607 int pack_idx, struct got_object_id *id)
1609 const struct got_error *err = NULL;
1611 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1616 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1619 static const struct got_error *
1620 read_packed_tag_privsep(struct got_tag_object **tag,
1621 struct got_pack *pack, struct got_packidx *packidx, int idx,
1622 struct got_object_id *id)
1624 const struct got_error *err = NULL;
1626 if (pack->privsep_child)
1627 return request_packed_tag(tag, pack, idx, id);
1629 err = start_pack_privsep_child(pack, packidx);
1633 return request_packed_tag(tag, pack, idx, id);
1636 static const struct got_error *
1637 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1638 int fd, struct got_object_id *id)
1640 const struct got_error *err = NULL;
1641 struct imsgbuf *ibuf;
1643 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1645 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1649 return got_privsep_recv_tag(tag, ibuf);
1652 static const struct got_error *
1653 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1654 struct got_object_id *id, struct got_repository *repo)
1656 const struct got_error *err;
1659 struct imsgbuf *ibuf;
1661 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1662 return request_tag(tag, repo, obj_fd, id);
1664 ibuf = calloc(1, sizeof(*ibuf));
1666 return got_error_from_errno("calloc");
1668 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1669 err = got_error_from_errno("socketpair");
1676 err = got_error_from_errno("fork");
1680 else if (pid == 0) {
1681 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1686 if (close(imsg_fds[1]) == -1) {
1687 err = got_error_from_errno("close");
1691 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1693 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1694 imsg_init(ibuf, imsg_fds[0]);
1695 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1697 return request_tag(tag, repo, obj_fd, id);
1700 static const struct got_error *
1701 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1702 struct got_object_id *id, int check_cache)
1704 const struct got_error *err = NULL;
1705 struct got_packidx *packidx = NULL;
1707 char *path_packfile = NULL;
1708 struct got_object *obj = NULL;
1709 int obj_type = GOT_OBJ_TYPE_ANY;
1712 *tag = got_repo_get_cached_tag(repo, id);
1720 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1722 struct got_pack *pack = NULL;
1724 err = got_packidx_get_packfile_path(&path_packfile,
1725 packidx->path_packidx);
1729 pack = got_repo_get_cached_pack(repo, path_packfile);
1731 err = got_repo_cache_pack(&pack, repo, path_packfile,
1737 /* Beware of "lightweight" tags: Check object type first. */
1738 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1742 obj_type = obj->type;
1743 got_object_close(obj);
1744 if (obj_type != GOT_OBJ_TYPE_TAG) {
1745 err = got_error(GOT_ERR_OBJ_TYPE);
1748 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1749 } else if (err->code == GOT_ERR_NO_OBJ) {
1752 err = got_object_open_loose_fd(&fd, id, repo);
1755 err = got_object_read_header_privsep(&obj, id, repo, fd);
1758 obj_type = obj->type;
1759 got_object_close(obj);
1760 if (obj_type != GOT_OBJ_TYPE_TAG)
1761 return got_error(GOT_ERR_OBJ_TYPE);
1763 err = got_object_open_loose_fd(&fd, id, repo);
1766 err = read_tag_privsep(tag, fd, id, repo);
1771 err = got_repo_cache_tag(repo, id, *tag);
1774 free(path_packfile);
1778 const struct got_error *
1779 got_object_open_as_tag(struct got_tag_object **tag,
1780 struct got_repository *repo, struct got_object_id *id)
1782 *tag = got_repo_get_cached_tag(repo, id);
1788 return open_tag(tag, repo, id, 0);
1791 const struct got_error *
1792 got_object_tag_open(struct got_tag_object **tag,
1793 struct got_repository *repo, struct got_object *obj)
1795 return open_tag(tag, repo, got_object_get_id(obj), 1);
1799 got_object_tag_get_name(struct got_tag_object *tag)
1805 got_object_tag_get_object_type(struct got_tag_object *tag)
1807 return tag->obj_type;
1810 struct got_object_id *
1811 got_object_tag_get_object_id(struct got_tag_object *tag)
1817 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1819 return tag->tagger_time;
1823 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1825 return tag->tagger_gmtoff;
1829 got_object_tag_get_tagger(struct got_tag_object *tag)
1835 got_object_tag_get_message(struct got_tag_object *tag)
1840 static struct got_tree_entry *
1841 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1845 /* Note that tree entries are sorted in strncmp() order. */
1846 for (i = 0; i < tree->nentries; i++) {
1847 struct got_tree_entry *te = &tree->entries[i];
1848 int cmp = strncmp(te->name, name, len);
1853 if (te->name[len] == '\0')
1859 struct got_tree_entry *
1860 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1862 return find_entry_by_name(tree, name, strlen(name));
1865 const struct got_error *
1866 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1867 struct got_repository *repo, struct got_tree_object *tree,
1870 const struct got_error *err = NULL;
1871 struct got_tree_object *subtree = NULL;
1872 struct got_tree_entry *te = NULL;
1873 const char *seg, *s;
1885 struct got_tree_object *next_tree;
1894 te = find_entry_by_name(subtree, seg, seglen);
1896 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1907 err = got_object_open_as_tree(&next_tree, repo,
1912 if (subtree != tree)
1913 got_object_tree_close(subtree);
1914 subtree = next_tree;
1919 *id = got_object_id_dup(&te->id);
1921 return got_error_from_errno("got_object_id_dup");
1925 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1927 if (subtree && subtree != tree)
1928 got_object_tree_close(subtree);
1931 const struct got_error *
1932 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1933 struct got_object_id *commit_id, const char *path)
1935 const struct got_error *err = NULL;
1936 struct got_commit_object *commit = NULL;
1937 struct got_tree_object *tree = NULL;
1941 err = got_object_open_as_commit(&commit, repo, commit_id);
1945 /* Handle opening of root of commit's tree. */
1946 if (got_path_is_root_dir(path)) {
1947 *id = got_object_id_dup(commit->tree_id);
1949 err = got_error_from_errno("got_object_id_dup");
1951 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1954 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1958 got_object_commit_close(commit);
1960 got_object_tree_close(tree);
1965 * Normalize file mode bits to avoid false positive tree entry differences
1966 * in case tree entries have unexpected mode bits set.
1969 normalize_mode_for_comparison(mode_t mode)
1972 * For directories, the only relevant bit is the IFDIR bit.
1973 * This allows us to detect paths changing from a directory
1974 * to a file and vice versa.
1977 return mode & S_IFDIR;
1980 * For symlinks, the only relevant bit is the IFLNK bit.
1981 * This allows us to detect paths changing from a symlinks
1982 * to a file or directory and vice versa.
1985 return mode & S_IFLNK;
1987 /* For files, the only change we care about is the executable bit. */
1988 return mode & S_IXUSR;
1991 const struct got_error *
1992 got_object_tree_path_changed(int *changed,
1993 struct got_tree_object *tree01, struct got_tree_object *tree02,
1994 const char *path, struct got_repository *repo)
1996 const struct got_error *err = NULL;
1997 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1998 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1999 const char *seg, *s;
2004 /* We not do support comparing the root path. */
2005 if (got_path_is_root_dir(path))
2006 return got_error_path(path, GOT_ERR_BAD_PATH);
2016 struct got_tree_object *next_tree1, *next_tree2;
2017 mode_t mode1, mode2;
2026 te1 = find_entry_by_name(tree1, seg, seglen);
2028 err = got_error(GOT_ERR_NO_OBJ);
2033 te2 = find_entry_by_name(tree2, seg, seglen);
2036 mode1 = normalize_mode_for_comparison(te1->mode);
2037 mode2 = normalize_mode_for_comparison(te2->mode);
2038 if (mode1 != mode2) {
2043 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2049 if (*s == '\0') { /* final path element */
2058 err = got_object_open_as_tree(&next_tree1, repo,
2063 if (tree1 != tree01)
2064 got_object_tree_close(tree1);
2068 err = got_object_open_as_tree(&next_tree2, repo,
2073 if (tree2 != tree02)
2074 got_object_tree_close(tree2);
2077 if (tree2 != tree02)
2078 got_object_tree_close(tree2);
2084 if (tree1 && tree1 != tree01)
2085 got_object_tree_close(tree1);
2086 if (tree2 && tree2 != tree02)
2087 got_object_tree_close(tree2);
2091 const struct got_error *
2092 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2093 struct got_tree_entry *te)
2095 const struct got_error *err = NULL;
2097 *new_te = calloc(1, sizeof(**new_te));
2098 if (*new_te == NULL)
2099 return got_error_from_errno("calloc");
2101 (*new_te)->mode = te->mode;
2102 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2103 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2108 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2110 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2114 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2116 /* S_IFDIR check avoids confusing symlinks with submodules. */
2117 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2120 static const struct got_error *
2121 resolve_symlink(char **link_target, const char *path,
2122 struct got_object_id *commit_id, struct got_repository *repo)
2124 const struct got_error *err = NULL;
2126 char *name, *parent_path = NULL;
2127 struct got_object_id *tree_obj_id = NULL;
2128 struct got_tree_object *tree = NULL;
2129 struct got_tree_entry *te = NULL;
2131 *link_target = NULL;
2133 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2134 return got_error(GOT_ERR_NO_SPACE);
2136 name = basename(buf);
2138 return got_error_from_errno2("basename", path);
2140 err = got_path_dirname(&parent_path, path);
2144 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2147 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2148 /* Display the complete path in error message. */
2149 err = got_error_path(path, err->code);
2154 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2158 te = got_object_tree_find_entry(tree, name);
2160 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2164 if (got_object_tree_entry_is_symlink(te)) {
2165 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2168 if (!got_path_is_absolute(*link_target)) {
2170 if (asprintf(&abspath, "%s/%s", parent_path,
2171 *link_target) == -1) {
2172 err = got_error_from_errno("asprintf");
2176 *link_target = malloc(PATH_MAX);
2177 if (*link_target == NULL) {
2178 err = got_error_from_errno("malloc");
2181 err = got_canonpath(abspath, *link_target, PATH_MAX);
2190 got_object_tree_close(tree);
2193 *link_target = NULL;
2198 const struct got_error *
2199 got_object_resolve_symlinks(char **link_target, const char *path,
2200 struct got_object_id *commit_id, struct got_repository *repo)
2202 const struct got_error *err = NULL;
2203 char *next_target = NULL;
2204 int max_recursion = 40; /* matches Git */
2206 *link_target = NULL;
2209 err = resolve_symlink(&next_target,
2210 *link_target ? *link_target : path, commit_id, repo);
2215 if (--max_recursion == 0) {
2216 err = got_error_path(path, GOT_ERR_RECURSION);
2217 *link_target = NULL;
2220 *link_target = next_target;
2222 } while (next_target);
2227 const struct got_error *
2228 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2229 struct got_object_id *commit_id, const char *path,
2230 struct got_repository *repo)
2232 const struct got_error *err = NULL;
2233 struct got_pack *pack = NULL;
2234 struct got_packidx *packidx = NULL;
2235 char *path_packfile = NULL;
2236 struct got_commit_object *changed_commit = NULL;
2237 struct got_object_id *changed_commit_id = NULL;
2240 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2242 if (err->code != GOT_ERR_NO_OBJ)
2247 err = got_packidx_get_packfile_path(&path_packfile,
2248 packidx->path_packidx);
2252 pack = got_repo_get_cached_pack(repo, path_packfile);
2254 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2259 if (pack->privsep_child == NULL) {
2260 err = start_pack_privsep_child(pack, packidx);
2265 err = got_privsep_send_commit_traversal_request(
2266 pack->privsep_child->ibuf, commit_id, idx, path);
2270 err = got_privsep_recv_traversed_commits(&changed_commit,
2271 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2275 if (changed_commit) {
2277 * Cache the commit in which the path was changed.
2278 * This commit might be opened again soon.
2280 changed_commit->refcnt++;
2281 err = got_repo_cache_commit(repo, changed_commit_id,
2283 got_object_commit_close(changed_commit);
2286 free(path_packfile);
2287 free(changed_commit_id);