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;
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 = got_opentempfd();
188 return got_error_from_errno("got_opentempfd");
190 err = got_privsep_send_tmpfd(ibuf, basefd);
194 accumfd = got_opentempfd();
196 return got_error_from_errno("got_opentempfd");
198 err = got_privsep_send_tmpfd(ibuf, accumfd);
202 pack->child_has_tempfiles = 1;
206 static const struct got_error *
207 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
208 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
210 const struct got_error *err = NULL;
211 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
214 err = pack_child_send_tempfiles(ibuf, pack);
218 outfd_child = dup(outfd);
219 if (outfd_child == -1)
220 return got_error_from_errno("dup");
222 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
228 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
232 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
240 set_max_datasize(void)
244 if (getrlimit(RLIMIT_DATA, &rl) != 0)
247 rl.rlim_cur = rl.rlim_max;
248 setrlimit(RLIMIT_DATA, &rl);
251 static const struct got_error *
252 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
254 const struct got_error *err = NULL;
257 struct imsgbuf *ibuf;
259 ibuf = calloc(1, sizeof(*ibuf));
261 return got_error_from_errno("calloc");
263 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
264 if (pack->privsep_child == NULL) {
265 err = got_error_from_errno("calloc");
269 pack->child_has_tempfiles = 0;
270 pack->child_has_delta_outfd = 0;
272 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
273 err = got_error_from_errno("socketpair");
279 err = got_error_from_errno("fork");
281 } else if (pid == 0) {
283 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
284 pack->path_packfile);
288 if (close(imsg_fds[1]) == -1)
289 return got_error_from_errno("close");
290 pack->privsep_child->imsg_fd = imsg_fds[0];
291 pack->privsep_child->pid = pid;
292 imsg_init(ibuf, imsg_fds[0]);
293 pack->privsep_child->ibuf = ibuf;
295 err = got_privsep_init_pack_child(ibuf, pack, packidx);
297 const struct got_error *child_err;
298 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
299 child_err = got_privsep_wait_for_child(
300 pack->privsep_child->pid);
301 if (child_err && err == NULL)
307 free(pack->privsep_child);
308 pack->privsep_child = NULL;
313 static const struct got_error *
314 read_packed_object_privsep(struct got_object **obj,
315 struct got_repository *repo, struct got_pack *pack,
316 struct got_packidx *packidx, int idx, struct got_object_id *id)
318 const struct got_error *err = NULL;
320 if (pack->privsep_child == NULL) {
321 err = start_pack_privsep_child(pack, packidx);
326 return request_packed_object(obj, pack, idx, id);
329 static const struct got_error *
330 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
331 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
332 struct got_object_id *id)
334 const struct got_error *err = NULL;
336 if (pack->privsep_child == NULL) {
337 err = start_pack_privsep_child(pack, packidx);
342 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
346 const struct got_error *
347 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
348 struct got_repository *repo)
350 const struct got_error *err = NULL;
351 struct got_pack *pack = NULL;
352 struct got_packidx *packidx = NULL;
356 err = got_repo_search_packidx(&packidx, &idx, repo, id);
360 err = got_packidx_get_packfile_path(&path_packfile,
361 packidx->path_packidx);
365 pack = got_repo_get_cached_pack(repo, path_packfile);
367 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
372 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
380 const struct got_error *
381 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
382 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
383 struct got_repository *repo)
385 return read_packed_object_privsep(obj, repo, pack, packidx,
389 const struct got_error *
390 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
391 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
392 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
393 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
394 struct got_repository *repo)
396 const struct got_error *err = NULL;
397 struct got_pack *pack = NULL;
403 *delta_compressed_size = 0;
405 *delta_out_offset = 0;
407 err = got_packidx_get_packfile_path(&path_packfile,
408 packidx->path_packidx);
412 pack = got_repo_get_cached_pack(repo, path_packfile);
414 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
419 if (pack->privsep_child == NULL) {
420 err = start_pack_privsep_child(pack, packidx);
425 if (!pack->child_has_delta_outfd) {
427 outfd_child = dup(delta_cache_fd);
428 if (outfd_child == -1)
429 return got_error_from_errno("dup");
430 err = got_privsep_send_raw_delta_outfd(
431 pack->privsep_child->ibuf, outfd_child);
434 pack->child_has_delta_outfd = 1;
437 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
442 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
443 delta_compressed_size, delta_offset, delta_out_offset, base_id,
444 pack->privsep_child->ibuf);
448 * XXX This function does not really belong in object.c. It is only here
449 * because it needs start_pack_privsep_child(); relevant code should
450 * probably be moved to pack.c/pack_create.c.
452 const struct got_error *
453 got_object_prepare_delta_reuse(struct got_pack **pack,
454 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
456 const struct got_error *err = NULL;
457 char *path_packfile = NULL;
459 err = got_packidx_get_packfile_path(&path_packfile,
460 packidx->path_packidx);
464 *pack = got_repo_get_cached_pack(repo, path_packfile);
466 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
470 if ((*pack)->privsep_child == NULL) {
471 err = start_pack_privsep_child(*pack, packidx);
476 if (!(*pack)->child_has_delta_outfd) {
478 outfd_child = dup(delta_outfd);
479 if (outfd_child == -1) {
480 err = got_error_from_errno("dup");
483 err = got_privsep_send_raw_delta_outfd(
484 (*pack)->privsep_child->ibuf, outfd_child);
487 (*pack)->child_has_delta_outfd = 1;
490 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
496 static const struct got_error *
497 request_object(struct got_object **obj, struct got_object_id *id,
498 struct got_repository *repo, int fd)
500 const struct got_error *err = NULL;
501 struct imsgbuf *ibuf;
503 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
505 err = got_privsep_send_obj_req(ibuf, fd, id);
509 return got_privsep_recv_obj(obj, ibuf);
512 static const struct got_error *
513 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
514 struct got_object_id *id, struct got_repository *repo, int infd)
516 const struct got_error *err = NULL;
517 struct imsgbuf *ibuf;
520 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
522 outfd_child = dup(outfd);
523 if (outfd_child == -1)
524 return got_error_from_errno("dup");
526 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
530 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
534 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
537 static const struct got_error *
538 start_read_object_child(struct got_repository *repo)
540 const struct got_error *err = NULL;
543 struct imsgbuf *ibuf;
545 ibuf = calloc(1, sizeof(*ibuf));
547 return got_error_from_errno("calloc");
549 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
550 err = got_error_from_errno("socketpair");
557 err = got_error_from_errno("fork");
562 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
567 if (close(imsg_fds[1]) == -1) {
568 err = got_error_from_errno("close");
573 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
575 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
576 imsg_init(ibuf, imsg_fds[0]);
577 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
582 const struct got_error *
583 got_object_read_header_privsep(struct got_object **obj,
584 struct got_object_id *id, struct got_repository *repo, int obj_fd)
586 const struct got_error *err;
588 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
589 return request_object(obj, id, repo, obj_fd);
591 err = start_read_object_child(repo);
597 return request_object(obj, id, repo, obj_fd);
600 static const struct got_error *
601 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
602 int outfd, struct got_object_id *id, struct got_repository *repo,
605 const struct got_error *err;
607 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
608 return request_raw_object(outbuf, size, hdrlen, outfd, id,
611 err = start_read_object_child(repo);
615 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
619 const struct got_error *
620 got_object_open(struct got_object **obj, struct got_repository *repo,
621 struct got_object_id *id)
623 const struct got_error *err = NULL;
626 *obj = got_repo_get_cached_object(repo, id);
632 err = got_object_open_packed(obj, id, repo);
633 if (err && err->code != GOT_ERR_NO_OBJ)
637 return got_repo_cache_object(repo, id, *obj);
640 err = got_object_open_loose_fd(&fd, id, repo);
642 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
643 err = got_error_no_obj(id);
647 err = got_object_read_header_privsep(obj, id, repo, fd);
651 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
654 return got_repo_cache_object(repo, id, *obj);
657 /* *outfd must be initialized to -1 by caller */
658 const struct got_error *
659 got_object_raw_open(struct got_raw_object **obj, int *outfd,
660 struct got_repository *repo, struct got_object_id *id)
662 const struct got_error *err = NULL;
663 struct got_packidx *packidx = NULL;
665 uint8_t *outbuf = NULL;
668 char *path_packfile = NULL;
670 *obj = got_repo_get_cached_raw_object(repo, id);
677 *outfd = got_opentempfd();
679 return got_error_from_errno("got_opentempfd");
682 err = got_repo_search_packidx(&packidx, &idx, repo, id);
684 struct got_pack *pack = NULL;
686 err = got_packidx_get_packfile_path(&path_packfile,
687 packidx->path_packidx);
691 pack = got_repo_get_cached_pack(repo, path_packfile);
693 err = got_repo_cache_pack(&pack, repo, path_packfile,
698 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
699 *outfd, pack, packidx, idx, id);
702 } else if (err->code == GOT_ERR_NO_OBJ) {
705 err = got_object_open_loose_fd(&fd, id, repo);
708 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
714 *obj = calloc(1, sizeof(**obj));
716 err = got_error_from_errno("calloc");
722 (*obj)->data = outbuf;
725 if (fstat(*outfd, &sb) == -1) {
726 err = got_error_from_errno("fstat");
730 if (sb.st_size != hdrlen + size) {
731 err = got_error(GOT_ERR_PRIVSEP_LEN);
734 #ifndef GOT_PACK_NO_MMAP
735 if (hdrlen + size > 0) {
736 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
737 MAP_PRIVATE, *outfd, 0);
738 if ((*obj)->data == MAP_FAILED) {
739 if (errno != ENOMEM) {
740 err = got_error_from_errno("mmap");
751 (*obj)->f = fdopen(*outfd, "r");
752 if ((*obj)->f == NULL) {
753 err = got_error_from_errno("fdopen");
759 (*obj)->hdrlen = hdrlen;
761 err = got_repo_cache_raw_object(repo, id, *obj);
766 got_object_raw_close(*obj);
775 const struct got_error *
776 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
779 struct got_object_id id;
781 if (!got_parse_sha1_digest(id.sha1, id_str))
782 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
784 return got_object_open(obj, repo, &id);
787 const struct got_error *
788 got_object_resolve_id_str(struct got_object_id **id,
789 struct got_repository *repo, const char *id_str)
791 const struct got_error *err = NULL;
792 struct got_object *obj;
794 err = got_object_open_by_id_str(&obj, repo, id_str);
798 *id = got_object_id_dup(got_object_get_id(obj));
799 got_object_close(obj);
801 return got_error_from_errno("got_object_id_dup");
806 static const struct got_error *
807 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
808 int pack_idx, struct got_object_id *id)
810 const struct got_error *err = NULL;
812 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
817 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
821 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
825 static const struct got_error *
826 read_packed_commit_privsep(struct got_commit_object **commit,
827 struct got_pack *pack, struct got_packidx *packidx, int idx,
828 struct got_object_id *id)
830 const struct got_error *err = NULL;
832 if (pack->privsep_child)
833 return request_packed_commit(commit, pack, idx, id);
835 err = start_pack_privsep_child(pack, packidx);
839 return request_packed_commit(commit, pack, idx, id);
842 static const struct got_error *
843 request_commit(struct got_commit_object **commit, struct got_repository *repo,
844 int fd, struct got_object_id *id)
846 const struct got_error *err = NULL;
847 struct imsgbuf *ibuf;
849 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
851 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
855 return got_privsep_recv_commit(commit, ibuf);
858 static const struct got_error *
859 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
860 struct got_object_id *id, struct got_repository *repo)
862 const struct got_error *err;
865 struct imsgbuf *ibuf;
867 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
868 return request_commit(commit, repo, obj_fd, id);
870 ibuf = calloc(1, sizeof(*ibuf));
872 return got_error_from_errno("calloc");
874 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
875 err = got_error_from_errno("socketpair");
882 err = got_error_from_errno("fork");
887 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
892 if (close(imsg_fds[1]) == -1) {
893 err = got_error_from_errno("close");
897 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
899 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
900 imsg_init(ibuf, imsg_fds[0]);
901 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
903 return request_commit(commit, repo, obj_fd, id);
907 static const struct got_error *
908 open_commit(struct got_commit_object **commit,
909 struct got_repository *repo, struct got_object_id *id, int check_cache)
911 const struct got_error *err = NULL;
912 struct got_packidx *packidx = NULL;
914 char *path_packfile = NULL;
917 *commit = got_repo_get_cached_commit(repo, id);
918 if (*commit != NULL) {
925 err = got_repo_search_packidx(&packidx, &idx, repo, id);
927 struct got_pack *pack = NULL;
929 err = got_packidx_get_packfile_path(&path_packfile,
930 packidx->path_packidx);
934 pack = got_repo_get_cached_pack(repo, path_packfile);
936 err = got_repo_cache_pack(&pack, repo, path_packfile,
941 err = read_packed_commit_privsep(commit, pack,
943 } else if (err->code == GOT_ERR_NO_OBJ) {
946 err = got_object_open_loose_fd(&fd, id, repo);
949 err = read_commit_privsep(commit, fd, id, repo);
954 err = got_repo_cache_commit(repo, id, *commit);
961 const struct got_error *
962 got_object_open_as_commit(struct got_commit_object **commit,
963 struct got_repository *repo, struct got_object_id *id)
965 *commit = got_repo_get_cached_commit(repo, id);
966 if (*commit != NULL) {
971 return open_commit(commit, repo, id, 0);
974 const struct got_error *
975 got_object_commit_open(struct got_commit_object **commit,
976 struct got_repository *repo, struct got_object *obj)
978 return open_commit(commit, repo, got_object_get_id(obj), 1);
981 const struct got_error *
982 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
984 *qid = calloc(1, sizeof(**qid));
986 return got_error_from_errno("calloc");
988 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
992 const struct got_error *
993 got_object_id_queue_copy(const struct got_object_id_queue *src,
994 struct got_object_id_queue *dest)
996 const struct got_error *err;
997 struct got_object_qid *qid;
999 STAILQ_FOREACH(qid, src, entry) {
1000 struct got_object_qid *new;
1002 * Deep-copy the object ID only. Let the caller deal
1003 * with setting up the new->data pointer if needed.
1005 err = got_object_qid_alloc(&new, &qid->id);
1007 got_object_id_queue_free(dest);
1010 STAILQ_INSERT_TAIL(dest, new, entry);
1016 static const struct got_error *
1017 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1018 int pack_idx, struct got_object_id *id)
1020 const struct got_error *err = NULL;
1022 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1027 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1030 static const struct got_error *
1031 read_packed_tree_privsep(struct got_tree_object **tree,
1032 struct got_pack *pack, struct got_packidx *packidx, int idx,
1033 struct got_object_id *id)
1035 const struct got_error *err = NULL;
1037 if (pack->privsep_child)
1038 return request_packed_tree(tree, pack, idx, id);
1040 err = start_pack_privsep_child(pack, packidx);
1044 return request_packed_tree(tree, pack, idx, id);
1047 static const struct got_error *
1048 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1049 int fd, struct got_object_id *id)
1051 const struct got_error *err = NULL;
1052 struct imsgbuf *ibuf;
1054 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1056 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1060 return got_privsep_recv_tree(tree, ibuf);
1063 const struct got_error *
1064 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1065 struct got_object_id *id, struct got_repository *repo)
1067 const struct got_error *err;
1070 struct imsgbuf *ibuf;
1072 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1073 return request_tree(tree, repo, obj_fd, id);
1075 ibuf = calloc(1, sizeof(*ibuf));
1077 return got_error_from_errno("calloc");
1079 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1080 err = got_error_from_errno("socketpair");
1087 err = got_error_from_errno("fork");
1091 else if (pid == 0) {
1092 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1097 if (close(imsg_fds[1]) == -1) {
1098 err = got_error_from_errno("close");
1102 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1104 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1105 imsg_init(ibuf, imsg_fds[0]);
1106 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1109 return request_tree(tree, repo, obj_fd, id);
1112 static const struct got_error *
1113 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1114 struct got_object_id *id, int check_cache)
1116 const struct got_error *err = NULL;
1117 struct got_packidx *packidx = NULL;
1119 char *path_packfile = NULL;
1122 *tree = got_repo_get_cached_tree(repo, id);
1123 if (*tree != NULL) {
1130 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1132 struct got_pack *pack = NULL;
1134 err = got_packidx_get_packfile_path(&path_packfile,
1135 packidx->path_packidx);
1139 pack = got_repo_get_cached_pack(repo, path_packfile);
1141 err = got_repo_cache_pack(&pack, repo, path_packfile,
1146 err = read_packed_tree_privsep(tree, pack,
1148 } else if (err->code == GOT_ERR_NO_OBJ) {
1151 err = got_object_open_loose_fd(&fd, id, repo);
1154 err = read_tree_privsep(tree, fd, id, repo);
1159 err = got_repo_cache_tree(repo, id, *tree);
1162 free(path_packfile);
1166 const struct got_error *
1167 got_object_open_as_tree(struct got_tree_object **tree,
1168 struct got_repository *repo, struct got_object_id *id)
1170 *tree = got_repo_get_cached_tree(repo, id);
1171 if (*tree != NULL) {
1176 return open_tree(tree, repo, id, 0);
1179 const struct got_error *
1180 got_object_tree_open(struct got_tree_object **tree,
1181 struct got_repository *repo, struct got_object *obj)
1183 return open_tree(tree, repo, got_object_get_id(obj), 1);
1187 got_object_tree_get_nentries(struct got_tree_object *tree)
1189 return tree->nentries;
1192 struct got_tree_entry *
1193 got_object_tree_get_first_entry(struct got_tree_object *tree)
1195 return got_object_tree_get_entry(tree, 0);
1198 struct got_tree_entry *
1199 got_object_tree_get_last_entry(struct got_tree_object *tree)
1201 return got_object_tree_get_entry(tree, tree->nentries - 1);
1204 struct got_tree_entry *
1205 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1207 if (i < 0 || i >= tree->nentries)
1209 return &tree->entries[i];
1213 got_tree_entry_get_mode(struct got_tree_entry *te)
1219 got_tree_entry_get_name(struct got_tree_entry *te)
1221 return &te->name[0];
1224 struct got_object_id *
1225 got_tree_entry_get_id(struct got_tree_entry *te)
1230 const struct got_error *
1231 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1233 const struct got_error *err = NULL;
1234 size_t len, totlen, hdrlen, offset;
1238 hdrlen = got_object_blob_get_hdrlen(blob);
1244 err = got_object_blob_read_block(&len, blob);
1251 totlen += len - hdrlen;
1252 p = realloc(*s, totlen + 1);
1254 err = got_error_from_errno("realloc");
1260 /* Skip blob object header first time around. */
1262 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1267 (*s)[totlen] = '\0';
1271 const struct got_error *
1272 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1273 struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 struct got_blob_object *blob = NULL;
1278 *link_target = NULL;
1280 if (!got_object_tree_entry_is_symlink(te))
1281 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1283 err = got_object_open_as_blob(&blob, repo,
1284 got_tree_entry_get_id(te), PATH_MAX);
1288 err = got_object_blob_read_to_str(link_target, blob);
1289 got_object_blob_close(blob);
1292 *link_target = NULL;
1298 got_tree_entry_get_index(struct got_tree_entry *te)
1303 struct got_tree_entry *
1304 got_tree_entry_get_next(struct got_tree_object *tree,
1305 struct got_tree_entry *te)
1307 return got_object_tree_get_entry(tree, te->idx + 1);
1310 struct got_tree_entry *
1311 got_tree_entry_get_prev(struct got_tree_object *tree,
1312 struct got_tree_entry *te)
1314 return got_object_tree_get_entry(tree, te->idx - 1);
1317 static const struct got_error *
1318 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1319 struct got_pack *pack, struct got_packidx *packidx, int idx,
1320 struct got_object_id *id)
1322 const struct got_error *err = NULL;
1323 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1326 err = pack_child_send_tempfiles(ibuf, pack);
1330 outfd_child = dup(outfd);
1331 if (outfd_child == -1)
1332 return got_error_from_errno("dup");
1334 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1338 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1344 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1345 pack->privsep_child->ibuf);
1349 if (lseek(outfd, SEEK_SET, 0) == -1)
1350 err = got_error_from_errno("lseek");
1355 static const struct got_error *
1356 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1357 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1358 struct got_object_id *id)
1360 const struct got_error *err = NULL;
1362 if (pack->privsep_child == NULL) {
1363 err = start_pack_privsep_child(pack, packidx);
1368 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1372 static const struct got_error *
1373 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1374 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1376 const struct got_error *err = NULL;
1379 outfd_child = dup(outfd);
1380 if (outfd_child == -1)
1381 return got_error_from_errno("dup");
1383 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1387 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1391 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1395 if (lseek(outfd, SEEK_SET, 0) == -1)
1396 return got_error_from_errno("lseek");
1401 static const struct got_error *
1402 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1403 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1405 const struct got_error *err;
1408 struct imsgbuf *ibuf;
1410 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1411 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1412 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1416 ibuf = calloc(1, sizeof(*ibuf));
1418 return got_error_from_errno("calloc");
1420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1421 err = got_error_from_errno("socketpair");
1428 err = got_error_from_errno("fork");
1432 else if (pid == 0) {
1433 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1438 if (close(imsg_fds[1]) == -1) {
1439 err = got_error_from_errno("close");
1443 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1445 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1446 imsg_init(ibuf, imsg_fds[0]);
1447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1449 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1452 static const struct got_error *
1453 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1454 struct got_object_id *id, size_t blocksize)
1456 const struct got_error *err = NULL;
1457 struct got_packidx *packidx = NULL;
1459 char *path_packfile = NULL;
1462 size_t size, hdrlen;
1465 *blob = calloc(1, sizeof(**blob));
1467 return got_error_from_errno("calloc");
1469 outfd = got_opentempfd();
1471 return got_error_from_errno("got_opentempfd");
1473 (*blob)->read_buf = malloc(blocksize);
1474 if ((*blob)->read_buf == NULL) {
1475 err = got_error_from_errno("malloc");
1479 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1481 struct got_pack *pack = NULL;
1483 err = got_packidx_get_packfile_path(&path_packfile,
1484 packidx->path_packidx);
1488 pack = got_repo_get_cached_pack(repo, path_packfile);
1490 err = got_repo_cache_pack(&pack, repo, path_packfile,
1495 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1496 pack, packidx, idx, id);
1497 } else if (err->code == GOT_ERR_NO_OBJ) {
1500 err = got_object_open_loose_fd(&infd, id, repo);
1503 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1509 if (hdrlen > size) {
1510 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1515 if (close(outfd) == -1 && err == NULL)
1516 err = got_error_from_errno("close");
1518 (*blob)->f = fmemopen(outbuf, size, "rb");
1519 if ((*blob)->f == NULL) {
1520 err = got_error_from_errno("fmemopen");
1524 (*blob)->data = outbuf;
1526 if (fstat(outfd, &sb) == -1) {
1527 err = got_error_from_errno("fstat");
1531 if (sb.st_size != size) {
1532 err = got_error(GOT_ERR_PRIVSEP_LEN);
1536 (*blob)->f = fdopen(outfd, "rb");
1537 if ((*blob)->f == NULL) {
1538 err = got_error_from_errno("fdopen");
1545 (*blob)->hdrlen = hdrlen;
1546 (*blob)->blocksize = blocksize;
1547 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1550 free(path_packfile);
1553 got_object_blob_close(*blob);
1555 } else if (outfd != -1)
1561 const struct got_error *
1562 got_object_open_as_blob(struct got_blob_object **blob,
1563 struct got_repository *repo, struct got_object_id *id,
1566 return open_blob(blob, repo, id, blocksize);
1569 const struct got_error *
1570 got_object_blob_open(struct got_blob_object **blob,
1571 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1573 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1576 const struct got_error *
1577 got_object_blob_close(struct got_blob_object *blob)
1579 const struct got_error *err = NULL;
1580 free(blob->read_buf);
1581 if (blob->f && fclose(blob->f) == EOF)
1582 err = got_error_from_errno("fclose");
1589 got_object_blob_rewind(struct got_blob_object *blob)
1596 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1598 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1602 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1604 return blob->hdrlen;
1608 got_object_blob_get_read_buf(struct got_blob_object *blob)
1610 return blob->read_buf;
1613 const struct got_error *
1614 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1618 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1619 if (n == 0 && ferror(blob->f))
1620 return got_ferror(blob->f, GOT_ERR_IO);
1625 const struct got_error *
1626 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1627 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1629 const struct got_error *err = NULL;
1630 size_t n, len, hdrlen;
1633 const int alloc_chunksz = 512;
1635 off_t off = 0, total_len = 0;
1638 *line_offsets = NULL;
1644 hdrlen = got_object_blob_get_hdrlen(blob);
1646 err = got_object_blob_read_block(&len, blob);
1651 buf = got_object_blob_get_read_buf(blob);
1654 if (line_offsets && *line_offsets == NULL) {
1655 /* Have some data but perhaps no '\n'. */
1657 nalloc = alloc_chunksz;
1658 *line_offsets = calloc(nalloc,
1659 sizeof(**line_offsets));
1660 if (*line_offsets == NULL)
1661 return got_error_from_errno("calloc");
1663 /* Skip forward over end of first line. */
1670 /* Scan '\n' offsets in remaining chunk of data. */
1672 if (buf[i] != '\n') {
1677 if (line_offsets && nalloc < *nlines) {
1678 size_t n = *nlines + alloc_chunksz;
1679 off_t *o = recallocarray(*line_offsets,
1680 nalloc, n, sizeof(**line_offsets));
1682 free(*line_offsets);
1683 *line_offsets = NULL;
1684 return got_error_from_errno(
1691 off = total_len + i - hdrlen + 1;
1692 (*line_offsets)[*nlines - 1] = off;
1697 /* Skip blob object header first time around. */
1698 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1699 if (n != len - hdrlen)
1700 return got_ferror(outfile, GOT_ERR_IO);
1701 total_len += len - hdrlen;
1705 if (fflush(outfile) != 0)
1706 return got_error_from_errno("fflush");
1710 *filesize = total_len;
1715 static const struct got_error *
1716 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1717 int pack_idx, struct got_object_id *id)
1719 const struct got_error *err = NULL;
1721 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1726 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1729 static const struct got_error *
1730 read_packed_tag_privsep(struct got_tag_object **tag,
1731 struct got_pack *pack, struct got_packidx *packidx, int idx,
1732 struct got_object_id *id)
1734 const struct got_error *err = NULL;
1736 if (pack->privsep_child)
1737 return request_packed_tag(tag, pack, idx, id);
1739 err = start_pack_privsep_child(pack, packidx);
1743 return request_packed_tag(tag, pack, idx, id);
1746 static const struct got_error *
1747 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1748 int fd, struct got_object_id *id)
1750 const struct got_error *err = NULL;
1751 struct imsgbuf *ibuf;
1753 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1755 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1759 return got_privsep_recv_tag(tag, ibuf);
1762 static const struct got_error *
1763 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1764 struct got_object_id *id, struct got_repository *repo)
1766 const struct got_error *err;
1769 struct imsgbuf *ibuf;
1771 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1772 return request_tag(tag, repo, obj_fd, id);
1774 ibuf = calloc(1, sizeof(*ibuf));
1776 return got_error_from_errno("calloc");
1778 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1779 err = got_error_from_errno("socketpair");
1786 err = got_error_from_errno("fork");
1790 else if (pid == 0) {
1791 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1796 if (close(imsg_fds[1]) == -1) {
1797 err = got_error_from_errno("close");
1801 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1803 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1804 imsg_init(ibuf, imsg_fds[0]);
1805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1807 return request_tag(tag, repo, obj_fd, id);
1810 static const struct got_error *
1811 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1812 struct got_object_id *id, int check_cache)
1814 const struct got_error *err = NULL;
1815 struct got_packidx *packidx = NULL;
1817 char *path_packfile = NULL;
1818 struct got_object *obj = NULL;
1819 int obj_type = GOT_OBJ_TYPE_ANY;
1822 *tag = got_repo_get_cached_tag(repo, id);
1830 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1832 struct got_pack *pack = NULL;
1834 err = got_packidx_get_packfile_path(&path_packfile,
1835 packidx->path_packidx);
1839 pack = got_repo_get_cached_pack(repo, path_packfile);
1841 err = got_repo_cache_pack(&pack, repo, path_packfile,
1847 /* Beware of "lightweight" tags: Check object type first. */
1848 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1852 obj_type = obj->type;
1853 got_object_close(obj);
1854 if (obj_type != GOT_OBJ_TYPE_TAG) {
1855 err = got_error(GOT_ERR_OBJ_TYPE);
1858 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1859 } else if (err->code == GOT_ERR_NO_OBJ) {
1862 err = got_object_open_loose_fd(&fd, id, repo);
1865 err = got_object_read_header_privsep(&obj, id, repo, fd);
1868 obj_type = obj->type;
1869 got_object_close(obj);
1870 if (obj_type != GOT_OBJ_TYPE_TAG)
1871 return got_error(GOT_ERR_OBJ_TYPE);
1873 err = got_object_open_loose_fd(&fd, id, repo);
1876 err = read_tag_privsep(tag, fd, id, repo);
1881 err = got_repo_cache_tag(repo, id, *tag);
1884 free(path_packfile);
1888 const struct got_error *
1889 got_object_open_as_tag(struct got_tag_object **tag,
1890 struct got_repository *repo, struct got_object_id *id)
1892 *tag = got_repo_get_cached_tag(repo, id);
1898 return open_tag(tag, repo, id, 0);
1901 const struct got_error *
1902 got_object_tag_open(struct got_tag_object **tag,
1903 struct got_repository *repo, struct got_object *obj)
1905 return open_tag(tag, repo, got_object_get_id(obj), 1);
1909 got_object_tag_get_name(struct got_tag_object *tag)
1915 got_object_tag_get_object_type(struct got_tag_object *tag)
1917 return tag->obj_type;
1920 struct got_object_id *
1921 got_object_tag_get_object_id(struct got_tag_object *tag)
1927 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1929 return tag->tagger_time;
1933 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1935 return tag->tagger_gmtoff;
1939 got_object_tag_get_tagger(struct got_tag_object *tag)
1945 got_object_tag_get_message(struct got_tag_object *tag)
1950 static struct got_tree_entry *
1951 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1955 /* Note that tree entries are sorted in strncmp() order. */
1956 for (i = 0; i < tree->nentries; i++) {
1957 struct got_tree_entry *te = &tree->entries[i];
1958 int cmp = strncmp(te->name, name, len);
1963 if (te->name[len] == '\0')
1969 struct got_tree_entry *
1970 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1972 return find_entry_by_name(tree, name, strlen(name));
1975 const struct got_error *
1976 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1977 struct got_repository *repo, struct got_tree_object *tree,
1980 const struct got_error *err = NULL;
1981 struct got_tree_object *subtree = NULL;
1982 struct got_tree_entry *te = NULL;
1983 const char *seg, *s;
1995 struct got_tree_object *next_tree;
2004 te = find_entry_by_name(subtree, seg, seglen);
2006 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2017 err = got_object_open_as_tree(&next_tree, repo,
2022 if (subtree != tree)
2023 got_object_tree_close(subtree);
2024 subtree = next_tree;
2029 *id = got_object_id_dup(&te->id);
2031 return got_error_from_errno("got_object_id_dup");
2035 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2037 if (subtree && subtree != tree)
2038 got_object_tree_close(subtree);
2041 const struct got_error *
2042 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2043 struct got_commit_object *commit, const char *path)
2045 const struct got_error *err = NULL;
2046 struct got_tree_object *tree = NULL;
2050 /* Handle opening of root of commit's tree. */
2051 if (got_path_is_root_dir(path)) {
2052 *id = got_object_id_dup(commit->tree_id);
2054 err = got_error_from_errno("got_object_id_dup");
2056 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2059 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2063 got_object_tree_close(tree);
2068 * Normalize file mode bits to avoid false positive tree entry differences
2069 * in case tree entries have unexpected mode bits set.
2072 normalize_mode_for_comparison(mode_t mode)
2075 * For directories, the only relevant bit is the IFDIR bit.
2076 * This allows us to detect paths changing from a directory
2077 * to a file and vice versa.
2080 return mode & S_IFDIR;
2083 * For symlinks, the only relevant bit is the IFLNK bit.
2084 * This allows us to detect paths changing from a symlinks
2085 * to a file or directory and vice versa.
2088 return mode & S_IFLNK;
2090 /* For files, the only change we care about is the executable bit. */
2091 return mode & S_IXUSR;
2094 const struct got_error *
2095 got_object_tree_path_changed(int *changed,
2096 struct got_tree_object *tree01, struct got_tree_object *tree02,
2097 const char *path, struct got_repository *repo)
2099 const struct got_error *err = NULL;
2100 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2101 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2102 const char *seg, *s;
2107 /* We not do support comparing the root path. */
2108 if (got_path_is_root_dir(path))
2109 return got_error_path(path, GOT_ERR_BAD_PATH);
2119 struct got_tree_object *next_tree1, *next_tree2;
2120 mode_t mode1, mode2;
2129 te1 = find_entry_by_name(tree1, seg, seglen);
2131 err = got_error(GOT_ERR_NO_OBJ);
2136 te2 = find_entry_by_name(tree2, seg, seglen);
2139 mode1 = normalize_mode_for_comparison(te1->mode);
2140 mode2 = normalize_mode_for_comparison(te2->mode);
2141 if (mode1 != mode2) {
2146 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2152 if (*s == '\0') { /* final path element */
2161 err = got_object_open_as_tree(&next_tree1, repo,
2166 if (tree1 != tree01)
2167 got_object_tree_close(tree1);
2171 err = got_object_open_as_tree(&next_tree2, repo,
2176 if (tree2 != tree02)
2177 got_object_tree_close(tree2);
2180 if (tree2 != tree02)
2181 got_object_tree_close(tree2);
2187 if (tree1 && tree1 != tree01)
2188 got_object_tree_close(tree1);
2189 if (tree2 && tree2 != tree02)
2190 got_object_tree_close(tree2);
2194 const struct got_error *
2195 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2196 struct got_tree_entry *te)
2198 const struct got_error *err = NULL;
2200 *new_te = calloc(1, sizeof(**new_te));
2201 if (*new_te == NULL)
2202 return got_error_from_errno("calloc");
2204 (*new_te)->mode = te->mode;
2205 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2206 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2211 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2213 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2217 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2219 /* S_IFDIR check avoids confusing symlinks with submodules. */
2220 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2223 static const struct got_error *
2224 resolve_symlink(char **link_target, const char *path,
2225 struct got_commit_object *commit, struct got_repository *repo)
2227 const struct got_error *err = NULL;
2229 char *name, *parent_path = NULL;
2230 struct got_object_id *tree_obj_id = NULL;
2231 struct got_tree_object *tree = NULL;
2232 struct got_tree_entry *te = NULL;
2234 *link_target = NULL;
2236 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2237 return got_error(GOT_ERR_NO_SPACE);
2239 name = basename(buf);
2241 return got_error_from_errno2("basename", path);
2243 err = got_path_dirname(&parent_path, path);
2247 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2250 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2251 /* Display the complete path in error message. */
2252 err = got_error_path(path, err->code);
2257 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2261 te = got_object_tree_find_entry(tree, name);
2263 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2267 if (got_object_tree_entry_is_symlink(te)) {
2268 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2271 if (!got_path_is_absolute(*link_target)) {
2273 if (asprintf(&abspath, "%s/%s", parent_path,
2274 *link_target) == -1) {
2275 err = got_error_from_errno("asprintf");
2279 *link_target = malloc(PATH_MAX);
2280 if (*link_target == NULL) {
2281 err = got_error_from_errno("malloc");
2284 err = got_canonpath(abspath, *link_target, PATH_MAX);
2293 got_object_tree_close(tree);
2296 *link_target = NULL;
2301 const struct got_error *
2302 got_object_resolve_symlinks(char **link_target, const char *path,
2303 struct got_commit_object *commit, struct got_repository *repo)
2305 const struct got_error *err = NULL;
2306 char *next_target = NULL;
2307 int max_recursion = 40; /* matches Git */
2309 *link_target = NULL;
2312 err = resolve_symlink(&next_target,
2313 *link_target ? *link_target : path, commit, repo);
2318 if (--max_recursion == 0) {
2319 err = got_error_path(path, GOT_ERR_RECURSION);
2320 *link_target = NULL;
2323 *link_target = next_target;
2325 } while (next_target);
2330 const struct got_error *
2331 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2332 struct got_object_id *commit_id, const char *path,
2333 struct got_repository *repo)
2335 const struct got_error *err = NULL;
2336 struct got_pack *pack = NULL;
2337 struct got_packidx *packidx = NULL;
2338 char *path_packfile = NULL;
2339 struct got_commit_object *changed_commit = NULL;
2340 struct got_object_id *changed_commit_id = NULL;
2343 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2345 if (err->code != GOT_ERR_NO_OBJ)
2350 err = got_packidx_get_packfile_path(&path_packfile,
2351 packidx->path_packidx);
2355 pack = got_repo_get_cached_pack(repo, path_packfile);
2357 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2362 if (pack->privsep_child == NULL) {
2363 err = start_pack_privsep_child(pack, packidx);
2368 err = got_privsep_send_commit_traversal_request(
2369 pack->privsep_child->ibuf, commit_id, idx, path);
2373 err = got_privsep_recv_traversed_commits(&changed_commit,
2374 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2378 if (changed_commit) {
2380 * Cache the commit in which the path was changed.
2381 * This commit might be opened again soon.
2383 changed_commit->refcnt++;
2384 err = got_repo_cache_commit(repo, changed_commit_id,
2386 got_object_commit_close(changed_commit);
2389 free(path_packfile);
2390 free(changed_commit_id);