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))
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 struct got_object_id *
66 got_object_get_id(struct got_object *obj)
71 const struct got_error *
72 got_object_get_id_str(char **outbuf, struct got_object *obj)
74 return got_object_id_str(outbuf, &obj->id);
77 const struct got_error *
78 got_object_get_type(int *type, struct got_repository *repo,
79 struct got_object_id *id)
81 const struct got_error *err = NULL;
82 struct got_object *obj;
84 err = got_object_open(&obj, repo, id);
89 case GOT_OBJ_TYPE_COMMIT:
90 case GOT_OBJ_TYPE_TREE:
91 case GOT_OBJ_TYPE_BLOB:
92 case GOT_OBJ_TYPE_TAG:
96 err = got_error(GOT_ERR_OBJ_TYPE);
100 got_object_close(obj);
104 const struct got_error *
105 got_object_get_path(char **path, struct got_object_id *id,
106 struct got_repository *repo)
108 const struct got_error *err = NULL;
114 path_objects = got_repo_get_path_objects(repo);
115 if (path_objects == NULL)
116 return got_error_from_errno("got_repo_get_path_objects");
118 err = got_object_id_str(&hex, id);
122 if (asprintf(path, "%s/%.2x/%s", path_objects,
123 id->sha1[0], hex + 2) == -1)
124 err = got_error_from_errno("asprintf");
132 const struct got_error *
133 got_object_open_loose_fd(int *fd, struct got_object_id *id,
134 struct got_repository *repo)
136 const struct got_error *err = NULL;
139 err = got_object_get_path(&path, id, repo);
142 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
144 err = got_error_from_errno2("open", path);
152 static const struct got_error *
153 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
154 struct got_object_id *id)
156 const struct got_error *err = NULL;
157 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
159 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
163 err = got_privsep_recv_obj(obj, ibuf);
167 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
172 /* Create temporary files used during delta application. */
173 static const struct got_error *
174 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
176 const struct got_error *err;
177 int basefd = -1, accumfd = -1;
180 * For performance reasons, the child will keep reusing the
181 * same temporary files during every object request.
182 * Opening and closing new files for every object request is
183 * too expensive during operations such as 'gotadmin pack'.
185 if (pack->child_has_tempfiles)
188 basefd = dup(pack->basefd);
190 return got_error_from_errno("dup");
192 accumfd = dup(pack->accumfd);
194 err = got_error_from_errno("dup");
198 err = got_privsep_send_tmpfd(ibuf, basefd);
202 err = got_privsep_send_tmpfd(ibuf, accumfd);
210 pack->child_has_tempfiles = 1;
214 static const struct got_error *
215 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
216 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
218 const struct got_error *err = NULL;
219 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
222 err = pack_child_send_tempfiles(ibuf, pack);
226 outfd_child = dup(outfd);
227 if (outfd_child == -1)
228 return got_error_from_errno("dup");
230 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
236 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
240 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
248 set_max_datasize(void)
252 if (getrlimit(RLIMIT_DATA, &rl) != 0)
255 rl.rlim_cur = rl.rlim_max;
256 setrlimit(RLIMIT_DATA, &rl);
259 static const struct got_error *
260 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
262 const struct got_error *err = NULL;
265 struct imsgbuf *ibuf;
267 ibuf = calloc(1, sizeof(*ibuf));
269 return got_error_from_errno("calloc");
271 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
272 if (pack->privsep_child == NULL) {
273 err = got_error_from_errno("calloc");
277 pack->child_has_tempfiles = 0;
278 pack->child_has_delta_outfd = 0;
280 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
281 err = got_error_from_errno("socketpair");
287 err = got_error_from_errno("fork");
289 } else if (pid == 0) {
291 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
292 pack->path_packfile);
296 if (close(imsg_fds[1]) == -1)
297 return got_error_from_errno("close");
298 pack->privsep_child->imsg_fd = imsg_fds[0];
299 pack->privsep_child->pid = pid;
300 imsg_init(ibuf, imsg_fds[0]);
301 pack->privsep_child->ibuf = ibuf;
303 err = got_privsep_init_pack_child(ibuf, pack, packidx);
305 const struct got_error *child_err;
306 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
307 child_err = got_privsep_wait_for_child(
308 pack->privsep_child->pid);
309 if (child_err && err == NULL)
315 free(pack->privsep_child);
316 pack->privsep_child = NULL;
321 static const struct got_error *
322 read_packed_object_privsep(struct got_object **obj,
323 struct got_repository *repo, struct got_pack *pack,
324 struct got_packidx *packidx, int idx, struct got_object_id *id)
326 const struct got_error *err = NULL;
328 if (pack->privsep_child == NULL) {
329 err = start_pack_privsep_child(pack, packidx);
334 return request_packed_object(obj, pack, idx, id);
337 static const struct got_error *
338 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
339 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
340 struct got_object_id *id)
342 const struct got_error *err = NULL;
344 if (pack->privsep_child == NULL) {
345 err = start_pack_privsep_child(pack, packidx);
350 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
354 const struct got_error *
355 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
356 struct got_repository *repo)
358 const struct got_error *err = NULL;
359 struct got_pack *pack = NULL;
360 struct got_packidx *packidx = NULL;
364 err = got_repo_search_packidx(&packidx, &idx, repo, id);
368 err = got_packidx_get_packfile_path(&path_packfile,
369 packidx->path_packidx);
373 pack = got_repo_get_cached_pack(repo, path_packfile);
375 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
380 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
388 const struct got_error *
389 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
390 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
391 struct got_repository *repo)
393 return read_packed_object_privsep(obj, repo, pack, packidx,
397 const struct got_error *
398 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
399 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
400 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
401 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
402 struct got_repository *repo)
404 const struct got_error *err = NULL;
405 struct got_pack *pack = NULL;
411 *delta_compressed_size = 0;
413 *delta_out_offset = 0;
415 err = got_packidx_get_packfile_path(&path_packfile,
416 packidx->path_packidx);
420 pack = got_repo_get_cached_pack(repo, path_packfile);
422 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
427 if (pack->privsep_child == NULL) {
428 err = start_pack_privsep_child(pack, packidx);
433 if (!pack->child_has_delta_outfd) {
435 outfd_child = dup(delta_cache_fd);
436 if (outfd_child == -1)
437 return got_error_from_errno("dup");
438 err = got_privsep_send_raw_delta_outfd(
439 pack->privsep_child->ibuf, outfd_child);
442 pack->child_has_delta_outfd = 1;
445 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
450 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
451 delta_compressed_size, delta_offset, delta_out_offset, base_id,
452 pack->privsep_child->ibuf);
456 * XXX This function does not really belong in object.c. It is only here
457 * because it needs start_pack_privsep_child(); relevant code should
458 * probably be moved to pack.c/pack_create.c.
460 const struct got_error *
461 got_object_prepare_delta_reuse(struct got_pack **pack,
462 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
464 const struct got_error *err = NULL;
465 char *path_packfile = NULL;
467 err = got_packidx_get_packfile_path(&path_packfile,
468 packidx->path_packidx);
472 *pack = got_repo_get_cached_pack(repo, path_packfile);
474 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
478 if ((*pack)->privsep_child == NULL) {
479 err = start_pack_privsep_child(*pack, packidx);
484 if (!(*pack)->child_has_delta_outfd) {
486 outfd_child = dup(delta_outfd);
487 if (outfd_child == -1) {
488 err = got_error_from_errno("dup");
491 err = got_privsep_send_raw_delta_outfd(
492 (*pack)->privsep_child->ibuf, outfd_child);
495 (*pack)->child_has_delta_outfd = 1;
498 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
504 static const struct got_error *
505 request_object(struct got_object **obj, struct got_object_id *id,
506 struct got_repository *repo, int fd)
508 const struct got_error *err = NULL;
509 struct imsgbuf *ibuf;
511 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
513 err = got_privsep_send_obj_req(ibuf, fd, id);
517 return got_privsep_recv_obj(obj, ibuf);
520 static const struct got_error *
521 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
522 struct got_object_id *id, struct got_repository *repo, int infd)
524 const struct got_error *err = NULL;
525 struct imsgbuf *ibuf;
528 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
530 outfd_child = dup(outfd);
531 if (outfd_child == -1)
532 return got_error_from_errno("dup");
534 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
538 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
542 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
545 static const struct got_error *
546 start_read_object_child(struct got_repository *repo)
548 const struct got_error *err = NULL;
551 struct imsgbuf *ibuf;
553 ibuf = calloc(1, sizeof(*ibuf));
555 return got_error_from_errno("calloc");
557 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
558 err = got_error_from_errno("socketpair");
565 err = got_error_from_errno("fork");
570 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
575 if (close(imsg_fds[1]) == -1) {
576 err = got_error_from_errno("close");
581 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
583 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
584 imsg_init(ibuf, imsg_fds[0]);
585 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
590 const struct got_error *
591 got_object_read_header_privsep(struct got_object **obj,
592 struct got_object_id *id, struct got_repository *repo, int obj_fd)
594 const struct got_error *err;
596 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
597 return request_object(obj, id, repo, obj_fd);
599 err = start_read_object_child(repo);
605 return request_object(obj, id, repo, obj_fd);
608 static const struct got_error *
609 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
610 int outfd, struct got_object_id *id, struct got_repository *repo,
613 const struct got_error *err;
615 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
616 return request_raw_object(outbuf, size, hdrlen, outfd, id,
619 err = start_read_object_child(repo);
623 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
627 const struct got_error *
628 got_object_open(struct got_object **obj, struct got_repository *repo,
629 struct got_object_id *id)
631 const struct got_error *err = NULL;
634 *obj = got_repo_get_cached_object(repo, id);
640 err = got_object_open_packed(obj, id, repo);
641 if (err && err->code != GOT_ERR_NO_OBJ)
645 return got_repo_cache_object(repo, id, *obj);
648 err = got_object_open_loose_fd(&fd, id, repo);
650 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
651 err = got_error_no_obj(id);
655 err = got_object_read_header_privsep(obj, id, repo, fd);
659 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
662 return got_repo_cache_object(repo, id, *obj);
665 /* *outfd must be initialized to -1 by caller */
666 const struct got_error *
667 got_object_raw_open(struct got_raw_object **obj, int *outfd,
668 struct got_repository *repo, struct got_object_id *id)
670 const struct got_error *err = NULL;
671 struct got_packidx *packidx = NULL;
673 uint8_t *outbuf = NULL;
676 char *path_packfile = NULL;
678 *obj = got_repo_get_cached_raw_object(repo, id);
685 *outfd = got_opentempfd();
687 return got_error_from_errno("got_opentempfd");
690 err = got_repo_search_packidx(&packidx, &idx, repo, id);
692 struct got_pack *pack = NULL;
694 err = got_packidx_get_packfile_path(&path_packfile,
695 packidx->path_packidx);
699 pack = got_repo_get_cached_pack(repo, path_packfile);
701 err = got_repo_cache_pack(&pack, repo, path_packfile,
706 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
707 *outfd, pack, packidx, idx, id);
710 } else if (err->code == GOT_ERR_NO_OBJ) {
713 err = got_object_open_loose_fd(&fd, id, repo);
716 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
722 *obj = calloc(1, sizeof(**obj));
724 err = got_error_from_errno("calloc");
730 (*obj)->data = outbuf;
733 if (fstat(*outfd, &sb) == -1) {
734 err = got_error_from_errno("fstat");
738 if (sb.st_size != hdrlen + size) {
739 err = got_error(GOT_ERR_PRIVSEP_LEN);
742 #ifndef GOT_PACK_NO_MMAP
743 if (hdrlen + size > 0) {
744 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
745 MAP_PRIVATE, *outfd, 0);
746 if ((*obj)->data == MAP_FAILED) {
747 if (errno != ENOMEM) {
748 err = got_error_from_errno("mmap");
759 (*obj)->f = fdopen(*outfd, "r");
760 if ((*obj)->f == NULL) {
761 err = got_error_from_errno("fdopen");
767 (*obj)->hdrlen = hdrlen;
769 err = got_repo_cache_raw_object(repo, id, *obj);
774 got_object_raw_close(*obj);
783 const struct got_error *
784 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
787 struct got_object_id id;
789 if (!got_parse_sha1_digest(id.sha1, id_str))
790 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
792 return got_object_open(obj, repo, &id);
795 const struct got_error *
796 got_object_resolve_id_str(struct got_object_id **id,
797 struct got_repository *repo, const char *id_str)
799 const struct got_error *err = NULL;
800 struct got_object *obj;
802 err = got_object_open_by_id_str(&obj, repo, id_str);
806 *id = got_object_id_dup(got_object_get_id(obj));
807 got_object_close(obj);
809 return got_error_from_errno("got_object_id_dup");
814 static const struct got_error *
815 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
816 int pack_idx, struct got_object_id *id)
818 const struct got_error *err = NULL;
820 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
825 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
829 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
833 static const struct got_error *
834 read_packed_commit_privsep(struct got_commit_object **commit,
835 struct got_pack *pack, struct got_packidx *packidx, int idx,
836 struct got_object_id *id)
838 const struct got_error *err = NULL;
840 if (pack->privsep_child)
841 return request_packed_commit(commit, pack, idx, id);
843 err = start_pack_privsep_child(pack, packidx);
847 return request_packed_commit(commit, pack, idx, id);
850 static const struct got_error *
851 request_commit(struct got_commit_object **commit, struct got_repository *repo,
852 int fd, struct got_object_id *id)
854 const struct got_error *err = NULL;
855 struct imsgbuf *ibuf;
857 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
859 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
863 return got_privsep_recv_commit(commit, ibuf);
866 static const struct got_error *
867 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
868 struct got_object_id *id, struct got_repository *repo)
870 const struct got_error *err;
873 struct imsgbuf *ibuf;
875 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
876 return request_commit(commit, repo, obj_fd, id);
878 ibuf = calloc(1, sizeof(*ibuf));
880 return got_error_from_errno("calloc");
882 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
883 err = got_error_from_errno("socketpair");
890 err = got_error_from_errno("fork");
895 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
900 if (close(imsg_fds[1]) == -1) {
901 err = got_error_from_errno("close");
905 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
907 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
908 imsg_init(ibuf, imsg_fds[0]);
909 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
911 return request_commit(commit, repo, obj_fd, id);
915 static const struct got_error *
916 open_commit(struct got_commit_object **commit,
917 struct got_repository *repo, struct got_object_id *id, int check_cache)
919 const struct got_error *err = NULL;
920 struct got_packidx *packidx = NULL;
922 char *path_packfile = NULL;
925 *commit = got_repo_get_cached_commit(repo, id);
926 if (*commit != NULL) {
933 err = got_repo_search_packidx(&packidx, &idx, repo, id);
935 struct got_pack *pack = NULL;
937 err = got_packidx_get_packfile_path(&path_packfile,
938 packidx->path_packidx);
942 pack = got_repo_get_cached_pack(repo, path_packfile);
944 err = got_repo_cache_pack(&pack, repo, path_packfile,
949 err = read_packed_commit_privsep(commit, pack,
951 } else if (err->code == GOT_ERR_NO_OBJ) {
954 err = got_object_open_loose_fd(&fd, id, repo);
957 err = read_commit_privsep(commit, fd, id, repo);
962 err = got_repo_cache_commit(repo, id, *commit);
969 const struct got_error *
970 got_object_open_as_commit(struct got_commit_object **commit,
971 struct got_repository *repo, struct got_object_id *id)
973 *commit = got_repo_get_cached_commit(repo, id);
974 if (*commit != NULL) {
979 return open_commit(commit, repo, id, 0);
982 const struct got_error *
983 got_object_commit_open(struct got_commit_object **commit,
984 struct got_repository *repo, struct got_object *obj)
986 return open_commit(commit, repo, got_object_get_id(obj), 1);
989 const struct got_error *
990 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
992 *qid = calloc(1, sizeof(**qid));
994 return got_error_from_errno("calloc");
996 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
1000 const struct got_error *
1001 got_object_id_queue_copy(const struct got_object_id_queue *src,
1002 struct got_object_id_queue *dest)
1004 const struct got_error *err;
1005 struct got_object_qid *qid;
1007 STAILQ_FOREACH(qid, src, entry) {
1008 struct got_object_qid *new;
1010 * Deep-copy the object ID only. Let the caller deal
1011 * with setting up the new->data pointer if needed.
1013 err = got_object_qid_alloc(&new, &qid->id);
1015 got_object_id_queue_free(dest);
1018 STAILQ_INSERT_TAIL(dest, new, entry);
1024 static const struct got_error *
1025 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1026 int pack_idx, struct got_object_id *id)
1028 const struct got_error *err = NULL;
1030 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1035 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1038 static const struct got_error *
1039 read_packed_tree_privsep(struct got_tree_object **tree,
1040 struct got_pack *pack, struct got_packidx *packidx, int idx,
1041 struct got_object_id *id)
1043 const struct got_error *err = NULL;
1045 if (pack->privsep_child)
1046 return request_packed_tree(tree, pack, idx, id);
1048 err = start_pack_privsep_child(pack, packidx);
1052 return request_packed_tree(tree, pack, idx, id);
1055 static const struct got_error *
1056 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1057 int fd, struct got_object_id *id)
1059 const struct got_error *err = NULL;
1060 struct imsgbuf *ibuf;
1062 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1064 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1068 return got_privsep_recv_tree(tree, ibuf);
1071 const struct got_error *
1072 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1073 struct got_object_id *id, struct got_repository *repo)
1075 const struct got_error *err;
1078 struct imsgbuf *ibuf;
1080 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1081 return request_tree(tree, repo, obj_fd, id);
1083 ibuf = calloc(1, sizeof(*ibuf));
1085 return got_error_from_errno("calloc");
1087 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1088 err = got_error_from_errno("socketpair");
1095 err = got_error_from_errno("fork");
1099 else if (pid == 0) {
1100 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1105 if (close(imsg_fds[1]) == -1) {
1106 err = got_error_from_errno("close");
1110 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1112 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1113 imsg_init(ibuf, imsg_fds[0]);
1114 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1117 return request_tree(tree, repo, obj_fd, id);
1120 static const struct got_error *
1121 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1122 struct got_object_id *id, int check_cache)
1124 const struct got_error *err = NULL;
1125 struct got_packidx *packidx = NULL;
1127 char *path_packfile = NULL;
1130 *tree = got_repo_get_cached_tree(repo, id);
1131 if (*tree != NULL) {
1138 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1140 struct got_pack *pack = NULL;
1142 err = got_packidx_get_packfile_path(&path_packfile,
1143 packidx->path_packidx);
1147 pack = got_repo_get_cached_pack(repo, path_packfile);
1149 err = got_repo_cache_pack(&pack, repo, path_packfile,
1154 err = read_packed_tree_privsep(tree, pack,
1156 } else if (err->code == GOT_ERR_NO_OBJ) {
1159 err = got_object_open_loose_fd(&fd, id, repo);
1162 err = read_tree_privsep(tree, fd, id, repo);
1167 err = got_repo_cache_tree(repo, id, *tree);
1170 free(path_packfile);
1174 const struct got_error *
1175 got_object_open_as_tree(struct got_tree_object **tree,
1176 struct got_repository *repo, struct got_object_id *id)
1178 *tree = got_repo_get_cached_tree(repo, id);
1179 if (*tree != NULL) {
1184 return open_tree(tree, repo, id, 0);
1187 const struct got_error *
1188 got_object_tree_open(struct got_tree_object **tree,
1189 struct got_repository *repo, struct got_object *obj)
1191 return open_tree(tree, repo, got_object_get_id(obj), 1);
1195 got_object_tree_get_nentries(struct got_tree_object *tree)
1197 return tree->nentries;
1200 struct got_tree_entry *
1201 got_object_tree_get_first_entry(struct got_tree_object *tree)
1203 return got_object_tree_get_entry(tree, 0);
1206 struct got_tree_entry *
1207 got_object_tree_get_last_entry(struct got_tree_object *tree)
1209 return got_object_tree_get_entry(tree, tree->nentries - 1);
1212 struct got_tree_entry *
1213 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1215 if (i < 0 || i >= tree->nentries)
1217 return &tree->entries[i];
1221 got_tree_entry_get_mode(struct got_tree_entry *te)
1227 got_tree_entry_get_name(struct got_tree_entry *te)
1229 return &te->name[0];
1232 struct got_object_id *
1233 got_tree_entry_get_id(struct got_tree_entry *te)
1238 const struct got_error *
1239 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1241 const struct got_error *err = NULL;
1242 size_t len, totlen, hdrlen, offset;
1246 hdrlen = got_object_blob_get_hdrlen(blob);
1252 err = got_object_blob_read_block(&len, blob);
1259 totlen += len - hdrlen;
1260 p = realloc(*s, totlen + 1);
1262 err = got_error_from_errno("realloc");
1268 /* Skip blob object header first time around. */
1270 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1275 (*s)[totlen] = '\0';
1279 const struct got_error *
1280 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1281 struct got_repository *repo)
1283 const struct got_error *err = NULL;
1284 struct got_blob_object *blob = NULL;
1286 *link_target = NULL;
1288 if (!got_object_tree_entry_is_symlink(te))
1289 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1291 err = got_object_open_as_blob(&blob, repo,
1292 got_tree_entry_get_id(te), PATH_MAX);
1296 err = got_object_blob_read_to_str(link_target, blob);
1297 got_object_blob_close(blob);
1300 *link_target = NULL;
1306 got_tree_entry_get_index(struct got_tree_entry *te)
1311 struct got_tree_entry *
1312 got_tree_entry_get_next(struct got_tree_object *tree,
1313 struct got_tree_entry *te)
1315 return got_object_tree_get_entry(tree, te->idx + 1);
1318 struct got_tree_entry *
1319 got_tree_entry_get_prev(struct got_tree_object *tree,
1320 struct got_tree_entry *te)
1322 return got_object_tree_get_entry(tree, te->idx - 1);
1325 static const struct got_error *
1326 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1327 struct got_pack *pack, struct got_packidx *packidx, int idx,
1328 struct got_object_id *id)
1330 const struct got_error *err = NULL;
1331 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1334 err = pack_child_send_tempfiles(ibuf, pack);
1338 outfd_child = dup(outfd);
1339 if (outfd_child == -1)
1340 return got_error_from_errno("dup");
1342 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1346 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1352 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1353 pack->privsep_child->ibuf);
1357 if (lseek(outfd, SEEK_SET, 0) == -1)
1358 err = got_error_from_errno("lseek");
1363 static const struct got_error *
1364 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1365 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1366 struct got_object_id *id)
1368 const struct got_error *err = NULL;
1370 if (pack->privsep_child == NULL) {
1371 err = start_pack_privsep_child(pack, packidx);
1376 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1380 static const struct got_error *
1381 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1382 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1384 const struct got_error *err = NULL;
1387 outfd_child = dup(outfd);
1388 if (outfd_child == -1)
1389 return got_error_from_errno("dup");
1391 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1395 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1399 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1403 if (lseek(outfd, SEEK_SET, 0) == -1)
1404 return got_error_from_errno("lseek");
1409 static const struct got_error *
1410 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1411 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1413 const struct got_error *err;
1416 struct imsgbuf *ibuf;
1418 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1419 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1420 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1424 ibuf = calloc(1, sizeof(*ibuf));
1426 return got_error_from_errno("calloc");
1428 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1429 err = got_error_from_errno("socketpair");
1436 err = got_error_from_errno("fork");
1440 else if (pid == 0) {
1441 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1446 if (close(imsg_fds[1]) == -1) {
1447 err = got_error_from_errno("close");
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1454 imsg_init(ibuf, imsg_fds[0]);
1455 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1457 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1460 static const struct got_error *
1461 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1462 struct got_object_id *id, size_t blocksize)
1464 const struct got_error *err = NULL;
1465 struct got_packidx *packidx = NULL;
1467 char *path_packfile = NULL;
1470 size_t size, hdrlen;
1473 *blob = calloc(1, sizeof(**blob));
1475 return got_error_from_errno("calloc");
1477 outfd = got_opentempfd();
1479 return got_error_from_errno("got_opentempfd");
1481 (*blob)->read_buf = malloc(blocksize);
1482 if ((*blob)->read_buf == NULL) {
1483 err = got_error_from_errno("malloc");
1487 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1489 struct got_pack *pack = NULL;
1491 err = got_packidx_get_packfile_path(&path_packfile,
1492 packidx->path_packidx);
1496 pack = got_repo_get_cached_pack(repo, path_packfile);
1498 err = got_repo_cache_pack(&pack, repo, path_packfile,
1503 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1504 pack, packidx, idx, id);
1505 } else if (err->code == GOT_ERR_NO_OBJ) {
1508 err = got_object_open_loose_fd(&infd, id, repo);
1511 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1517 if (hdrlen > size) {
1518 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1523 if (close(outfd) == -1 && err == NULL)
1524 err = got_error_from_errno("close");
1526 (*blob)->f = fmemopen(outbuf, size, "rb");
1527 if ((*blob)->f == NULL) {
1528 err = got_error_from_errno("fmemopen");
1532 (*blob)->data = outbuf;
1534 if (fstat(outfd, &sb) == -1) {
1535 err = got_error_from_errno("fstat");
1539 if (sb.st_size != size) {
1540 err = got_error(GOT_ERR_PRIVSEP_LEN);
1544 (*blob)->f = fdopen(outfd, "rb");
1545 if ((*blob)->f == NULL) {
1546 err = got_error_from_errno("fdopen");
1553 (*blob)->hdrlen = hdrlen;
1554 (*blob)->blocksize = blocksize;
1555 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1558 free(path_packfile);
1561 got_object_blob_close(*blob);
1563 } else if (outfd != -1)
1569 const struct got_error *
1570 got_object_open_as_blob(struct got_blob_object **blob,
1571 struct got_repository *repo, struct got_object_id *id,
1574 return open_blob(blob, repo, id, blocksize);
1577 const struct got_error *
1578 got_object_blob_open(struct got_blob_object **blob,
1579 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1581 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1584 const struct got_error *
1585 got_object_blob_close(struct got_blob_object *blob)
1587 const struct got_error *err = NULL;
1588 free(blob->read_buf);
1589 if (blob->f && fclose(blob->f) == EOF)
1590 err = got_error_from_errno("fclose");
1597 got_object_blob_rewind(struct got_blob_object *blob)
1604 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1606 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1610 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1612 return blob->hdrlen;
1616 got_object_blob_get_read_buf(struct got_blob_object *blob)
1618 return blob->read_buf;
1621 const struct got_error *
1622 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1626 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1627 if (n == 0 && ferror(blob->f))
1628 return got_ferror(blob->f, GOT_ERR_IO);
1633 const struct got_error *
1634 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1635 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1637 const struct got_error *err = NULL;
1638 size_t n, len, hdrlen;
1641 const int alloc_chunksz = 512;
1643 off_t off = 0, total_len = 0;
1646 *line_offsets = NULL;
1652 hdrlen = got_object_blob_get_hdrlen(blob);
1654 err = got_object_blob_read_block(&len, blob);
1659 buf = got_object_blob_get_read_buf(blob);
1662 if (line_offsets && *line_offsets == NULL) {
1663 /* Have some data but perhaps no '\n'. */
1665 nalloc = alloc_chunksz;
1666 *line_offsets = calloc(nalloc,
1667 sizeof(**line_offsets));
1668 if (*line_offsets == NULL)
1669 return got_error_from_errno("calloc");
1671 /* Skip forward over end of first line. */
1678 /* Scan '\n' offsets in remaining chunk of data. */
1680 if (buf[i] != '\n') {
1685 if (line_offsets && nalloc < *nlines) {
1686 size_t n = *nlines + alloc_chunksz;
1687 off_t *o = recallocarray(*line_offsets,
1688 nalloc, n, sizeof(**line_offsets));
1690 free(*line_offsets);
1691 *line_offsets = NULL;
1692 return got_error_from_errno(
1699 off = total_len + i - hdrlen + 1;
1700 (*line_offsets)[*nlines - 1] = off;
1705 /* Skip blob object header first time around. */
1706 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1707 if (n != len - hdrlen)
1708 return got_ferror(outfile, GOT_ERR_IO);
1709 total_len += len - hdrlen;
1713 if (fflush(outfile) != 0)
1714 return got_error_from_errno("fflush");
1718 *filesize = total_len;
1723 static const struct got_error *
1724 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1725 int pack_idx, struct got_object_id *id)
1727 const struct got_error *err = NULL;
1729 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1734 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1737 static const struct got_error *
1738 read_packed_tag_privsep(struct got_tag_object **tag,
1739 struct got_pack *pack, struct got_packidx *packidx, int idx,
1740 struct got_object_id *id)
1742 const struct got_error *err = NULL;
1744 if (pack->privsep_child)
1745 return request_packed_tag(tag, pack, idx, id);
1747 err = start_pack_privsep_child(pack, packidx);
1751 return request_packed_tag(tag, pack, idx, id);
1754 static const struct got_error *
1755 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1756 int fd, struct got_object_id *id)
1758 const struct got_error *err = NULL;
1759 struct imsgbuf *ibuf;
1761 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1763 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1767 return got_privsep_recv_tag(tag, ibuf);
1770 static const struct got_error *
1771 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1772 struct got_object_id *id, struct got_repository *repo)
1774 const struct got_error *err;
1777 struct imsgbuf *ibuf;
1779 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1780 return request_tag(tag, repo, obj_fd, id);
1782 ibuf = calloc(1, sizeof(*ibuf));
1784 return got_error_from_errno("calloc");
1786 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1787 err = got_error_from_errno("socketpair");
1794 err = got_error_from_errno("fork");
1798 else if (pid == 0) {
1799 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1804 if (close(imsg_fds[1]) == -1) {
1805 err = got_error_from_errno("close");
1809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1812 imsg_init(ibuf, imsg_fds[0]);
1813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1815 return request_tag(tag, repo, obj_fd, id);
1818 static const struct got_error *
1819 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1820 struct got_object_id *id, int check_cache)
1822 const struct got_error *err = NULL;
1823 struct got_packidx *packidx = NULL;
1825 char *path_packfile = NULL;
1826 struct got_object *obj = NULL;
1827 int obj_type = GOT_OBJ_TYPE_ANY;
1830 *tag = got_repo_get_cached_tag(repo, id);
1838 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1840 struct got_pack *pack = NULL;
1842 err = got_packidx_get_packfile_path(&path_packfile,
1843 packidx->path_packidx);
1847 pack = got_repo_get_cached_pack(repo, path_packfile);
1849 err = got_repo_cache_pack(&pack, repo, path_packfile,
1855 /* Beware of "lightweight" tags: Check object type first. */
1856 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1860 obj_type = obj->type;
1861 got_object_close(obj);
1862 if (obj_type != GOT_OBJ_TYPE_TAG) {
1863 err = got_error(GOT_ERR_OBJ_TYPE);
1866 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1867 } else if (err->code == GOT_ERR_NO_OBJ) {
1870 err = got_object_open_loose_fd(&fd, id, repo);
1873 err = got_object_read_header_privsep(&obj, id, repo, fd);
1876 obj_type = obj->type;
1877 got_object_close(obj);
1878 if (obj_type != GOT_OBJ_TYPE_TAG)
1879 return got_error(GOT_ERR_OBJ_TYPE);
1881 err = got_object_open_loose_fd(&fd, id, repo);
1884 err = read_tag_privsep(tag, fd, id, repo);
1889 err = got_repo_cache_tag(repo, id, *tag);
1892 free(path_packfile);
1896 const struct got_error *
1897 got_object_open_as_tag(struct got_tag_object **tag,
1898 struct got_repository *repo, struct got_object_id *id)
1900 *tag = got_repo_get_cached_tag(repo, id);
1906 return open_tag(tag, repo, id, 0);
1909 const struct got_error *
1910 got_object_tag_open(struct got_tag_object **tag,
1911 struct got_repository *repo, struct got_object *obj)
1913 return open_tag(tag, repo, got_object_get_id(obj), 1);
1917 got_object_tag_get_name(struct got_tag_object *tag)
1923 got_object_tag_get_object_type(struct got_tag_object *tag)
1925 return tag->obj_type;
1928 struct got_object_id *
1929 got_object_tag_get_object_id(struct got_tag_object *tag)
1935 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1937 return tag->tagger_time;
1941 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1943 return tag->tagger_gmtoff;
1947 got_object_tag_get_tagger(struct got_tag_object *tag)
1953 got_object_tag_get_message(struct got_tag_object *tag)
1958 static struct got_tree_entry *
1959 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1963 /* Note that tree entries are sorted in strncmp() order. */
1964 for (i = 0; i < tree->nentries; i++) {
1965 struct got_tree_entry *te = &tree->entries[i];
1966 int cmp = strncmp(te->name, name, len);
1971 if (te->name[len] == '\0')
1977 struct got_tree_entry *
1978 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1980 return find_entry_by_name(tree, name, strlen(name));
1983 const struct got_error *
1984 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1985 struct got_repository *repo, struct got_tree_object *tree,
1988 const struct got_error *err = NULL;
1989 struct got_tree_object *subtree = NULL;
1990 struct got_tree_entry *te = NULL;
1991 const char *seg, *s;
2003 struct got_tree_object *next_tree;
2012 te = find_entry_by_name(subtree, seg, seglen);
2014 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2025 err = got_object_open_as_tree(&next_tree, repo,
2030 if (subtree != tree)
2031 got_object_tree_close(subtree);
2032 subtree = next_tree;
2037 *id = got_object_id_dup(&te->id);
2039 return got_error_from_errno("got_object_id_dup");
2043 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2045 if (subtree && subtree != tree)
2046 got_object_tree_close(subtree);
2049 const struct got_error *
2050 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2051 struct got_commit_object *commit, const char *path)
2053 const struct got_error *err = NULL;
2054 struct got_tree_object *tree = NULL;
2058 /* Handle opening of root of commit's tree. */
2059 if (got_path_is_root_dir(path)) {
2060 *id = got_object_id_dup(commit->tree_id);
2062 err = got_error_from_errno("got_object_id_dup");
2064 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2067 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2071 got_object_tree_close(tree);
2076 * Normalize file mode bits to avoid false positive tree entry differences
2077 * in case tree entries have unexpected mode bits set.
2080 normalize_mode_for_comparison(mode_t mode)
2083 * For directories, the only relevant bit is the IFDIR bit.
2084 * This allows us to detect paths changing from a directory
2085 * to a file and vice versa.
2088 return mode & S_IFDIR;
2091 * For symlinks, the only relevant bit is the IFLNK bit.
2092 * This allows us to detect paths changing from a symlinks
2093 * to a file or directory and vice versa.
2096 return mode & S_IFLNK;
2098 /* For files, the only change we care about is the executable bit. */
2099 return mode & S_IXUSR;
2102 const struct got_error *
2103 got_object_tree_path_changed(int *changed,
2104 struct got_tree_object *tree01, struct got_tree_object *tree02,
2105 const char *path, struct got_repository *repo)
2107 const struct got_error *err = NULL;
2108 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2109 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2110 const char *seg, *s;
2115 /* We not do support comparing the root path. */
2116 if (got_path_is_root_dir(path))
2117 return got_error_path(path, GOT_ERR_BAD_PATH);
2127 struct got_tree_object *next_tree1, *next_tree2;
2128 mode_t mode1, mode2;
2137 te1 = find_entry_by_name(tree1, seg, seglen);
2139 err = got_error(GOT_ERR_NO_OBJ);
2144 te2 = find_entry_by_name(tree2, seg, seglen);
2147 mode1 = normalize_mode_for_comparison(te1->mode);
2148 mode2 = normalize_mode_for_comparison(te2->mode);
2149 if (mode1 != mode2) {
2154 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2160 if (*s == '\0') { /* final path element */
2169 err = got_object_open_as_tree(&next_tree1, repo,
2174 if (tree1 != tree01)
2175 got_object_tree_close(tree1);
2179 err = got_object_open_as_tree(&next_tree2, repo,
2184 if (tree2 != tree02)
2185 got_object_tree_close(tree2);
2188 if (tree2 != tree02)
2189 got_object_tree_close(tree2);
2195 if (tree1 && tree1 != tree01)
2196 got_object_tree_close(tree1);
2197 if (tree2 && tree2 != tree02)
2198 got_object_tree_close(tree2);
2202 const struct got_error *
2203 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2204 struct got_tree_entry *te)
2206 const struct got_error *err = NULL;
2208 *new_te = calloc(1, sizeof(**new_te));
2209 if (*new_te == NULL)
2210 return got_error_from_errno("calloc");
2212 (*new_te)->mode = te->mode;
2213 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2214 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2219 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2221 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2225 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2227 /* S_IFDIR check avoids confusing symlinks with submodules. */
2228 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2231 static const struct got_error *
2232 resolve_symlink(char **link_target, const char *path,
2233 struct got_commit_object *commit, struct got_repository *repo)
2235 const struct got_error *err = NULL;
2237 char *name, *parent_path = NULL;
2238 struct got_object_id *tree_obj_id = NULL;
2239 struct got_tree_object *tree = NULL;
2240 struct got_tree_entry *te = NULL;
2242 *link_target = NULL;
2244 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2245 return got_error(GOT_ERR_NO_SPACE);
2247 name = basename(buf);
2249 return got_error_from_errno2("basename", path);
2251 err = got_path_dirname(&parent_path, path);
2255 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2258 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2259 /* Display the complete path in error message. */
2260 err = got_error_path(path, err->code);
2265 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2269 te = got_object_tree_find_entry(tree, name);
2271 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2275 if (got_object_tree_entry_is_symlink(te)) {
2276 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2279 if (!got_path_is_absolute(*link_target)) {
2281 if (asprintf(&abspath, "%s/%s", parent_path,
2282 *link_target) == -1) {
2283 err = got_error_from_errno("asprintf");
2287 *link_target = malloc(PATH_MAX);
2288 if (*link_target == NULL) {
2289 err = got_error_from_errno("malloc");
2292 err = got_canonpath(abspath, *link_target, PATH_MAX);
2301 got_object_tree_close(tree);
2304 *link_target = NULL;
2309 const struct got_error *
2310 got_object_resolve_symlinks(char **link_target, const char *path,
2311 struct got_commit_object *commit, struct got_repository *repo)
2313 const struct got_error *err = NULL;
2314 char *next_target = NULL;
2315 int max_recursion = 40; /* matches Git */
2317 *link_target = NULL;
2320 err = resolve_symlink(&next_target,
2321 *link_target ? *link_target : path, commit, repo);
2326 if (--max_recursion == 0) {
2327 err = got_error_path(path, GOT_ERR_RECURSION);
2328 *link_target = NULL;
2331 *link_target = next_target;
2333 } while (next_target);
2338 const struct got_error *
2339 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2340 struct got_object_id *commit_id, const char *path,
2341 struct got_repository *repo)
2343 const struct got_error *err = NULL;
2344 struct got_pack *pack = NULL;
2345 struct got_packidx *packidx = NULL;
2346 char *path_packfile = NULL;
2347 struct got_commit_object *changed_commit = NULL;
2348 struct got_object_id *changed_commit_id = NULL;
2351 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2353 if (err->code != GOT_ERR_NO_OBJ)
2358 err = got_packidx_get_packfile_path(&path_packfile,
2359 packidx->path_packidx);
2363 pack = got_repo_get_cached_pack(repo, path_packfile);
2365 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2370 if (pack->privsep_child == NULL) {
2371 err = start_pack_privsep_child(pack, packidx);
2376 err = got_privsep_send_commit_traversal_request(
2377 pack->privsep_child->ibuf, commit_id, idx, path);
2381 err = got_privsep_recv_traversed_commits(&changed_commit,
2382 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2386 if (changed_commit) {
2388 * Cache the commit in which the path was changed.
2389 * This commit might be opened again soon.
2391 changed_commit->refcnt++;
2392 err = got_repo_cache_commit(repo, changed_commit_id,
2394 got_object_commit_close(changed_commit);
2397 free(path_packfile);
2398 free(changed_commit_id);
2402 const struct got_error *
2403 got_object_enumerate(got_object_enumerate_commit_cb cb_commit,
2404 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2405 struct got_object_id **ours, int nours,
2406 struct got_object_id **theirs, int ntheirs,
2407 struct got_packidx *packidx, struct got_repository *repo)
2409 const struct got_error *err = NULL;
2410 struct got_pack *pack;
2411 char *path_packfile = NULL;
2413 err = got_packidx_get_packfile_path(&path_packfile,
2414 packidx->path_packidx);
2418 pack = got_repo_get_cached_pack(repo, path_packfile);
2420 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2425 if (pack->privsep_child == NULL) {
2426 err = start_pack_privsep_child(pack, packidx);
2431 err = got_privsep_send_object_enumeration_request(
2432 pack->privsep_child->ibuf);
2436 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2440 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2444 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2448 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2452 err = got_privsep_recv_enumerated_objects(pack->privsep_child->ibuf,
2453 cb_commit, cb_tree, cb_arg, repo);
2455 free(path_packfile);