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>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_object_idcache.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 struct got_object_id *
64 got_object_get_id(struct got_object *obj)
69 const struct got_error *
70 got_object_get_id_str(char **outbuf, struct got_object *obj)
72 return got_object_id_str(outbuf, &obj->id);
75 const struct got_error *
76 got_object_get_type(int *type, struct got_repository *repo,
77 struct got_object_id *id)
79 const struct got_error *err = NULL;
80 struct got_object *obj;
82 err = got_object_open(&obj, repo, id);
87 case GOT_OBJ_TYPE_COMMIT:
88 case GOT_OBJ_TYPE_TREE:
89 case GOT_OBJ_TYPE_BLOB:
90 case GOT_OBJ_TYPE_TAG:
94 err = got_error(GOT_ERR_OBJ_TYPE);
98 got_object_close(obj);
102 const struct got_error *
103 got_object_get_path(char **path, struct got_object_id *id,
104 struct got_repository *repo)
106 const struct got_error *err = NULL;
112 path_objects = got_repo_get_path_objects(repo);
113 if (path_objects == NULL)
114 return got_error_from_errno("got_repo_get_path_objects");
116 err = got_object_id_str(&hex, id);
120 if (asprintf(path, "%s/%.2x/%s", path_objects,
121 id->sha1[0], hex + 2) == -1)
122 err = got_error_from_errno("asprintf");
130 const struct got_error *
131 got_object_open_loose_fd(int *fd, struct got_object_id *id,
132 struct got_repository *repo)
134 const struct got_error *err = NULL;
137 err = got_object_get_path(&path, id, repo);
140 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
142 err = got_error_from_errno2("open", path);
150 static const struct got_error *
151 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
152 struct got_object_id *id)
154 const struct got_error *err = NULL;
155 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
157 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
161 err = got_privsep_recv_obj(obj, ibuf);
165 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
170 /* Create temporary files used during delta application. */
171 static const struct got_error *
172 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
174 const struct got_error *err;
175 int basefd = -1, accumfd = -1;
178 * For performance reasons, the child will keep reusing the
179 * same temporary files during every object request.
180 * Opening and closing new files for every object request is
181 * too expensive during operations such as 'gotadmin pack'.
183 if (pack->child_has_tempfiles)
186 basefd = dup(pack->basefd);
188 return got_error_from_errno("dup");
190 accumfd = dup(pack->accumfd);
192 err = got_error_from_errno("dup");
196 err = got_privsep_send_tmpfd(ibuf, basefd);
200 err = got_privsep_send_tmpfd(ibuf, accumfd);
208 pack->child_has_tempfiles = 1;
212 static const struct got_error *
213 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
214 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
216 const struct got_error *err = NULL;
217 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
220 err = pack_child_send_tempfiles(ibuf, pack);
224 outfd_child = dup(outfd);
225 if (outfd_child == -1)
226 return got_error_from_errno("dup");
228 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
234 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
238 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
246 set_max_datasize(void)
250 if (getrlimit(RLIMIT_DATA, &rl) != 0)
253 rl.rlim_cur = rl.rlim_max;
254 setrlimit(RLIMIT_DATA, &rl);
257 static const struct got_error *
258 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
260 const struct got_error *err = NULL;
263 struct imsgbuf *ibuf;
265 ibuf = calloc(1, sizeof(*ibuf));
267 return got_error_from_errno("calloc");
269 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
270 if (pack->privsep_child == NULL) {
271 err = got_error_from_errno("calloc");
275 pack->child_has_tempfiles = 0;
276 pack->child_has_delta_outfd = 0;
278 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
279 err = got_error_from_errno("socketpair");
285 err = got_error_from_errno("fork");
287 } else if (pid == 0) {
289 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
290 pack->path_packfile);
294 if (close(imsg_fds[1]) == -1)
295 return got_error_from_errno("close");
296 pack->privsep_child->imsg_fd = imsg_fds[0];
297 pack->privsep_child->pid = pid;
298 imsg_init(ibuf, imsg_fds[0]);
299 pack->privsep_child->ibuf = ibuf;
301 err = got_privsep_init_pack_child(ibuf, pack, packidx);
303 const struct got_error *child_err;
304 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
305 child_err = got_privsep_wait_for_child(
306 pack->privsep_child->pid);
307 if (child_err && err == NULL)
313 free(pack->privsep_child);
314 pack->privsep_child = NULL;
319 static const struct got_error *
320 read_packed_object_privsep(struct got_object **obj,
321 struct got_repository *repo, struct got_pack *pack,
322 struct got_packidx *packidx, int idx, 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(obj, pack, idx, id);
335 static const struct got_error *
336 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
337 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
338 struct got_object_id *id)
340 const struct got_error *err = NULL;
342 if (pack->privsep_child == NULL) {
343 err = start_pack_privsep_child(pack, packidx);
348 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
352 const struct got_error *
353 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
354 struct got_repository *repo)
356 const struct got_error *err = NULL;
357 struct got_pack *pack = NULL;
358 struct got_packidx *packidx = NULL;
362 err = got_repo_search_packidx(&packidx, &idx, repo, id);
366 err = got_packidx_get_packfile_path(&path_packfile,
367 packidx->path_packidx);
371 pack = got_repo_get_cached_pack(repo, path_packfile);
373 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
378 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
386 const struct got_error *
387 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
388 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
389 struct got_repository *repo)
391 return read_packed_object_privsep(obj, repo, pack, packidx,
395 const struct got_error *
396 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
397 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
398 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
399 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
400 struct got_repository *repo)
402 const struct got_error *err = NULL;
403 struct got_pack *pack = NULL;
409 *delta_compressed_size = 0;
411 *delta_out_offset = 0;
413 err = got_packidx_get_packfile_path(&path_packfile,
414 packidx->path_packidx);
418 pack = got_repo_get_cached_pack(repo, path_packfile);
420 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
425 if (pack->privsep_child == NULL) {
426 err = start_pack_privsep_child(pack, packidx);
431 if (!pack->child_has_delta_outfd) {
433 outfd_child = dup(delta_cache_fd);
434 if (outfd_child == -1)
435 return got_error_from_errno("dup");
436 err = got_privsep_send_raw_delta_outfd(
437 pack->privsep_child->ibuf, outfd_child);
440 pack->child_has_delta_outfd = 1;
443 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
448 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
449 delta_compressed_size, delta_offset, delta_out_offset, base_id,
450 pack->privsep_child->ibuf);
454 * XXX This function does not really belong in object.c. It is only here
455 * because it needs start_pack_privsep_child(); relevant code should
456 * probably be moved to pack.c/pack_create.c.
458 const struct got_error *
459 got_object_prepare_delta_reuse(struct got_pack **pack,
460 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
462 const struct got_error *err = NULL;
463 char *path_packfile = NULL;
465 err = got_packidx_get_packfile_path(&path_packfile,
466 packidx->path_packidx);
470 *pack = got_repo_get_cached_pack(repo, path_packfile);
472 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
476 if ((*pack)->privsep_child == NULL) {
477 err = start_pack_privsep_child(*pack, packidx);
482 if (!(*pack)->child_has_delta_outfd) {
484 outfd_child = dup(delta_outfd);
485 if (outfd_child == -1) {
486 err = got_error_from_errno("dup");
489 err = got_privsep_send_raw_delta_outfd(
490 (*pack)->privsep_child->ibuf, outfd_child);
493 (*pack)->child_has_delta_outfd = 1;
496 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
502 static const struct got_error *
503 request_object(struct got_object **obj, struct got_object_id *id,
504 struct got_repository *repo, int fd)
506 const struct got_error *err = NULL;
507 struct imsgbuf *ibuf;
509 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
511 err = got_privsep_send_obj_req(ibuf, fd, id);
515 return got_privsep_recv_obj(obj, ibuf);
518 static const struct got_error *
519 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
520 struct got_object_id *id, struct got_repository *repo, int infd)
522 const struct got_error *err = NULL;
523 struct imsgbuf *ibuf;
526 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
528 outfd_child = dup(outfd);
529 if (outfd_child == -1)
530 return got_error_from_errno("dup");
532 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
536 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
540 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
543 static const struct got_error *
544 start_read_object_child(struct got_repository *repo)
546 const struct got_error *err = NULL;
549 struct imsgbuf *ibuf;
551 ibuf = calloc(1, sizeof(*ibuf));
553 return got_error_from_errno("calloc");
555 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
556 err = got_error_from_errno("socketpair");
563 err = got_error_from_errno("fork");
568 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
573 if (close(imsg_fds[1]) == -1) {
574 err = got_error_from_errno("close");
579 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
581 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
582 imsg_init(ibuf, imsg_fds[0]);
583 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
588 const struct got_error *
589 got_object_read_header_privsep(struct got_object **obj,
590 struct got_object_id *id, struct got_repository *repo, int obj_fd)
592 const struct got_error *err;
594 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
595 return request_object(obj, id, repo, obj_fd);
597 err = start_read_object_child(repo);
603 return request_object(obj, id, repo, obj_fd);
606 static const struct got_error *
607 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
608 int outfd, struct got_object_id *id, struct got_repository *repo,
611 const struct got_error *err;
613 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
614 return request_raw_object(outbuf, size, hdrlen, outfd, id,
617 err = start_read_object_child(repo);
621 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
625 const struct got_error *
626 got_object_open(struct got_object **obj, struct got_repository *repo,
627 struct got_object_id *id)
629 const struct got_error *err = NULL;
632 *obj = got_repo_get_cached_object(repo, id);
638 err = got_object_open_packed(obj, id, repo);
639 if (err && err->code != GOT_ERR_NO_OBJ)
643 return got_repo_cache_object(repo, id, *obj);
646 err = got_object_open_loose_fd(&fd, id, repo);
648 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
649 err = got_error_no_obj(id);
653 err = got_object_read_header_privsep(obj, id, repo, fd);
657 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
660 return got_repo_cache_object(repo, id, *obj);
663 /* *outfd must be initialized to -1 by caller */
664 const struct got_error *
665 got_object_raw_open(struct got_raw_object **obj, int *outfd,
666 struct got_repository *repo, struct got_object_id *id)
668 const struct got_error *err = NULL;
669 struct got_packidx *packidx = NULL;
671 uint8_t *outbuf = NULL;
674 char *path_packfile = NULL;
676 *obj = got_repo_get_cached_raw_object(repo, id);
683 *outfd = got_opentempfd();
685 return got_error_from_errno("got_opentempfd");
688 err = got_repo_search_packidx(&packidx, &idx, repo, id);
690 struct got_pack *pack = NULL;
692 err = got_packidx_get_packfile_path(&path_packfile,
693 packidx->path_packidx);
697 pack = got_repo_get_cached_pack(repo, path_packfile);
699 err = got_repo_cache_pack(&pack, repo, path_packfile,
704 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
705 *outfd, pack, packidx, idx, id);
708 } else if (err->code == GOT_ERR_NO_OBJ) {
711 err = got_object_open_loose_fd(&fd, id, repo);
714 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
720 *obj = calloc(1, sizeof(**obj));
722 err = got_error_from_errno("calloc");
728 (*obj)->data = outbuf;
731 if (fstat(*outfd, &sb) == -1) {
732 err = got_error_from_errno("fstat");
736 if (sb.st_size != hdrlen + size) {
737 err = got_error(GOT_ERR_PRIVSEP_LEN);
740 #ifndef GOT_PACK_NO_MMAP
741 if (hdrlen + size > 0) {
742 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
743 MAP_PRIVATE, *outfd, 0);
744 if ((*obj)->data == MAP_FAILED) {
745 if (errno != ENOMEM) {
746 err = got_error_from_errno("mmap");
757 (*obj)->f = fdopen(*outfd, "r");
758 if ((*obj)->f == NULL) {
759 err = got_error_from_errno("fdopen");
765 (*obj)->hdrlen = hdrlen;
767 err = got_repo_cache_raw_object(repo, id, *obj);
772 got_object_raw_close(*obj);
781 const struct got_error *
782 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
785 struct got_object_id id;
787 if (!got_parse_sha1_digest(id.sha1, id_str))
788 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
790 return got_object_open(obj, repo, &id);
793 const struct got_error *
794 got_object_resolve_id_str(struct got_object_id **id,
795 struct got_repository *repo, const char *id_str)
797 const struct got_error *err = NULL;
798 struct got_object *obj;
800 err = got_object_open_by_id_str(&obj, repo, id_str);
804 *id = got_object_id_dup(got_object_get_id(obj));
805 got_object_close(obj);
807 return got_error_from_errno("got_object_id_dup");
812 static const struct got_error *
813 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
814 int pack_idx, struct got_object_id *id)
816 const struct got_error *err = NULL;
818 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
823 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
827 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
831 static const struct got_error *
832 read_packed_commit_privsep(struct got_commit_object **commit,
833 struct got_pack *pack, struct got_packidx *packidx, int idx,
834 struct got_object_id *id)
836 const struct got_error *err = NULL;
838 if (pack->privsep_child)
839 return request_packed_commit(commit, pack, idx, id);
841 err = start_pack_privsep_child(pack, packidx);
845 return request_packed_commit(commit, pack, idx, id);
848 static const struct got_error *
849 request_commit(struct got_commit_object **commit, struct got_repository *repo,
850 int fd, struct got_object_id *id)
852 const struct got_error *err = NULL;
853 struct imsgbuf *ibuf;
855 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
857 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
861 return got_privsep_recv_commit(commit, ibuf);
864 static const struct got_error *
865 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
866 struct got_object_id *id, struct got_repository *repo)
868 const struct got_error *err;
871 struct imsgbuf *ibuf;
873 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
874 return request_commit(commit, repo, obj_fd, id);
876 ibuf = calloc(1, sizeof(*ibuf));
878 return got_error_from_errno("calloc");
880 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
881 err = got_error_from_errno("socketpair");
888 err = got_error_from_errno("fork");
893 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
898 if (close(imsg_fds[1]) == -1) {
899 err = got_error_from_errno("close");
903 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
905 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
906 imsg_init(ibuf, imsg_fds[0]);
907 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
909 return request_commit(commit, repo, obj_fd, id);
913 static const struct got_error *
914 open_commit(struct got_commit_object **commit,
915 struct got_repository *repo, struct got_object_id *id, int check_cache)
917 const struct got_error *err = NULL;
918 struct got_packidx *packidx = NULL;
920 char *path_packfile = NULL;
923 *commit = got_repo_get_cached_commit(repo, id);
924 if (*commit != NULL) {
931 err = got_repo_search_packidx(&packidx, &idx, repo, id);
933 struct got_pack *pack = NULL;
935 err = got_packidx_get_packfile_path(&path_packfile,
936 packidx->path_packidx);
940 pack = got_repo_get_cached_pack(repo, path_packfile);
942 err = got_repo_cache_pack(&pack, repo, path_packfile,
947 err = read_packed_commit_privsep(commit, pack,
949 } else if (err->code == GOT_ERR_NO_OBJ) {
952 err = got_object_open_loose_fd(&fd, id, repo);
955 err = read_commit_privsep(commit, fd, id, repo);
960 err = got_repo_cache_commit(repo, id, *commit);
967 const struct got_error *
968 got_object_open_as_commit(struct got_commit_object **commit,
969 struct got_repository *repo, struct got_object_id *id)
971 *commit = got_repo_get_cached_commit(repo, id);
972 if (*commit != NULL) {
977 return open_commit(commit, repo, id, 0);
980 const struct got_error *
981 got_object_commit_open(struct got_commit_object **commit,
982 struct got_repository *repo, struct got_object *obj)
984 return open_commit(commit, repo, got_object_get_id(obj), 1);
987 const struct got_error *
988 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
990 *qid = calloc(1, sizeof(**qid));
992 return got_error_from_errno("calloc");
994 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
998 const struct got_error *
999 got_object_id_queue_copy(const struct got_object_id_queue *src,
1000 struct got_object_id_queue *dest)
1002 const struct got_error *err;
1003 struct got_object_qid *qid;
1005 STAILQ_FOREACH(qid, src, entry) {
1006 struct got_object_qid *new;
1008 * Deep-copy the object ID only. Let the caller deal
1009 * with setting up the new->data pointer if needed.
1011 err = got_object_qid_alloc(&new, &qid->id);
1013 got_object_id_queue_free(dest);
1016 STAILQ_INSERT_TAIL(dest, new, entry);
1022 static const struct got_error *
1023 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1024 int pack_idx, struct got_object_id *id)
1026 const struct got_error *err = NULL;
1028 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1033 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1036 static const struct got_error *
1037 read_packed_tree_privsep(struct got_tree_object **tree,
1038 struct got_pack *pack, struct got_packidx *packidx, int idx,
1039 struct got_object_id *id)
1041 const struct got_error *err = NULL;
1043 if (pack->privsep_child)
1044 return request_packed_tree(tree, pack, idx, id);
1046 err = start_pack_privsep_child(pack, packidx);
1050 return request_packed_tree(tree, pack, idx, id);
1053 static const struct got_error *
1054 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1055 int fd, struct got_object_id *id)
1057 const struct got_error *err = NULL;
1058 struct imsgbuf *ibuf;
1060 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1062 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1066 return got_privsep_recv_tree(tree, ibuf);
1069 const struct got_error *
1070 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1071 struct got_object_id *id, struct got_repository *repo)
1073 const struct got_error *err;
1076 struct imsgbuf *ibuf;
1078 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1079 return request_tree(tree, repo, obj_fd, id);
1081 ibuf = calloc(1, sizeof(*ibuf));
1083 return got_error_from_errno("calloc");
1085 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1086 err = got_error_from_errno("socketpair");
1093 err = got_error_from_errno("fork");
1097 else if (pid == 0) {
1098 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1103 if (close(imsg_fds[1]) == -1) {
1104 err = got_error_from_errno("close");
1108 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1110 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1111 imsg_init(ibuf, imsg_fds[0]);
1112 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1115 return request_tree(tree, repo, obj_fd, id);
1118 static const struct got_error *
1119 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1120 struct got_object_id *id, int check_cache)
1122 const struct got_error *err = NULL;
1123 struct got_packidx *packidx = NULL;
1125 char *path_packfile = NULL;
1128 *tree = got_repo_get_cached_tree(repo, id);
1129 if (*tree != NULL) {
1136 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1138 struct got_pack *pack = NULL;
1140 err = got_packidx_get_packfile_path(&path_packfile,
1141 packidx->path_packidx);
1145 pack = got_repo_get_cached_pack(repo, path_packfile);
1147 err = got_repo_cache_pack(&pack, repo, path_packfile,
1152 err = read_packed_tree_privsep(tree, pack,
1154 } else if (err->code == GOT_ERR_NO_OBJ) {
1157 err = got_object_open_loose_fd(&fd, id, repo);
1160 err = read_tree_privsep(tree, fd, id, repo);
1165 err = got_repo_cache_tree(repo, id, *tree);
1168 free(path_packfile);
1172 const struct got_error *
1173 got_object_open_as_tree(struct got_tree_object **tree,
1174 struct got_repository *repo, struct got_object_id *id)
1176 *tree = got_repo_get_cached_tree(repo, id);
1177 if (*tree != NULL) {
1182 return open_tree(tree, repo, id, 0);
1185 const struct got_error *
1186 got_object_tree_open(struct got_tree_object **tree,
1187 struct got_repository *repo, struct got_object *obj)
1189 return open_tree(tree, repo, got_object_get_id(obj), 1);
1193 got_object_tree_get_nentries(struct got_tree_object *tree)
1195 return tree->nentries;
1198 struct got_tree_entry *
1199 got_object_tree_get_first_entry(struct got_tree_object *tree)
1201 return got_object_tree_get_entry(tree, 0);
1204 struct got_tree_entry *
1205 got_object_tree_get_last_entry(struct got_tree_object *tree)
1207 return got_object_tree_get_entry(tree, tree->nentries - 1);
1210 struct got_tree_entry *
1211 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1213 if (i < 0 || i >= tree->nentries)
1215 return &tree->entries[i];
1219 got_tree_entry_get_mode(struct got_tree_entry *te)
1225 got_tree_entry_get_name(struct got_tree_entry *te)
1227 return &te->name[0];
1230 struct got_object_id *
1231 got_tree_entry_get_id(struct got_tree_entry *te)
1236 const struct got_error *
1237 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1239 const struct got_error *err = NULL;
1240 size_t len, totlen, hdrlen, offset;
1244 hdrlen = got_object_blob_get_hdrlen(blob);
1250 err = got_object_blob_read_block(&len, blob);
1257 totlen += len - hdrlen;
1258 p = realloc(*s, totlen + 1);
1260 err = got_error_from_errno("realloc");
1266 /* Skip blob object header first time around. */
1268 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1273 (*s)[totlen] = '\0';
1277 const struct got_error *
1278 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1279 struct got_repository *repo)
1281 const struct got_error *err = NULL;
1282 struct got_blob_object *blob = NULL;
1284 *link_target = NULL;
1286 if (!got_object_tree_entry_is_symlink(te))
1287 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1289 err = got_object_open_as_blob(&blob, repo,
1290 got_tree_entry_get_id(te), PATH_MAX);
1294 err = got_object_blob_read_to_str(link_target, blob);
1295 got_object_blob_close(blob);
1298 *link_target = NULL;
1304 got_tree_entry_get_index(struct got_tree_entry *te)
1309 struct got_tree_entry *
1310 got_tree_entry_get_next(struct got_tree_object *tree,
1311 struct got_tree_entry *te)
1313 return got_object_tree_get_entry(tree, te->idx + 1);
1316 struct got_tree_entry *
1317 got_tree_entry_get_prev(struct got_tree_object *tree,
1318 struct got_tree_entry *te)
1320 return got_object_tree_get_entry(tree, te->idx - 1);
1323 static const struct got_error *
1324 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1325 struct got_pack *pack, struct got_packidx *packidx, int idx,
1326 struct got_object_id *id)
1328 const struct got_error *err = NULL;
1329 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1332 err = pack_child_send_tempfiles(ibuf, pack);
1336 outfd_child = dup(outfd);
1337 if (outfd_child == -1)
1338 return got_error_from_errno("dup");
1340 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1344 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1350 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1351 pack->privsep_child->ibuf);
1355 if (lseek(outfd, SEEK_SET, 0) == -1)
1356 err = got_error_from_errno("lseek");
1361 static const struct got_error *
1362 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1363 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1364 struct got_object_id *id)
1366 const struct got_error *err = NULL;
1368 if (pack->privsep_child == NULL) {
1369 err = start_pack_privsep_child(pack, packidx);
1374 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1378 static const struct got_error *
1379 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1380 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1382 const struct got_error *err = NULL;
1385 outfd_child = dup(outfd);
1386 if (outfd_child == -1)
1387 return got_error_from_errno("dup");
1389 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1393 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1397 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1401 if (lseek(outfd, SEEK_SET, 0) == -1)
1402 return got_error_from_errno("lseek");
1407 static const struct got_error *
1408 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1409 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1411 const struct got_error *err;
1414 struct imsgbuf *ibuf;
1416 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1417 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1418 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1422 ibuf = calloc(1, sizeof(*ibuf));
1424 return got_error_from_errno("calloc");
1426 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1427 err = got_error_from_errno("socketpair");
1434 err = got_error_from_errno("fork");
1438 else if (pid == 0) {
1439 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1444 if (close(imsg_fds[1]) == -1) {
1445 err = got_error_from_errno("close");
1449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1452 imsg_init(ibuf, imsg_fds[0]);
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1455 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1458 static const struct got_error *
1459 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1460 struct got_object_id *id, size_t blocksize)
1462 const struct got_error *err = NULL;
1463 struct got_packidx *packidx = NULL;
1465 char *path_packfile = NULL;
1468 size_t size, hdrlen;
1471 *blob = calloc(1, sizeof(**blob));
1473 return got_error_from_errno("calloc");
1475 outfd = got_opentempfd();
1477 return got_error_from_errno("got_opentempfd");
1479 (*blob)->read_buf = malloc(blocksize);
1480 if ((*blob)->read_buf == NULL) {
1481 err = got_error_from_errno("malloc");
1485 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1487 struct got_pack *pack = NULL;
1489 err = got_packidx_get_packfile_path(&path_packfile,
1490 packidx->path_packidx);
1494 pack = got_repo_get_cached_pack(repo, path_packfile);
1496 err = got_repo_cache_pack(&pack, repo, path_packfile,
1501 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1502 pack, packidx, idx, id);
1503 } else if (err->code == GOT_ERR_NO_OBJ) {
1506 err = got_object_open_loose_fd(&infd, id, repo);
1509 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1515 if (hdrlen > size) {
1516 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1521 if (close(outfd) == -1 && err == NULL)
1522 err = got_error_from_errno("close");
1524 (*blob)->f = fmemopen(outbuf, size, "rb");
1525 if ((*blob)->f == NULL) {
1526 err = got_error_from_errno("fmemopen");
1530 (*blob)->data = outbuf;
1532 if (fstat(outfd, &sb) == -1) {
1533 err = got_error_from_errno("fstat");
1537 if (sb.st_size != size) {
1538 err = got_error(GOT_ERR_PRIVSEP_LEN);
1542 (*blob)->f = fdopen(outfd, "rb");
1543 if ((*blob)->f == NULL) {
1544 err = got_error_from_errno("fdopen");
1551 (*blob)->hdrlen = hdrlen;
1552 (*blob)->blocksize = blocksize;
1553 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1556 free(path_packfile);
1559 got_object_blob_close(*blob);
1561 } else if (outfd != -1)
1567 const struct got_error *
1568 got_object_open_as_blob(struct got_blob_object **blob,
1569 struct got_repository *repo, struct got_object_id *id,
1572 return open_blob(blob, repo, id, blocksize);
1575 const struct got_error *
1576 got_object_blob_open(struct got_blob_object **blob,
1577 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1579 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1582 const struct got_error *
1583 got_object_blob_close(struct got_blob_object *blob)
1585 const struct got_error *err = NULL;
1586 free(blob->read_buf);
1587 if (blob->f && fclose(blob->f) == EOF)
1588 err = got_error_from_errno("fclose");
1595 got_object_blob_rewind(struct got_blob_object *blob)
1602 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1604 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1608 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1610 return blob->hdrlen;
1614 got_object_blob_get_read_buf(struct got_blob_object *blob)
1616 return blob->read_buf;
1619 const struct got_error *
1620 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1624 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1625 if (n == 0 && ferror(blob->f))
1626 return got_ferror(blob->f, GOT_ERR_IO);
1631 const struct got_error *
1632 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1633 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1635 const struct got_error *err = NULL;
1636 size_t n, len, hdrlen;
1639 const int alloc_chunksz = 512;
1641 off_t off = 0, total_len = 0;
1644 *line_offsets = NULL;
1650 hdrlen = got_object_blob_get_hdrlen(blob);
1652 err = got_object_blob_read_block(&len, blob);
1657 buf = got_object_blob_get_read_buf(blob);
1660 if (line_offsets && *line_offsets == NULL) {
1661 /* Have some data but perhaps no '\n'. */
1663 nalloc = alloc_chunksz;
1664 *line_offsets = calloc(nalloc,
1665 sizeof(**line_offsets));
1666 if (*line_offsets == NULL)
1667 return got_error_from_errno("calloc");
1669 /* Skip forward over end of first line. */
1676 /* Scan '\n' offsets in remaining chunk of data. */
1678 if (buf[i] != '\n') {
1683 if (line_offsets && nalloc < *nlines) {
1684 size_t n = *nlines + alloc_chunksz;
1685 off_t *o = recallocarray(*line_offsets,
1686 nalloc, n, sizeof(**line_offsets));
1688 free(*line_offsets);
1689 *line_offsets = NULL;
1690 return got_error_from_errno(
1697 off = total_len + i - hdrlen + 1;
1698 (*line_offsets)[*nlines - 1] = off;
1703 /* Skip blob object header first time around. */
1704 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1705 if (n != len - hdrlen)
1706 return got_ferror(outfile, GOT_ERR_IO);
1707 total_len += len - hdrlen;
1711 if (fflush(outfile) != 0)
1712 return got_error_from_errno("fflush");
1716 *filesize = total_len;
1721 static const struct got_error *
1722 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1723 int pack_idx, struct got_object_id *id)
1725 const struct got_error *err = NULL;
1727 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1732 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1735 static const struct got_error *
1736 read_packed_tag_privsep(struct got_tag_object **tag,
1737 struct got_pack *pack, struct got_packidx *packidx, int idx,
1738 struct got_object_id *id)
1740 const struct got_error *err = NULL;
1742 if (pack->privsep_child)
1743 return request_packed_tag(tag, pack, idx, id);
1745 err = start_pack_privsep_child(pack, packidx);
1749 return request_packed_tag(tag, pack, idx, id);
1752 static const struct got_error *
1753 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1754 int fd, struct got_object_id *id)
1756 const struct got_error *err = NULL;
1757 struct imsgbuf *ibuf;
1759 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1761 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1765 return got_privsep_recv_tag(tag, ibuf);
1768 static const struct got_error *
1769 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1770 struct got_object_id *id, struct got_repository *repo)
1772 const struct got_error *err;
1775 struct imsgbuf *ibuf;
1777 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1778 return request_tag(tag, repo, obj_fd, id);
1780 ibuf = calloc(1, sizeof(*ibuf));
1782 return got_error_from_errno("calloc");
1784 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1785 err = got_error_from_errno("socketpair");
1792 err = got_error_from_errno("fork");
1796 else if (pid == 0) {
1797 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1802 if (close(imsg_fds[1]) == -1) {
1803 err = got_error_from_errno("close");
1807 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1810 imsg_init(ibuf, imsg_fds[0]);
1811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1813 return request_tag(tag, repo, obj_fd, id);
1816 static const struct got_error *
1817 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1818 struct got_object_id *id, int check_cache)
1820 const struct got_error *err = NULL;
1821 struct got_packidx *packidx = NULL;
1823 char *path_packfile = NULL;
1824 struct got_object *obj = NULL;
1825 int obj_type = GOT_OBJ_TYPE_ANY;
1828 *tag = got_repo_get_cached_tag(repo, id);
1836 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1838 struct got_pack *pack = NULL;
1840 err = got_packidx_get_packfile_path(&path_packfile,
1841 packidx->path_packidx);
1845 pack = got_repo_get_cached_pack(repo, path_packfile);
1847 err = got_repo_cache_pack(&pack, repo, path_packfile,
1853 /* Beware of "lightweight" tags: Check object type first. */
1854 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1858 obj_type = obj->type;
1859 got_object_close(obj);
1860 if (obj_type != GOT_OBJ_TYPE_TAG) {
1861 err = got_error(GOT_ERR_OBJ_TYPE);
1864 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1865 } else if (err->code == GOT_ERR_NO_OBJ) {
1868 err = got_object_open_loose_fd(&fd, id, repo);
1871 err = got_object_read_header_privsep(&obj, id, repo, fd);
1874 obj_type = obj->type;
1875 got_object_close(obj);
1876 if (obj_type != GOT_OBJ_TYPE_TAG)
1877 return got_error(GOT_ERR_OBJ_TYPE);
1879 err = got_object_open_loose_fd(&fd, id, repo);
1882 err = read_tag_privsep(tag, fd, id, repo);
1887 err = got_repo_cache_tag(repo, id, *tag);
1890 free(path_packfile);
1894 const struct got_error *
1895 got_object_open_as_tag(struct got_tag_object **tag,
1896 struct got_repository *repo, struct got_object_id *id)
1898 *tag = got_repo_get_cached_tag(repo, id);
1904 return open_tag(tag, repo, id, 0);
1907 const struct got_error *
1908 got_object_tag_open(struct got_tag_object **tag,
1909 struct got_repository *repo, struct got_object *obj)
1911 return open_tag(tag, repo, got_object_get_id(obj), 1);
1915 got_object_tag_get_name(struct got_tag_object *tag)
1921 got_object_tag_get_object_type(struct got_tag_object *tag)
1923 return tag->obj_type;
1926 struct got_object_id *
1927 got_object_tag_get_object_id(struct got_tag_object *tag)
1933 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1935 return tag->tagger_time;
1939 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1941 return tag->tagger_gmtoff;
1945 got_object_tag_get_tagger(struct got_tag_object *tag)
1951 got_object_tag_get_message(struct got_tag_object *tag)
1956 static struct got_tree_entry *
1957 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1961 /* Note that tree entries are sorted in strncmp() order. */
1962 for (i = 0; i < tree->nentries; i++) {
1963 struct got_tree_entry *te = &tree->entries[i];
1964 int cmp = strncmp(te->name, name, len);
1969 if (te->name[len] == '\0')
1975 struct got_tree_entry *
1976 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1978 return find_entry_by_name(tree, name, strlen(name));
1981 const struct got_error *
1982 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1983 struct got_repository *repo, struct got_tree_object *tree,
1986 const struct got_error *err = NULL;
1987 struct got_tree_object *subtree = NULL;
1988 struct got_tree_entry *te = NULL;
1989 const char *seg, *s;
2001 struct got_tree_object *next_tree;
2010 te = find_entry_by_name(subtree, seg, seglen);
2012 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2023 err = got_object_open_as_tree(&next_tree, repo,
2028 if (subtree != tree)
2029 got_object_tree_close(subtree);
2030 subtree = next_tree;
2035 *id = got_object_id_dup(&te->id);
2037 return got_error_from_errno("got_object_id_dup");
2041 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2043 if (subtree && subtree != tree)
2044 got_object_tree_close(subtree);
2047 const struct got_error *
2048 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2049 struct got_commit_object *commit, const char *path)
2051 const struct got_error *err = NULL;
2052 struct got_tree_object *tree = NULL;
2056 /* Handle opening of root of commit's tree. */
2057 if (got_path_is_root_dir(path)) {
2058 *id = got_object_id_dup(commit->tree_id);
2060 err = got_error_from_errno("got_object_id_dup");
2062 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2065 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2069 got_object_tree_close(tree);
2074 * Normalize file mode bits to avoid false positive tree entry differences
2075 * in case tree entries have unexpected mode bits set.
2078 normalize_mode_for_comparison(mode_t mode)
2081 * For directories, the only relevant bit is the IFDIR bit.
2082 * This allows us to detect paths changing from a directory
2083 * to a file and vice versa.
2086 return mode & S_IFDIR;
2089 * For symlinks, the only relevant bit is the IFLNK bit.
2090 * This allows us to detect paths changing from a symlinks
2091 * to a file or directory and vice versa.
2094 return mode & S_IFLNK;
2096 /* For files, the only change we care about is the executable bit. */
2097 return mode & S_IXUSR;
2100 const struct got_error *
2101 got_object_tree_path_changed(int *changed,
2102 struct got_tree_object *tree01, struct got_tree_object *tree02,
2103 const char *path, struct got_repository *repo)
2105 const struct got_error *err = NULL;
2106 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2107 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2108 const char *seg, *s;
2113 /* We not do support comparing the root path. */
2114 if (got_path_is_root_dir(path))
2115 return got_error_path(path, GOT_ERR_BAD_PATH);
2125 struct got_tree_object *next_tree1, *next_tree2;
2126 mode_t mode1, mode2;
2135 te1 = find_entry_by_name(tree1, seg, seglen);
2137 err = got_error(GOT_ERR_NO_OBJ);
2142 te2 = find_entry_by_name(tree2, seg, seglen);
2145 mode1 = normalize_mode_for_comparison(te1->mode);
2146 mode2 = normalize_mode_for_comparison(te2->mode);
2147 if (mode1 != mode2) {
2152 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2158 if (*s == '\0') { /* final path element */
2167 err = got_object_open_as_tree(&next_tree1, repo,
2172 if (tree1 != tree01)
2173 got_object_tree_close(tree1);
2177 err = got_object_open_as_tree(&next_tree2, repo,
2182 if (tree2 != tree02)
2183 got_object_tree_close(tree2);
2186 if (tree2 != tree02)
2187 got_object_tree_close(tree2);
2193 if (tree1 && tree1 != tree01)
2194 got_object_tree_close(tree1);
2195 if (tree2 && tree2 != tree02)
2196 got_object_tree_close(tree2);
2200 const struct got_error *
2201 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2202 struct got_tree_entry *te)
2204 const struct got_error *err = NULL;
2206 *new_te = calloc(1, sizeof(**new_te));
2207 if (*new_te == NULL)
2208 return got_error_from_errno("calloc");
2210 (*new_te)->mode = te->mode;
2211 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2212 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2217 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2219 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2223 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2225 /* S_IFDIR check avoids confusing symlinks with submodules. */
2226 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2229 static const struct got_error *
2230 resolve_symlink(char **link_target, const char *path,
2231 struct got_commit_object *commit, struct got_repository *repo)
2233 const struct got_error *err = NULL;
2235 char *name, *parent_path = NULL;
2236 struct got_object_id *tree_obj_id = NULL;
2237 struct got_tree_object *tree = NULL;
2238 struct got_tree_entry *te = NULL;
2240 *link_target = NULL;
2242 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2243 return got_error(GOT_ERR_NO_SPACE);
2245 name = basename(buf);
2247 return got_error_from_errno2("basename", path);
2249 err = got_path_dirname(&parent_path, path);
2253 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2256 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2257 /* Display the complete path in error message. */
2258 err = got_error_path(path, err->code);
2263 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2267 te = got_object_tree_find_entry(tree, name);
2269 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2273 if (got_object_tree_entry_is_symlink(te)) {
2274 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2277 if (!got_path_is_absolute(*link_target)) {
2279 if (asprintf(&abspath, "%s/%s", parent_path,
2280 *link_target) == -1) {
2281 err = got_error_from_errno("asprintf");
2285 *link_target = malloc(PATH_MAX);
2286 if (*link_target == NULL) {
2287 err = got_error_from_errno("malloc");
2290 err = got_canonpath(abspath, *link_target, PATH_MAX);
2299 got_object_tree_close(tree);
2302 *link_target = NULL;
2307 const struct got_error *
2308 got_object_resolve_symlinks(char **link_target, const char *path,
2309 struct got_commit_object *commit, struct got_repository *repo)
2311 const struct got_error *err = NULL;
2312 char *next_target = NULL;
2313 int max_recursion = 40; /* matches Git */
2315 *link_target = NULL;
2318 err = resolve_symlink(&next_target,
2319 *link_target ? *link_target : path, commit, repo);
2324 if (--max_recursion == 0) {
2325 err = got_error_path(path, GOT_ERR_RECURSION);
2326 *link_target = NULL;
2329 *link_target = next_target;
2331 } while (next_target);
2336 const struct got_error *
2337 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2338 struct got_object_id *commit_id, const char *path,
2339 struct got_repository *repo)
2341 const struct got_error *err = NULL;
2342 struct got_pack *pack = NULL;
2343 struct got_packidx *packidx = NULL;
2344 char *path_packfile = NULL;
2345 struct got_commit_object *changed_commit = NULL;
2346 struct got_object_id *changed_commit_id = NULL;
2349 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2351 if (err->code != GOT_ERR_NO_OBJ)
2356 err = got_packidx_get_packfile_path(&path_packfile,
2357 packidx->path_packidx);
2361 pack = got_repo_get_cached_pack(repo, path_packfile);
2363 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2368 if (pack->privsep_child == NULL) {
2369 err = start_pack_privsep_child(pack, packidx);
2374 err = got_privsep_send_commit_traversal_request(
2375 pack->privsep_child->ibuf, commit_id, idx, path);
2379 err = got_privsep_recv_traversed_commits(&changed_commit,
2380 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2384 if (changed_commit) {
2386 * Cache the commit in which the path was changed.
2387 * This commit might be opened again soon.
2389 changed_commit->refcnt++;
2390 err = got_repo_cache_commit(repo, changed_commit_id,
2392 got_object_commit_close(changed_commit);
2395 free(path_packfile);
2396 free(changed_commit_id);