2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <sys/resource.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
140 err = got_error_from_errno2("open", path);
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
159 err = got_privsep_recv_obj(obj, ibuf);
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
168 /* Create temporary files used during delta application. */
169 static const struct got_error *
170 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
172 const struct got_error *err;
173 int basefd = -1, accumfd = -1;
176 * For performance reasons, the child will keep reusing the
177 * same temporary files during every object request.
178 * Opening and closing new files for every object request is
179 * too expensive during operations such as 'gotadmin pack'.
181 if (pack->child_has_tempfiles)
184 basefd = dup(pack->basefd);
186 return got_error_from_errno("dup");
188 accumfd = dup(pack->accumfd);
190 err = got_error_from_errno("dup");
194 err = got_privsep_send_tmpfd(ibuf, basefd);
198 err = got_privsep_send_tmpfd(ibuf, accumfd);
206 pack->child_has_tempfiles = 1;
210 static const struct got_error *
211 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
212 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
214 const struct got_error *err = NULL;
215 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
218 err = pack_child_send_tempfiles(ibuf, pack);
222 outfd_child = dup(outfd);
223 if (outfd_child == -1)
224 return got_error_from_errno("dup");
226 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
232 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
236 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
244 set_max_datasize(void)
248 if (getrlimit(RLIMIT_DATA, &rl) != 0)
251 rl.rlim_cur = rl.rlim_max;
252 setrlimit(RLIMIT_DATA, &rl);
255 static const struct got_error *
256 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
258 const struct got_error *err = NULL;
261 struct imsgbuf *ibuf;
263 ibuf = calloc(1, sizeof(*ibuf));
265 return got_error_from_errno("calloc");
267 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
268 if (pack->privsep_child == NULL) {
269 err = got_error_from_errno("calloc");
273 pack->child_has_tempfiles = 0;
274 pack->child_has_delta_outfd = 0;
276 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
277 err = got_error_from_errno("socketpair");
283 err = got_error_from_errno("fork");
285 } else if (pid == 0) {
287 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
288 pack->path_packfile);
292 if (close(imsg_fds[1]) == -1)
293 return got_error_from_errno("close");
294 pack->privsep_child->imsg_fd = imsg_fds[0];
295 pack->privsep_child->pid = pid;
296 imsg_init(ibuf, imsg_fds[0]);
297 pack->privsep_child->ibuf = ibuf;
299 err = got_privsep_init_pack_child(ibuf, pack, packidx);
301 const struct got_error *child_err;
302 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
303 child_err = got_privsep_wait_for_child(
304 pack->privsep_child->pid);
305 if (child_err && err == NULL)
311 free(pack->privsep_child);
312 pack->privsep_child = NULL;
317 static const struct got_error *
318 read_packed_object_privsep(struct got_object **obj,
319 struct got_repository *repo, struct got_pack *pack,
320 struct got_packidx *packidx, int idx, struct got_object_id *id)
322 const struct got_error *err = NULL;
324 if (pack->privsep_child == NULL) {
325 err = start_pack_privsep_child(pack, packidx);
330 return request_packed_object(obj, pack, idx, id);
333 static const struct got_error *
334 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
335 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
336 struct got_object_id *id)
338 const struct got_error *err = NULL;
340 if (pack->privsep_child == NULL) {
341 err = start_pack_privsep_child(pack, packidx);
346 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
350 const struct got_error *
351 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
352 struct got_repository *repo)
354 const struct got_error *err = NULL;
355 struct got_pack *pack = NULL;
356 struct got_packidx *packidx = NULL;
360 err = got_repo_search_packidx(&packidx, &idx, repo, id);
364 err = got_packidx_get_packfile_path(&path_packfile,
365 packidx->path_packidx);
369 pack = got_repo_get_cached_pack(repo, path_packfile);
371 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
376 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
384 const struct got_error *
385 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
386 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
387 struct got_repository *repo)
389 return read_packed_object_privsep(obj, repo, pack, packidx,
393 const struct got_error *
394 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
395 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
396 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
397 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
398 struct got_repository *repo)
400 const struct got_error *err = NULL;
401 struct got_pack *pack = NULL;
407 *delta_compressed_size = 0;
409 *delta_out_offset = 0;
411 err = got_packidx_get_packfile_path(&path_packfile,
412 packidx->path_packidx);
416 pack = got_repo_get_cached_pack(repo, path_packfile);
418 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
423 if (pack->privsep_child == NULL) {
424 err = start_pack_privsep_child(pack, packidx);
429 if (!pack->child_has_delta_outfd) {
431 outfd_child = dup(delta_cache_fd);
432 if (outfd_child == -1)
433 return got_error_from_errno("dup");
434 err = got_privsep_send_raw_delta_outfd(
435 pack->privsep_child->ibuf, outfd_child);
438 pack->child_has_delta_outfd = 1;
441 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
446 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
447 delta_compressed_size, delta_offset, delta_out_offset, base_id,
448 pack->privsep_child->ibuf);
452 * XXX This function does not really belong in object.c. It is only here
453 * because it needs start_pack_privsep_child(); relevant code should
454 * probably be moved to pack.c/pack_create.c.
456 const struct got_error *
457 got_object_prepare_delta_reuse(struct got_pack **pack,
458 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
460 const struct got_error *err = NULL;
461 char *path_packfile = NULL;
463 err = got_packidx_get_packfile_path(&path_packfile,
464 packidx->path_packidx);
468 *pack = got_repo_get_cached_pack(repo, path_packfile);
470 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
474 if ((*pack)->privsep_child == NULL) {
475 err = start_pack_privsep_child(*pack, packidx);
480 if (!(*pack)->child_has_delta_outfd) {
482 outfd_child = dup(delta_outfd);
483 if (outfd_child == -1) {
484 err = got_error_from_errno("dup");
487 err = got_privsep_send_raw_delta_outfd(
488 (*pack)->privsep_child->ibuf, outfd_child);
491 (*pack)->child_has_delta_outfd = 1;
494 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
500 static const struct got_error *
501 request_object(struct got_object **obj, struct got_object_id *id,
502 struct got_repository *repo, int fd)
504 const struct got_error *err = NULL;
505 struct imsgbuf *ibuf;
507 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
509 err = got_privsep_send_obj_req(ibuf, fd, id);
513 return got_privsep_recv_obj(obj, ibuf);
516 static const struct got_error *
517 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
518 struct got_object_id *id, struct got_repository *repo, int infd)
520 const struct got_error *err = NULL;
521 struct imsgbuf *ibuf;
524 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
526 outfd_child = dup(outfd);
527 if (outfd_child == -1)
528 return got_error_from_errno("dup");
530 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
534 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
538 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
541 static const struct got_error *
542 start_read_object_child(struct got_repository *repo)
544 const struct got_error *err = NULL;
547 struct imsgbuf *ibuf;
549 ibuf = calloc(1, sizeof(*ibuf));
551 return got_error_from_errno("calloc");
553 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
554 err = got_error_from_errno("socketpair");
561 err = got_error_from_errno("fork");
566 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
571 if (close(imsg_fds[1]) == -1) {
572 err = got_error_from_errno("close");
577 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
579 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
580 imsg_init(ibuf, imsg_fds[0]);
581 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
586 const struct got_error *
587 got_object_read_header_privsep(struct got_object **obj,
588 struct got_object_id *id, struct got_repository *repo, int obj_fd)
590 const struct got_error *err;
592 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
593 return request_object(obj, id, repo, obj_fd);
595 err = start_read_object_child(repo);
601 return request_object(obj, id, repo, obj_fd);
604 static const struct got_error *
605 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
606 int outfd, struct got_object_id *id, struct got_repository *repo,
609 const struct got_error *err;
611 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
612 return request_raw_object(outbuf, size, hdrlen, outfd, id,
615 err = start_read_object_child(repo);
619 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
623 const struct got_error *
624 got_object_open(struct got_object **obj, struct got_repository *repo,
625 struct got_object_id *id)
627 const struct got_error *err = NULL;
630 *obj = got_repo_get_cached_object(repo, id);
636 err = got_object_open_packed(obj, id, repo);
637 if (err && err->code != GOT_ERR_NO_OBJ)
641 return got_repo_cache_object(repo, id, *obj);
644 err = got_object_open_loose_fd(&fd, id, repo);
646 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
647 err = got_error_no_obj(id);
651 err = got_object_read_header_privsep(obj, id, repo, fd);
655 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
658 return got_repo_cache_object(repo, id, *obj);
661 /* *outfd must be initialized to -1 by caller */
662 const struct got_error *
663 got_object_raw_open(struct got_raw_object **obj, int *outfd,
664 struct got_repository *repo, struct got_object_id *id)
666 const struct got_error *err = NULL;
667 struct got_packidx *packidx = NULL;
669 uint8_t *outbuf = NULL;
672 char *path_packfile = NULL;
674 *obj = got_repo_get_cached_raw_object(repo, id);
681 *outfd = got_opentempfd();
683 return got_error_from_errno("got_opentempfd");
686 err = got_repo_search_packidx(&packidx, &idx, repo, id);
688 struct got_pack *pack = NULL;
690 err = got_packidx_get_packfile_path(&path_packfile,
691 packidx->path_packidx);
695 pack = got_repo_get_cached_pack(repo, path_packfile);
697 err = got_repo_cache_pack(&pack, repo, path_packfile,
702 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
703 *outfd, pack, packidx, idx, id);
706 } else if (err->code == GOT_ERR_NO_OBJ) {
709 err = got_object_open_loose_fd(&fd, id, repo);
712 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
718 *obj = calloc(1, sizeof(**obj));
720 err = got_error_from_errno("calloc");
726 (*obj)->data = outbuf;
729 if (fstat(*outfd, &sb) == -1) {
730 err = got_error_from_errno("fstat");
734 if (sb.st_size != hdrlen + size) {
735 err = got_error(GOT_ERR_PRIVSEP_LEN);
738 #ifndef GOT_PACK_NO_MMAP
739 if (hdrlen + size > 0) {
740 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
741 MAP_PRIVATE, *outfd, 0);
742 if ((*obj)->data == MAP_FAILED) {
743 if (errno != ENOMEM) {
744 err = got_error_from_errno("mmap");
755 (*obj)->f = fdopen(*outfd, "r");
756 if ((*obj)->f == NULL) {
757 err = got_error_from_errno("fdopen");
763 (*obj)->hdrlen = hdrlen;
765 err = got_repo_cache_raw_object(repo, id, *obj);
770 got_object_raw_close(*obj);
779 const struct got_error *
780 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
783 struct got_object_id id;
785 if (!got_parse_sha1_digest(id.sha1, id_str))
786 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
788 return got_object_open(obj, repo, &id);
791 const struct got_error *
792 got_object_resolve_id_str(struct got_object_id **id,
793 struct got_repository *repo, const char *id_str)
795 const struct got_error *err = NULL;
796 struct got_object *obj;
798 err = got_object_open_by_id_str(&obj, repo, id_str);
802 *id = got_object_id_dup(got_object_get_id(obj));
803 got_object_close(obj);
805 return got_error_from_errno("got_object_id_dup");
810 static const struct got_error *
811 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
812 int pack_idx, struct got_object_id *id)
814 const struct got_error *err = NULL;
816 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
821 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
825 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
829 static const struct got_error *
830 read_packed_commit_privsep(struct got_commit_object **commit,
831 struct got_pack *pack, struct got_packidx *packidx, int idx,
832 struct got_object_id *id)
834 const struct got_error *err = NULL;
836 if (pack->privsep_child)
837 return request_packed_commit(commit, pack, idx, id);
839 err = start_pack_privsep_child(pack, packidx);
843 return request_packed_commit(commit, pack, idx, id);
846 static const struct got_error *
847 request_commit(struct got_commit_object **commit, struct got_repository *repo,
848 int fd, struct got_object_id *id)
850 const struct got_error *err = NULL;
851 struct imsgbuf *ibuf;
853 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
855 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
859 return got_privsep_recv_commit(commit, ibuf);
862 static const struct got_error *
863 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
864 struct got_object_id *id, struct got_repository *repo)
866 const struct got_error *err;
869 struct imsgbuf *ibuf;
871 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
872 return request_commit(commit, repo, obj_fd, id);
874 ibuf = calloc(1, sizeof(*ibuf));
876 return got_error_from_errno("calloc");
878 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
879 err = got_error_from_errno("socketpair");
886 err = got_error_from_errno("fork");
891 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
896 if (close(imsg_fds[1]) == -1) {
897 err = got_error_from_errno("close");
901 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
903 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
904 imsg_init(ibuf, imsg_fds[0]);
905 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
907 return request_commit(commit, repo, obj_fd, id);
911 static const struct got_error *
912 open_commit(struct got_commit_object **commit,
913 struct got_repository *repo, struct got_object_id *id, int check_cache)
915 const struct got_error *err = NULL;
916 struct got_packidx *packidx = NULL;
918 char *path_packfile = NULL;
921 *commit = got_repo_get_cached_commit(repo, id);
922 if (*commit != NULL) {
929 err = got_repo_search_packidx(&packidx, &idx, repo, id);
931 struct got_pack *pack = NULL;
933 err = got_packidx_get_packfile_path(&path_packfile,
934 packidx->path_packidx);
938 pack = got_repo_get_cached_pack(repo, path_packfile);
940 err = got_repo_cache_pack(&pack, repo, path_packfile,
945 err = read_packed_commit_privsep(commit, pack,
947 } else if (err->code == GOT_ERR_NO_OBJ) {
950 err = got_object_open_loose_fd(&fd, id, repo);
953 err = read_commit_privsep(commit, fd, id, repo);
958 err = got_repo_cache_commit(repo, id, *commit);
965 const struct got_error *
966 got_object_open_as_commit(struct got_commit_object **commit,
967 struct got_repository *repo, struct got_object_id *id)
969 *commit = got_repo_get_cached_commit(repo, id);
970 if (*commit != NULL) {
975 return open_commit(commit, repo, id, 0);
978 const struct got_error *
979 got_object_commit_open(struct got_commit_object **commit,
980 struct got_repository *repo, struct got_object *obj)
982 return open_commit(commit, repo, got_object_get_id(obj), 1);
985 const struct got_error *
986 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
988 *qid = calloc(1, sizeof(**qid));
990 return got_error_from_errno("calloc");
992 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
996 const struct got_error *
997 got_object_id_queue_copy(const struct got_object_id_queue *src,
998 struct got_object_id_queue *dest)
1000 const struct got_error *err;
1001 struct got_object_qid *qid;
1003 STAILQ_FOREACH(qid, src, entry) {
1004 struct got_object_qid *new;
1006 * Deep-copy the object ID only. Let the caller deal
1007 * with setting up the new->data pointer if needed.
1009 err = got_object_qid_alloc(&new, &qid->id);
1011 got_object_id_queue_free(dest);
1014 STAILQ_INSERT_TAIL(dest, new, entry);
1020 static const struct got_error *
1021 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1022 int pack_idx, struct got_object_id *id)
1024 const struct got_error *err = NULL;
1026 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1031 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1034 static const struct got_error *
1035 read_packed_tree_privsep(struct got_tree_object **tree,
1036 struct got_pack *pack, struct got_packidx *packidx, int idx,
1037 struct got_object_id *id)
1039 const struct got_error *err = NULL;
1041 if (pack->privsep_child)
1042 return request_packed_tree(tree, pack, idx, id);
1044 err = start_pack_privsep_child(pack, packidx);
1048 return request_packed_tree(tree, pack, idx, id);
1051 static const struct got_error *
1052 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1053 int fd, struct got_object_id *id)
1055 const struct got_error *err = NULL;
1056 struct imsgbuf *ibuf;
1058 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1060 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1064 return got_privsep_recv_tree(tree, ibuf);
1067 const struct got_error *
1068 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1069 struct got_object_id *id, struct got_repository *repo)
1071 const struct got_error *err;
1074 struct imsgbuf *ibuf;
1076 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1077 return request_tree(tree, repo, obj_fd, id);
1079 ibuf = calloc(1, sizeof(*ibuf));
1081 return got_error_from_errno("calloc");
1083 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1084 err = got_error_from_errno("socketpair");
1091 err = got_error_from_errno("fork");
1095 else if (pid == 0) {
1096 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1101 if (close(imsg_fds[1]) == -1) {
1102 err = got_error_from_errno("close");
1106 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1108 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1109 imsg_init(ibuf, imsg_fds[0]);
1110 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1113 return request_tree(tree, repo, obj_fd, id);
1116 static const struct got_error *
1117 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1118 struct got_object_id *id, int check_cache)
1120 const struct got_error *err = NULL;
1121 struct got_packidx *packidx = NULL;
1123 char *path_packfile = NULL;
1126 *tree = got_repo_get_cached_tree(repo, id);
1127 if (*tree != NULL) {
1134 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1136 struct got_pack *pack = NULL;
1138 err = got_packidx_get_packfile_path(&path_packfile,
1139 packidx->path_packidx);
1143 pack = got_repo_get_cached_pack(repo, path_packfile);
1145 err = got_repo_cache_pack(&pack, repo, path_packfile,
1150 err = read_packed_tree_privsep(tree, pack,
1152 } else if (err->code == GOT_ERR_NO_OBJ) {
1155 err = got_object_open_loose_fd(&fd, id, repo);
1158 err = read_tree_privsep(tree, fd, id, repo);
1163 err = got_repo_cache_tree(repo, id, *tree);
1166 free(path_packfile);
1170 const struct got_error *
1171 got_object_open_as_tree(struct got_tree_object **tree,
1172 struct got_repository *repo, struct got_object_id *id)
1174 *tree = got_repo_get_cached_tree(repo, id);
1175 if (*tree != NULL) {
1180 return open_tree(tree, repo, id, 0);
1183 const struct got_error *
1184 got_object_tree_open(struct got_tree_object **tree,
1185 struct got_repository *repo, struct got_object *obj)
1187 return open_tree(tree, repo, got_object_get_id(obj), 1);
1191 got_object_tree_get_nentries(struct got_tree_object *tree)
1193 return tree->nentries;
1196 struct got_tree_entry *
1197 got_object_tree_get_first_entry(struct got_tree_object *tree)
1199 return got_object_tree_get_entry(tree, 0);
1202 struct got_tree_entry *
1203 got_object_tree_get_last_entry(struct got_tree_object *tree)
1205 return got_object_tree_get_entry(tree, tree->nentries - 1);
1208 struct got_tree_entry *
1209 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1211 if (i < 0 || i >= tree->nentries)
1213 return &tree->entries[i];
1217 got_tree_entry_get_mode(struct got_tree_entry *te)
1223 got_tree_entry_get_name(struct got_tree_entry *te)
1225 return &te->name[0];
1228 struct got_object_id *
1229 got_tree_entry_get_id(struct got_tree_entry *te)
1234 const struct got_error *
1235 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1237 const struct got_error *err = NULL;
1238 size_t len, totlen, hdrlen, offset;
1242 hdrlen = got_object_blob_get_hdrlen(blob);
1248 err = got_object_blob_read_block(&len, blob);
1255 totlen += len - hdrlen;
1256 p = realloc(*s, totlen + 1);
1258 err = got_error_from_errno("realloc");
1264 /* Skip blob object header first time around. */
1266 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1271 (*s)[totlen] = '\0';
1275 const struct got_error *
1276 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1277 struct got_repository *repo)
1279 const struct got_error *err = NULL;
1280 struct got_blob_object *blob = NULL;
1282 *link_target = NULL;
1284 if (!got_object_tree_entry_is_symlink(te))
1285 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1287 err = got_object_open_as_blob(&blob, repo,
1288 got_tree_entry_get_id(te), PATH_MAX);
1292 err = got_object_blob_read_to_str(link_target, blob);
1293 got_object_blob_close(blob);
1296 *link_target = NULL;
1302 got_tree_entry_get_index(struct got_tree_entry *te)
1307 struct got_tree_entry *
1308 got_tree_entry_get_next(struct got_tree_object *tree,
1309 struct got_tree_entry *te)
1311 return got_object_tree_get_entry(tree, te->idx + 1);
1314 struct got_tree_entry *
1315 got_tree_entry_get_prev(struct got_tree_object *tree,
1316 struct got_tree_entry *te)
1318 return got_object_tree_get_entry(tree, te->idx - 1);
1321 static const struct got_error *
1322 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1323 struct got_pack *pack, struct got_packidx *packidx, int idx,
1324 struct got_object_id *id)
1326 const struct got_error *err = NULL;
1327 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1330 err = pack_child_send_tempfiles(ibuf, pack);
1334 outfd_child = dup(outfd);
1335 if (outfd_child == -1)
1336 return got_error_from_errno("dup");
1338 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1342 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1348 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1349 pack->privsep_child->ibuf);
1353 if (lseek(outfd, SEEK_SET, 0) == -1)
1354 err = got_error_from_errno("lseek");
1359 static const struct got_error *
1360 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1361 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1362 struct got_object_id *id)
1364 const struct got_error *err = NULL;
1366 if (pack->privsep_child == NULL) {
1367 err = start_pack_privsep_child(pack, packidx);
1372 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1376 static const struct got_error *
1377 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1378 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1380 const struct got_error *err = NULL;
1383 outfd_child = dup(outfd);
1384 if (outfd_child == -1)
1385 return got_error_from_errno("dup");
1387 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1391 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1395 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1399 if (lseek(outfd, SEEK_SET, 0) == -1)
1400 return got_error_from_errno("lseek");
1405 static const struct got_error *
1406 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1407 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1409 const struct got_error *err;
1412 struct imsgbuf *ibuf;
1414 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1415 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1416 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1420 ibuf = calloc(1, sizeof(*ibuf));
1422 return got_error_from_errno("calloc");
1424 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1425 err = got_error_from_errno("socketpair");
1432 err = got_error_from_errno("fork");
1436 else if (pid == 0) {
1437 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1442 if (close(imsg_fds[1]) == -1) {
1443 err = got_error_from_errno("close");
1447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1450 imsg_init(ibuf, imsg_fds[0]);
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1453 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1456 static const struct got_error *
1457 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1458 struct got_object_id *id, size_t blocksize)
1460 const struct got_error *err = NULL;
1461 struct got_packidx *packidx = NULL;
1463 char *path_packfile = NULL;
1466 size_t size, hdrlen;
1469 *blob = calloc(1, sizeof(**blob));
1471 return got_error_from_errno("calloc");
1473 outfd = got_opentempfd();
1475 return got_error_from_errno("got_opentempfd");
1477 (*blob)->read_buf = malloc(blocksize);
1478 if ((*blob)->read_buf == NULL) {
1479 err = got_error_from_errno("malloc");
1483 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1485 struct got_pack *pack = NULL;
1487 err = got_packidx_get_packfile_path(&path_packfile,
1488 packidx->path_packidx);
1492 pack = got_repo_get_cached_pack(repo, path_packfile);
1494 err = got_repo_cache_pack(&pack, repo, path_packfile,
1499 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1500 pack, packidx, idx, id);
1501 } else if (err->code == GOT_ERR_NO_OBJ) {
1504 err = got_object_open_loose_fd(&infd, id, repo);
1507 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1513 if (hdrlen > size) {
1514 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1519 if (close(outfd) == -1 && err == NULL)
1520 err = got_error_from_errno("close");
1522 (*blob)->f = fmemopen(outbuf, size, "rb");
1523 if ((*blob)->f == NULL) {
1524 err = got_error_from_errno("fmemopen");
1528 (*blob)->data = outbuf;
1530 if (fstat(outfd, &sb) == -1) {
1531 err = got_error_from_errno("fstat");
1535 if (sb.st_size != size) {
1536 err = got_error(GOT_ERR_PRIVSEP_LEN);
1540 (*blob)->f = fdopen(outfd, "rb");
1541 if ((*blob)->f == NULL) {
1542 err = got_error_from_errno("fdopen");
1549 (*blob)->hdrlen = hdrlen;
1550 (*blob)->blocksize = blocksize;
1551 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1554 free(path_packfile);
1557 got_object_blob_close(*blob);
1559 } else if (outfd != -1)
1565 const struct got_error *
1566 got_object_open_as_blob(struct got_blob_object **blob,
1567 struct got_repository *repo, struct got_object_id *id,
1570 return open_blob(blob, repo, id, blocksize);
1573 const struct got_error *
1574 got_object_blob_open(struct got_blob_object **blob,
1575 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1577 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1580 const struct got_error *
1581 got_object_blob_close(struct got_blob_object *blob)
1583 const struct got_error *err = NULL;
1584 free(blob->read_buf);
1585 if (blob->f && fclose(blob->f) == EOF)
1586 err = got_error_from_errno("fclose");
1593 got_object_blob_rewind(struct got_blob_object *blob)
1600 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1602 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1606 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1608 return blob->hdrlen;
1612 got_object_blob_get_read_buf(struct got_blob_object *blob)
1614 return blob->read_buf;
1617 const struct got_error *
1618 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1622 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1623 if (n == 0 && ferror(blob->f))
1624 return got_ferror(blob->f, GOT_ERR_IO);
1629 const struct got_error *
1630 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1631 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1633 const struct got_error *err = NULL;
1634 size_t n, len, hdrlen;
1637 const int alloc_chunksz = 512;
1639 off_t off = 0, total_len = 0;
1642 *line_offsets = NULL;
1648 hdrlen = got_object_blob_get_hdrlen(blob);
1650 err = got_object_blob_read_block(&len, blob);
1655 buf = got_object_blob_get_read_buf(blob);
1658 if (line_offsets && *line_offsets == NULL) {
1659 /* Have some data but perhaps no '\n'. */
1661 nalloc = alloc_chunksz;
1662 *line_offsets = calloc(nalloc,
1663 sizeof(**line_offsets));
1664 if (*line_offsets == NULL)
1665 return got_error_from_errno("calloc");
1667 /* Skip forward over end of first line. */
1674 /* Scan '\n' offsets in remaining chunk of data. */
1676 if (buf[i] != '\n') {
1681 if (line_offsets && nalloc < *nlines) {
1682 size_t n = *nlines + alloc_chunksz;
1683 off_t *o = recallocarray(*line_offsets,
1684 nalloc, n, sizeof(**line_offsets));
1686 free(*line_offsets);
1687 *line_offsets = NULL;
1688 return got_error_from_errno(
1695 off = total_len + i - hdrlen + 1;
1696 (*line_offsets)[*nlines - 1] = off;
1701 /* Skip blob object header first time around. */
1702 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1703 if (n != len - hdrlen)
1704 return got_ferror(outfile, GOT_ERR_IO);
1705 total_len += len - hdrlen;
1709 if (fflush(outfile) != 0)
1710 return got_error_from_errno("fflush");
1714 *filesize = total_len;
1719 static const struct got_error *
1720 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1721 int pack_idx, struct got_object_id *id)
1723 const struct got_error *err = NULL;
1725 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1730 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1733 static const struct got_error *
1734 read_packed_tag_privsep(struct got_tag_object **tag,
1735 struct got_pack *pack, struct got_packidx *packidx, int idx,
1736 struct got_object_id *id)
1738 const struct got_error *err = NULL;
1740 if (pack->privsep_child)
1741 return request_packed_tag(tag, pack, idx, id);
1743 err = start_pack_privsep_child(pack, packidx);
1747 return request_packed_tag(tag, pack, idx, id);
1750 static const struct got_error *
1751 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1752 int fd, struct got_object_id *id)
1754 const struct got_error *err = NULL;
1755 struct imsgbuf *ibuf;
1757 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1759 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1763 return got_privsep_recv_tag(tag, ibuf);
1766 static const struct got_error *
1767 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1768 struct got_object_id *id, struct got_repository *repo)
1770 const struct got_error *err;
1773 struct imsgbuf *ibuf;
1775 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1776 return request_tag(tag, repo, obj_fd, id);
1778 ibuf = calloc(1, sizeof(*ibuf));
1780 return got_error_from_errno("calloc");
1782 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1783 err = got_error_from_errno("socketpair");
1790 err = got_error_from_errno("fork");
1794 else if (pid == 0) {
1795 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1800 if (close(imsg_fds[1]) == -1) {
1801 err = got_error_from_errno("close");
1805 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1807 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1808 imsg_init(ibuf, imsg_fds[0]);
1809 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1811 return request_tag(tag, repo, obj_fd, id);
1814 static const struct got_error *
1815 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1816 struct got_object_id *id, int check_cache)
1818 const struct got_error *err = NULL;
1819 struct got_packidx *packidx = NULL;
1821 char *path_packfile = NULL;
1822 struct got_object *obj = NULL;
1823 int obj_type = GOT_OBJ_TYPE_ANY;
1826 *tag = got_repo_get_cached_tag(repo, id);
1834 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1836 struct got_pack *pack = NULL;
1838 err = got_packidx_get_packfile_path(&path_packfile,
1839 packidx->path_packidx);
1843 pack = got_repo_get_cached_pack(repo, path_packfile);
1845 err = got_repo_cache_pack(&pack, repo, path_packfile,
1851 /* Beware of "lightweight" tags: Check object type first. */
1852 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1856 obj_type = obj->type;
1857 got_object_close(obj);
1858 if (obj_type != GOT_OBJ_TYPE_TAG) {
1859 err = got_error(GOT_ERR_OBJ_TYPE);
1862 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1863 } else if (err->code == GOT_ERR_NO_OBJ) {
1866 err = got_object_open_loose_fd(&fd, id, repo);
1869 err = got_object_read_header_privsep(&obj, id, repo, fd);
1872 obj_type = obj->type;
1873 got_object_close(obj);
1874 if (obj_type != GOT_OBJ_TYPE_TAG)
1875 return got_error(GOT_ERR_OBJ_TYPE);
1877 err = got_object_open_loose_fd(&fd, id, repo);
1880 err = read_tag_privsep(tag, fd, id, repo);
1885 err = got_repo_cache_tag(repo, id, *tag);
1888 free(path_packfile);
1892 const struct got_error *
1893 got_object_open_as_tag(struct got_tag_object **tag,
1894 struct got_repository *repo, struct got_object_id *id)
1896 *tag = got_repo_get_cached_tag(repo, id);
1902 return open_tag(tag, repo, id, 0);
1905 const struct got_error *
1906 got_object_tag_open(struct got_tag_object **tag,
1907 struct got_repository *repo, struct got_object *obj)
1909 return open_tag(tag, repo, got_object_get_id(obj), 1);
1913 got_object_tag_get_name(struct got_tag_object *tag)
1919 got_object_tag_get_object_type(struct got_tag_object *tag)
1921 return tag->obj_type;
1924 struct got_object_id *
1925 got_object_tag_get_object_id(struct got_tag_object *tag)
1931 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1933 return tag->tagger_time;
1937 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1939 return tag->tagger_gmtoff;
1943 got_object_tag_get_tagger(struct got_tag_object *tag)
1949 got_object_tag_get_message(struct got_tag_object *tag)
1954 static struct got_tree_entry *
1955 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1959 /* Note that tree entries are sorted in strncmp() order. */
1960 for (i = 0; i < tree->nentries; i++) {
1961 struct got_tree_entry *te = &tree->entries[i];
1962 int cmp = strncmp(te->name, name, len);
1967 if (te->name[len] == '\0')
1973 struct got_tree_entry *
1974 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1976 return find_entry_by_name(tree, name, strlen(name));
1979 const struct got_error *
1980 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1981 struct got_repository *repo, struct got_tree_object *tree,
1984 const struct got_error *err = NULL;
1985 struct got_tree_object *subtree = NULL;
1986 struct got_tree_entry *te = NULL;
1987 const char *seg, *s;
1999 struct got_tree_object *next_tree;
2008 te = find_entry_by_name(subtree, seg, seglen);
2010 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2021 err = got_object_open_as_tree(&next_tree, repo,
2026 if (subtree != tree)
2027 got_object_tree_close(subtree);
2028 subtree = next_tree;
2033 *id = got_object_id_dup(&te->id);
2035 return got_error_from_errno("got_object_id_dup");
2039 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2041 if (subtree && subtree != tree)
2042 got_object_tree_close(subtree);
2045 const struct got_error *
2046 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2047 struct got_commit_object *commit, const char *path)
2049 const struct got_error *err = NULL;
2050 struct got_tree_object *tree = NULL;
2054 /* Handle opening of root of commit's tree. */
2055 if (got_path_is_root_dir(path)) {
2056 *id = got_object_id_dup(commit->tree_id);
2058 err = got_error_from_errno("got_object_id_dup");
2060 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2063 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2067 got_object_tree_close(tree);
2072 * Normalize file mode bits to avoid false positive tree entry differences
2073 * in case tree entries have unexpected mode bits set.
2076 normalize_mode_for_comparison(mode_t mode)
2079 * For directories, the only relevant bit is the IFDIR bit.
2080 * This allows us to detect paths changing from a directory
2081 * to a file and vice versa.
2084 return mode & S_IFDIR;
2087 * For symlinks, the only relevant bit is the IFLNK bit.
2088 * This allows us to detect paths changing from a symlinks
2089 * to a file or directory and vice versa.
2092 return mode & S_IFLNK;
2094 /* For files, the only change we care about is the executable bit. */
2095 return mode & S_IXUSR;
2098 const struct got_error *
2099 got_object_tree_path_changed(int *changed,
2100 struct got_tree_object *tree01, struct got_tree_object *tree02,
2101 const char *path, struct got_repository *repo)
2103 const struct got_error *err = NULL;
2104 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2105 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2106 const char *seg, *s;
2111 /* We not do support comparing the root path. */
2112 if (got_path_is_root_dir(path))
2113 return got_error_path(path, GOT_ERR_BAD_PATH);
2123 struct got_tree_object *next_tree1, *next_tree2;
2124 mode_t mode1, mode2;
2133 te1 = find_entry_by_name(tree1, seg, seglen);
2135 err = got_error(GOT_ERR_NO_OBJ);
2140 te2 = find_entry_by_name(tree2, seg, seglen);
2143 mode1 = normalize_mode_for_comparison(te1->mode);
2144 mode2 = normalize_mode_for_comparison(te2->mode);
2145 if (mode1 != mode2) {
2150 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2156 if (*s == '\0') { /* final path element */
2165 err = got_object_open_as_tree(&next_tree1, repo,
2170 if (tree1 != tree01)
2171 got_object_tree_close(tree1);
2175 err = got_object_open_as_tree(&next_tree2, repo,
2180 if (tree2 != tree02)
2181 got_object_tree_close(tree2);
2184 if (tree2 != tree02)
2185 got_object_tree_close(tree2);
2191 if (tree1 && tree1 != tree01)
2192 got_object_tree_close(tree1);
2193 if (tree2 && tree2 != tree02)
2194 got_object_tree_close(tree2);
2198 const struct got_error *
2199 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2200 struct got_tree_entry *te)
2202 const struct got_error *err = NULL;
2204 *new_te = calloc(1, sizeof(**new_te));
2205 if (*new_te == NULL)
2206 return got_error_from_errno("calloc");
2208 (*new_te)->mode = te->mode;
2209 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2210 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2215 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2217 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2221 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2223 /* S_IFDIR check avoids confusing symlinks with submodules. */
2224 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2227 static const struct got_error *
2228 resolve_symlink(char **link_target, const char *path,
2229 struct got_commit_object *commit, struct got_repository *repo)
2231 const struct got_error *err = NULL;
2233 char *name, *parent_path = NULL;
2234 struct got_object_id *tree_obj_id = NULL;
2235 struct got_tree_object *tree = NULL;
2236 struct got_tree_entry *te = NULL;
2238 *link_target = NULL;
2240 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2241 return got_error(GOT_ERR_NO_SPACE);
2243 name = basename(buf);
2245 return got_error_from_errno2("basename", path);
2247 err = got_path_dirname(&parent_path, path);
2251 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2254 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2255 /* Display the complete path in error message. */
2256 err = got_error_path(path, err->code);
2261 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2265 te = got_object_tree_find_entry(tree, name);
2267 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2271 if (got_object_tree_entry_is_symlink(te)) {
2272 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2275 if (!got_path_is_absolute(*link_target)) {
2277 if (asprintf(&abspath, "%s/%s", parent_path,
2278 *link_target) == -1) {
2279 err = got_error_from_errno("asprintf");
2283 *link_target = malloc(PATH_MAX);
2284 if (*link_target == NULL) {
2285 err = got_error_from_errno("malloc");
2288 err = got_canonpath(abspath, *link_target, PATH_MAX);
2297 got_object_tree_close(tree);
2300 *link_target = NULL;
2305 const struct got_error *
2306 got_object_resolve_symlinks(char **link_target, const char *path,
2307 struct got_commit_object *commit, struct got_repository *repo)
2309 const struct got_error *err = NULL;
2310 char *next_target = NULL;
2311 int max_recursion = 40; /* matches Git */
2313 *link_target = NULL;
2316 err = resolve_symlink(&next_target,
2317 *link_target ? *link_target : path, commit, repo);
2322 if (--max_recursion == 0) {
2323 err = got_error_path(path, GOT_ERR_RECURSION);
2324 *link_target = NULL;
2327 *link_target = next_target;
2329 } while (next_target);
2334 const struct got_error *
2335 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2336 struct got_object_id *commit_id, const char *path,
2337 struct got_repository *repo)
2339 const struct got_error *err = NULL;
2340 struct got_pack *pack = NULL;
2341 struct got_packidx *packidx = NULL;
2342 char *path_packfile = NULL;
2343 struct got_commit_object *changed_commit = NULL;
2344 struct got_object_id *changed_commit_id = NULL;
2347 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2349 if (err->code != GOT_ERR_NO_OBJ)
2354 err = got_packidx_get_packfile_path(&path_packfile,
2355 packidx->path_packidx);
2359 pack = got_repo_get_cached_pack(repo, path_packfile);
2361 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2366 if (pack->privsep_child == NULL) {
2367 err = start_pack_privsep_child(pack, packidx);
2372 err = got_privsep_send_commit_traversal_request(
2373 pack->privsep_child->ibuf, commit_id, idx, path);
2377 err = got_privsep_recv_traversed_commits(&changed_commit,
2378 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2382 if (changed_commit) {
2384 * Cache the commit in which the path was changed.
2385 * This commit might be opened again soon.
2387 changed_commit->refcnt++;
2388 err = got_repo_cache_commit(repo, changed_commit_id,
2390 got_object_commit_close(changed_commit);
2393 free(path_packfile);
2394 free(changed_commit_id);