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))
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 struct got_object_id *
68 got_object_get_id(struct got_object *obj)
73 const struct got_error *
74 got_object_get_id_str(char **outbuf, struct got_object *obj)
76 return got_object_id_str(outbuf, &obj->id);
79 const struct got_error *
80 got_object_get_type(int *type, struct got_repository *repo,
81 struct got_object_id *id)
83 const struct got_error *err = NULL;
84 struct got_object *obj;
86 err = got_object_open(&obj, repo, id);
91 case GOT_OBJ_TYPE_COMMIT:
92 case GOT_OBJ_TYPE_TREE:
93 case GOT_OBJ_TYPE_BLOB:
94 case GOT_OBJ_TYPE_TAG:
98 err = got_error(GOT_ERR_OBJ_TYPE);
102 got_object_close(obj);
106 const struct got_error *
107 got_object_get_path(char **path, struct got_object_id *id,
108 struct got_repository *repo)
110 const struct got_error *err = NULL;
116 path_objects = got_repo_get_path_objects(repo);
117 if (path_objects == NULL)
118 return got_error_from_errno("got_repo_get_path_objects");
120 err = got_object_id_str(&hex, id);
124 if (asprintf(path, "%s/%.2x/%s", path_objects,
125 id->sha1[0], hex + 2) == -1)
126 err = got_error_from_errno("asprintf");
134 const struct got_error *
135 got_object_open_loose_fd(int *fd, struct got_object_id *id,
136 struct got_repository *repo)
138 const struct got_error *err = NULL;
141 err = got_object_get_path(&path, id, repo);
144 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
146 err = got_error_from_errno2("open", path);
154 static const struct got_error *
155 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
156 struct got_object_id *id)
158 const struct got_error *err = NULL;
159 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
161 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
165 err = got_privsep_recv_obj(obj, ibuf);
169 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
174 /* Create temporary files used during delta application. */
175 static const struct got_error *
176 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
178 const struct got_error *err;
179 int basefd = -1, accumfd = -1;
182 * For performance reasons, the child will keep reusing the
183 * same temporary files during every object request.
184 * Opening and closing new files for every object request is
185 * too expensive during operations such as 'gotadmin pack'.
187 if (pack->child_has_tempfiles)
190 basefd = dup(pack->basefd);
192 return got_error_from_errno("dup");
194 accumfd = dup(pack->accumfd);
196 err = got_error_from_errno("dup");
200 err = got_privsep_send_tmpfd(ibuf, basefd);
204 err = got_privsep_send_tmpfd(ibuf, accumfd);
212 pack->child_has_tempfiles = 1;
216 static const struct got_error *
217 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
218 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
220 const struct got_error *err = NULL;
221 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
224 err = pack_child_send_tempfiles(ibuf, pack);
228 outfd_child = dup(outfd);
229 if (outfd_child == -1)
230 return got_error_from_errno("dup");
232 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
238 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
242 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
250 set_max_datasize(void)
254 if (getrlimit(RLIMIT_DATA, &rl) != 0)
257 rl.rlim_cur = rl.rlim_max;
258 setrlimit(RLIMIT_DATA, &rl);
261 static const struct got_error *
262 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
264 const struct got_error *err = NULL;
267 struct imsgbuf *ibuf;
269 ibuf = calloc(1, sizeof(*ibuf));
271 return got_error_from_errno("calloc");
273 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
274 if (pack->privsep_child == NULL) {
275 err = got_error_from_errno("calloc");
279 pack->child_has_tempfiles = 0;
280 pack->child_has_delta_outfd = 0;
282 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
283 err = got_error_from_errno("socketpair");
289 err = got_error_from_errno("fork");
291 } else if (pid == 0) {
293 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
294 pack->path_packfile);
298 if (close(imsg_fds[1]) == -1)
299 return got_error_from_errno("close");
300 pack->privsep_child->imsg_fd = imsg_fds[0];
301 pack->privsep_child->pid = pid;
302 imsg_init(ibuf, imsg_fds[0]);
303 pack->privsep_child->ibuf = ibuf;
305 err = got_privsep_init_pack_child(ibuf, pack, packidx);
307 const struct got_error *child_err;
308 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
309 child_err = got_privsep_wait_for_child(
310 pack->privsep_child->pid);
311 if (child_err && err == NULL)
317 free(pack->privsep_child);
318 pack->privsep_child = NULL;
323 static const struct got_error *
324 read_packed_object_privsep(struct got_object **obj,
325 struct got_repository *repo, struct got_pack *pack,
326 struct got_packidx *packidx, int idx, struct got_object_id *id)
328 const struct got_error *err = NULL;
330 if (pack->privsep_child == NULL) {
331 err = start_pack_privsep_child(pack, packidx);
336 return request_packed_object(obj, pack, idx, id);
339 static const struct got_error *
340 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
341 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
342 struct got_object_id *id)
344 const struct got_error *err = NULL;
346 if (pack->privsep_child == NULL) {
347 err = start_pack_privsep_child(pack, packidx);
352 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
356 const struct got_error *
357 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
358 struct got_repository *repo)
360 const struct got_error *err = NULL;
361 struct got_pack *pack = NULL;
362 struct got_packidx *packidx = NULL;
366 err = got_repo_search_packidx(&packidx, &idx, repo, id);
370 err = got_packidx_get_packfile_path(&path_packfile,
371 packidx->path_packidx);
375 pack = got_repo_get_cached_pack(repo, path_packfile);
377 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
382 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
390 const struct got_error *
391 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
392 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
393 struct got_repository *repo)
395 return read_packed_object_privsep(obj, repo, pack, packidx,
399 const struct got_error *
400 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
401 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
402 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
403 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
404 struct got_repository *repo)
406 const struct got_error *err = NULL;
407 struct got_pack *pack = NULL;
413 *delta_compressed_size = 0;
415 *delta_out_offset = 0;
417 err = got_packidx_get_packfile_path(&path_packfile,
418 packidx->path_packidx);
422 pack = got_repo_get_cached_pack(repo, path_packfile);
424 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
429 if (pack->privsep_child == NULL) {
430 err = start_pack_privsep_child(pack, packidx);
435 if (!pack->child_has_delta_outfd) {
437 outfd_child = dup(delta_cache_fd);
438 if (outfd_child == -1)
439 return got_error_from_errno("dup");
440 err = got_privsep_send_raw_delta_outfd(
441 pack->privsep_child->ibuf, outfd_child);
444 pack->child_has_delta_outfd = 1;
447 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
452 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
453 delta_compressed_size, delta_offset, delta_out_offset, base_id,
454 pack->privsep_child->ibuf);
458 * XXX This function does not really belong in object.c. It is only here
459 * because it needs start_pack_privsep_child(); relevant code should
460 * probably be moved to pack.c/pack_create.c.
462 const struct got_error *
463 got_object_prepare_delta_reuse(struct got_pack **pack,
464 struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
466 const struct got_error *err = NULL;
467 char *path_packfile = NULL;
469 err = got_packidx_get_packfile_path(&path_packfile,
470 packidx->path_packidx);
474 *pack = got_repo_get_cached_pack(repo, path_packfile);
476 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
480 if ((*pack)->privsep_child == NULL) {
481 err = start_pack_privsep_child(*pack, packidx);
486 if (!(*pack)->child_has_delta_outfd) {
488 outfd_child = dup(delta_outfd);
489 if (outfd_child == -1) {
490 err = got_error_from_errno("dup");
493 err = got_privsep_send_raw_delta_outfd(
494 (*pack)->privsep_child->ibuf, outfd_child);
497 (*pack)->child_has_delta_outfd = 1;
500 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
506 static const struct got_error *
507 request_object(struct got_object **obj, struct got_object_id *id,
508 struct got_repository *repo, int fd)
510 const struct got_error *err = NULL;
511 struct imsgbuf *ibuf;
513 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
515 err = got_privsep_send_obj_req(ibuf, fd, id);
519 return got_privsep_recv_obj(obj, ibuf);
522 static const struct got_error *
523 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
524 struct got_object_id *id, struct got_repository *repo, int infd)
526 const struct got_error *err = NULL;
527 struct imsgbuf *ibuf;
530 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
532 outfd_child = dup(outfd);
533 if (outfd_child == -1)
534 return got_error_from_errno("dup");
536 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
540 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
544 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
547 static const struct got_error *
548 start_read_object_child(struct got_repository *repo)
550 const struct got_error *err = NULL;
553 struct imsgbuf *ibuf;
555 ibuf = calloc(1, sizeof(*ibuf));
557 return got_error_from_errno("calloc");
559 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
560 err = got_error_from_errno("socketpair");
567 err = got_error_from_errno("fork");
572 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
577 if (close(imsg_fds[1]) == -1) {
578 err = got_error_from_errno("close");
583 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
585 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
586 imsg_init(ibuf, imsg_fds[0]);
587 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
592 const struct got_error *
593 got_object_read_header_privsep(struct got_object **obj,
594 struct got_object_id *id, struct got_repository *repo, int obj_fd)
596 const struct got_error *err;
598 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
599 return request_object(obj, id, repo, obj_fd);
601 err = start_read_object_child(repo);
607 return request_object(obj, id, repo, obj_fd);
610 static const struct got_error *
611 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
612 int outfd, struct got_object_id *id, struct got_repository *repo,
615 const struct got_error *err;
617 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
618 return request_raw_object(outbuf, size, hdrlen, outfd, id,
621 err = start_read_object_child(repo);
625 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
629 const struct got_error *
630 got_object_open(struct got_object **obj, struct got_repository *repo,
631 struct got_object_id *id)
633 const struct got_error *err = NULL;
636 *obj = got_repo_get_cached_object(repo, id);
642 err = got_object_open_packed(obj, id, repo);
643 if (err && err->code != GOT_ERR_NO_OBJ)
647 return got_repo_cache_object(repo, id, *obj);
650 err = got_object_open_loose_fd(&fd, id, repo);
652 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
653 err = got_error_no_obj(id);
657 err = got_object_read_header_privsep(obj, id, repo, fd);
661 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
664 return got_repo_cache_object(repo, id, *obj);
667 /* *outfd must be initialized to -1 by caller */
668 const struct got_error *
669 got_object_raw_open(struct got_raw_object **obj, int *outfd,
670 struct got_repository *repo, struct got_object_id *id)
672 const struct got_error *err = NULL;
673 struct got_packidx *packidx = NULL;
675 uint8_t *outbuf = NULL;
678 char *path_packfile = NULL;
680 *obj = got_repo_get_cached_raw_object(repo, id);
687 *outfd = got_opentempfd();
689 return got_error_from_errno("got_opentempfd");
692 err = got_repo_search_packidx(&packidx, &idx, repo, id);
694 struct got_pack *pack = NULL;
696 err = got_packidx_get_packfile_path(&path_packfile,
697 packidx->path_packidx);
701 pack = got_repo_get_cached_pack(repo, path_packfile);
703 err = got_repo_cache_pack(&pack, repo, path_packfile,
708 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
709 *outfd, pack, packidx, idx, id);
712 } else if (err->code == GOT_ERR_NO_OBJ) {
715 err = got_object_open_loose_fd(&fd, id, repo);
718 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
724 *obj = calloc(1, sizeof(**obj));
726 err = got_error_from_errno("calloc");
732 (*obj)->data = outbuf;
735 if (fstat(*outfd, &sb) == -1) {
736 err = got_error_from_errno("fstat");
740 if (sb.st_size != hdrlen + size) {
741 err = got_error(GOT_ERR_PRIVSEP_LEN);
744 #ifndef GOT_PACK_NO_MMAP
745 if (hdrlen + size > 0) {
746 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
747 MAP_PRIVATE, *outfd, 0);
748 if ((*obj)->data == MAP_FAILED) {
749 if (errno != ENOMEM) {
750 err = got_error_from_errno("mmap");
761 (*obj)->f = fdopen(*outfd, "r");
762 if ((*obj)->f == NULL) {
763 err = got_error_from_errno("fdopen");
769 (*obj)->hdrlen = hdrlen;
771 err = got_repo_cache_raw_object(repo, id, *obj);
776 got_object_raw_close(*obj);
785 const struct got_error *
786 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
789 struct got_object_id id;
791 if (!got_parse_sha1_digest(id.sha1, id_str))
792 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
794 return got_object_open(obj, repo, &id);
797 const struct got_error *
798 got_object_resolve_id_str(struct got_object_id **id,
799 struct got_repository *repo, const char *id_str)
801 const struct got_error *err = NULL;
802 struct got_object *obj;
804 err = got_object_open_by_id_str(&obj, repo, id_str);
808 *id = got_object_id_dup(got_object_get_id(obj));
809 got_object_close(obj);
811 return got_error_from_errno("got_object_id_dup");
816 static const struct got_error *
817 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
818 int pack_idx, struct got_object_id *id)
820 const struct got_error *err = NULL;
822 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
827 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
831 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
835 static const struct got_error *
836 read_packed_commit_privsep(struct got_commit_object **commit,
837 struct got_pack *pack, struct got_packidx *packidx, int idx,
838 struct got_object_id *id)
840 const struct got_error *err = NULL;
842 if (pack->privsep_child)
843 return request_packed_commit(commit, pack, idx, id);
845 err = start_pack_privsep_child(pack, packidx);
849 return request_packed_commit(commit, pack, idx, id);
852 static const struct got_error *
853 request_commit(struct got_commit_object **commit, struct got_repository *repo,
854 int fd, struct got_object_id *id)
856 const struct got_error *err = NULL;
857 struct imsgbuf *ibuf;
859 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
861 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
865 return got_privsep_recv_commit(commit, ibuf);
868 static const struct got_error *
869 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
870 struct got_object_id *id, struct got_repository *repo)
872 const struct got_error *err;
875 struct imsgbuf *ibuf;
877 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
878 return request_commit(commit, repo, obj_fd, id);
880 ibuf = calloc(1, sizeof(*ibuf));
882 return got_error_from_errno("calloc");
884 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
885 err = got_error_from_errno("socketpair");
892 err = got_error_from_errno("fork");
897 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
902 if (close(imsg_fds[1]) == -1) {
903 err = got_error_from_errno("close");
907 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
909 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
910 imsg_init(ibuf, imsg_fds[0]);
911 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
913 return request_commit(commit, repo, obj_fd, id);
917 static const struct got_error *
918 open_commit(struct got_commit_object **commit,
919 struct got_repository *repo, struct got_object_id *id, int check_cache)
921 const struct got_error *err = NULL;
922 struct got_packidx *packidx = NULL;
924 char *path_packfile = NULL;
927 *commit = got_repo_get_cached_commit(repo, id);
928 if (*commit != NULL) {
935 err = got_repo_search_packidx(&packidx, &idx, repo, id);
937 struct got_pack *pack = NULL;
939 err = got_packidx_get_packfile_path(&path_packfile,
940 packidx->path_packidx);
944 pack = got_repo_get_cached_pack(repo, path_packfile);
946 err = got_repo_cache_pack(&pack, repo, path_packfile,
951 err = read_packed_commit_privsep(commit, pack,
953 } else if (err->code == GOT_ERR_NO_OBJ) {
956 err = got_object_open_loose_fd(&fd, id, repo);
959 err = read_commit_privsep(commit, fd, id, repo);
964 err = got_repo_cache_commit(repo, id, *commit);
971 const struct got_error *
972 got_object_open_as_commit(struct got_commit_object **commit,
973 struct got_repository *repo, struct got_object_id *id)
975 *commit = got_repo_get_cached_commit(repo, id);
976 if (*commit != NULL) {
981 return open_commit(commit, repo, id, 0);
984 const struct got_error *
985 got_object_commit_open(struct got_commit_object **commit,
986 struct got_repository *repo, struct got_object *obj)
988 return open_commit(commit, repo, got_object_get_id(obj), 1);
991 const struct got_error *
992 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
994 *qid = calloc(1, sizeof(**qid));
996 return got_error_from_errno("calloc");
998 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
1002 const struct got_error *
1003 got_object_id_queue_copy(const struct got_object_id_queue *src,
1004 struct got_object_id_queue *dest)
1006 const struct got_error *err;
1007 struct got_object_qid *qid;
1009 STAILQ_FOREACH(qid, src, entry) {
1010 struct got_object_qid *new;
1012 * Deep-copy the object ID only. Let the caller deal
1013 * with setting up the new->data pointer if needed.
1015 err = got_object_qid_alloc(&new, &qid->id);
1017 got_object_id_queue_free(dest);
1020 STAILQ_INSERT_TAIL(dest, new, entry);
1026 static const struct got_error *
1027 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
1028 int pack_idx, struct got_object_id *id)
1030 const struct got_error *err = NULL;
1032 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
1037 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1040 static const struct got_error *
1041 read_packed_tree_privsep(struct got_tree_object **tree,
1042 struct got_pack *pack, struct got_packidx *packidx, int idx,
1043 struct got_object_id *id)
1045 const struct got_error *err = NULL;
1047 if (pack->privsep_child)
1048 return request_packed_tree(tree, pack, idx, id);
1050 err = start_pack_privsep_child(pack, packidx);
1054 return request_packed_tree(tree, pack, idx, id);
1057 static const struct got_error *
1058 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1059 int fd, struct got_object_id *id)
1061 const struct got_error *err = NULL;
1062 struct imsgbuf *ibuf;
1064 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1066 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1070 return got_privsep_recv_tree(tree, ibuf);
1073 const struct got_error *
1074 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1075 struct got_object_id *id, struct got_repository *repo)
1077 const struct got_error *err;
1080 struct imsgbuf *ibuf;
1082 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1083 return request_tree(tree, repo, obj_fd, id);
1085 ibuf = calloc(1, sizeof(*ibuf));
1087 return got_error_from_errno("calloc");
1089 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1090 err = got_error_from_errno("socketpair");
1097 err = got_error_from_errno("fork");
1101 else if (pid == 0) {
1102 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1107 if (close(imsg_fds[1]) == -1) {
1108 err = got_error_from_errno("close");
1112 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1114 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1115 imsg_init(ibuf, imsg_fds[0]);
1116 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1119 return request_tree(tree, repo, obj_fd, id);
1122 static const struct got_error *
1123 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1124 struct got_object_id *id, int check_cache)
1126 const struct got_error *err = NULL;
1127 struct got_packidx *packidx = NULL;
1129 char *path_packfile = NULL;
1132 *tree = got_repo_get_cached_tree(repo, id);
1133 if (*tree != NULL) {
1140 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1142 struct got_pack *pack = NULL;
1144 err = got_packidx_get_packfile_path(&path_packfile,
1145 packidx->path_packidx);
1149 pack = got_repo_get_cached_pack(repo, path_packfile);
1151 err = got_repo_cache_pack(&pack, repo, path_packfile,
1156 err = read_packed_tree_privsep(tree, pack,
1158 } else if (err->code == GOT_ERR_NO_OBJ) {
1161 err = got_object_open_loose_fd(&fd, id, repo);
1164 err = read_tree_privsep(tree, fd, id, repo);
1169 err = got_repo_cache_tree(repo, id, *tree);
1172 free(path_packfile);
1176 const struct got_error *
1177 got_object_open_as_tree(struct got_tree_object **tree,
1178 struct got_repository *repo, struct got_object_id *id)
1180 *tree = got_repo_get_cached_tree(repo, id);
1181 if (*tree != NULL) {
1186 return open_tree(tree, repo, id, 0);
1189 const struct got_error *
1190 got_object_tree_open(struct got_tree_object **tree,
1191 struct got_repository *repo, struct got_object *obj)
1193 return open_tree(tree, repo, got_object_get_id(obj), 1);
1197 got_object_tree_get_nentries(struct got_tree_object *tree)
1199 return tree->nentries;
1202 struct got_tree_entry *
1203 got_object_tree_get_first_entry(struct got_tree_object *tree)
1205 return got_object_tree_get_entry(tree, 0);
1208 struct got_tree_entry *
1209 got_object_tree_get_last_entry(struct got_tree_object *tree)
1211 return got_object_tree_get_entry(tree, tree->nentries - 1);
1214 struct got_tree_entry *
1215 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1217 if (i < 0 || i >= tree->nentries)
1219 return &tree->entries[i];
1223 got_tree_entry_get_mode(struct got_tree_entry *te)
1229 got_tree_entry_get_name(struct got_tree_entry *te)
1231 return &te->name[0];
1234 struct got_object_id *
1235 got_tree_entry_get_id(struct got_tree_entry *te)
1240 const struct got_error *
1241 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1243 const struct got_error *err = NULL;
1244 size_t len, totlen, hdrlen, offset;
1248 hdrlen = got_object_blob_get_hdrlen(blob);
1254 err = got_object_blob_read_block(&len, blob);
1261 totlen += len - hdrlen;
1262 p = realloc(*s, totlen + 1);
1264 err = got_error_from_errno("realloc");
1270 /* Skip blob object header first time around. */
1272 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1277 (*s)[totlen] = '\0';
1281 const struct got_error *
1282 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1283 struct got_repository *repo)
1285 const struct got_error *err = NULL;
1286 struct got_blob_object *blob = NULL;
1288 *link_target = NULL;
1290 if (!got_object_tree_entry_is_symlink(te))
1291 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1293 err = got_object_open_as_blob(&blob, repo,
1294 got_tree_entry_get_id(te), PATH_MAX);
1298 err = got_object_blob_read_to_str(link_target, blob);
1299 got_object_blob_close(blob);
1302 *link_target = NULL;
1308 got_tree_entry_get_index(struct got_tree_entry *te)
1313 struct got_tree_entry *
1314 got_tree_entry_get_next(struct got_tree_object *tree,
1315 struct got_tree_entry *te)
1317 return got_object_tree_get_entry(tree, te->idx + 1);
1320 struct got_tree_entry *
1321 got_tree_entry_get_prev(struct got_tree_object *tree,
1322 struct got_tree_entry *te)
1324 return got_object_tree_get_entry(tree, te->idx - 1);
1327 static const struct got_error *
1328 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1329 struct got_pack *pack, struct got_packidx *packidx, int idx,
1330 struct got_object_id *id)
1332 const struct got_error *err = NULL;
1333 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1336 err = pack_child_send_tempfiles(ibuf, pack);
1340 outfd_child = dup(outfd);
1341 if (outfd_child == -1)
1342 return got_error_from_errno("dup");
1344 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1348 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1354 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1355 pack->privsep_child->ibuf);
1359 if (lseek(outfd, SEEK_SET, 0) == -1)
1360 err = got_error_from_errno("lseek");
1365 static const struct got_error *
1366 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1367 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1368 struct got_object_id *id)
1370 const struct got_error *err = NULL;
1372 if (pack->privsep_child == NULL) {
1373 err = start_pack_privsep_child(pack, packidx);
1378 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1382 static const struct got_error *
1383 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1384 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1386 const struct got_error *err = NULL;
1389 outfd_child = dup(outfd);
1390 if (outfd_child == -1)
1391 return got_error_from_errno("dup");
1393 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1397 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1401 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1405 if (lseek(outfd, SEEK_SET, 0) == -1)
1406 return got_error_from_errno("lseek");
1411 static const struct got_error *
1412 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1413 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1415 const struct got_error *err;
1418 struct imsgbuf *ibuf;
1420 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1421 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1422 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1426 ibuf = calloc(1, sizeof(*ibuf));
1428 return got_error_from_errno("calloc");
1430 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1431 err = got_error_from_errno("socketpair");
1438 err = got_error_from_errno("fork");
1442 else if (pid == 0) {
1443 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1448 if (close(imsg_fds[1]) == -1) {
1449 err = got_error_from_errno("close");
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1455 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1456 imsg_init(ibuf, imsg_fds[0]);
1457 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1459 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1462 static const struct got_error *
1463 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1464 struct got_object_id *id, size_t blocksize)
1466 const struct got_error *err = NULL;
1467 struct got_packidx *packidx = NULL;
1469 char *path_packfile = NULL;
1472 size_t size, hdrlen;
1475 *blob = calloc(1, sizeof(**blob));
1477 return got_error_from_errno("calloc");
1479 outfd = got_opentempfd();
1481 return got_error_from_errno("got_opentempfd");
1483 (*blob)->read_buf = malloc(blocksize);
1484 if ((*blob)->read_buf == NULL) {
1485 err = got_error_from_errno("malloc");
1489 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1491 struct got_pack *pack = NULL;
1493 err = got_packidx_get_packfile_path(&path_packfile,
1494 packidx->path_packidx);
1498 pack = got_repo_get_cached_pack(repo, path_packfile);
1500 err = got_repo_cache_pack(&pack, repo, path_packfile,
1505 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1506 pack, packidx, idx, id);
1507 } else if (err->code == GOT_ERR_NO_OBJ) {
1510 err = got_object_open_loose_fd(&infd, id, repo);
1513 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1519 if (hdrlen > size) {
1520 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1525 if (close(outfd) == -1 && err == NULL)
1526 err = got_error_from_errno("close");
1528 (*blob)->f = fmemopen(outbuf, size, "rb");
1529 if ((*blob)->f == NULL) {
1530 err = got_error_from_errno("fmemopen");
1534 (*blob)->data = outbuf;
1536 if (fstat(outfd, &sb) == -1) {
1537 err = got_error_from_errno("fstat");
1541 if (sb.st_size != size) {
1542 err = got_error(GOT_ERR_PRIVSEP_LEN);
1546 (*blob)->f = fdopen(outfd, "rb");
1547 if ((*blob)->f == NULL) {
1548 err = got_error_from_errno("fdopen");
1555 (*blob)->hdrlen = hdrlen;
1556 (*blob)->blocksize = blocksize;
1557 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1560 free(path_packfile);
1563 got_object_blob_close(*blob);
1565 } else if (outfd != -1)
1571 const struct got_error *
1572 got_object_open_as_blob(struct got_blob_object **blob,
1573 struct got_repository *repo, struct got_object_id *id,
1576 return open_blob(blob, repo, id, blocksize);
1579 const struct got_error *
1580 got_object_blob_open(struct got_blob_object **blob,
1581 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1583 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1586 const struct got_error *
1587 got_object_blob_close(struct got_blob_object *blob)
1589 const struct got_error *err = NULL;
1590 free(blob->read_buf);
1591 if (blob->f && fclose(blob->f) == EOF)
1592 err = got_error_from_errno("fclose");
1599 got_object_blob_rewind(struct got_blob_object *blob)
1606 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1608 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1612 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1614 return blob->hdrlen;
1618 got_object_blob_get_read_buf(struct got_blob_object *blob)
1620 return blob->read_buf;
1623 const struct got_error *
1624 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1628 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1629 if (n == 0 && ferror(blob->f))
1630 return got_ferror(blob->f, GOT_ERR_IO);
1635 const struct got_error *
1636 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1637 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1639 const struct got_error *err = NULL;
1640 size_t n, len, hdrlen;
1643 const int alloc_chunksz = 512;
1645 off_t off = 0, total_len = 0;
1648 *line_offsets = NULL;
1654 hdrlen = got_object_blob_get_hdrlen(blob);
1656 err = got_object_blob_read_block(&len, blob);
1661 buf = got_object_blob_get_read_buf(blob);
1664 if (line_offsets && *line_offsets == NULL) {
1665 /* Have some data but perhaps no '\n'. */
1667 nalloc = alloc_chunksz;
1668 *line_offsets = calloc(nalloc,
1669 sizeof(**line_offsets));
1670 if (*line_offsets == NULL)
1671 return got_error_from_errno("calloc");
1673 /* Skip forward over end of first line. */
1680 /* Scan '\n' offsets in remaining chunk of data. */
1682 if (buf[i] != '\n') {
1687 if (line_offsets && nalloc < *nlines) {
1688 size_t n = *nlines + alloc_chunksz;
1689 off_t *o = recallocarray(*line_offsets,
1690 nalloc, n, sizeof(**line_offsets));
1692 free(*line_offsets);
1693 *line_offsets = NULL;
1694 return got_error_from_errno(
1701 off = total_len + i - hdrlen + 1;
1702 (*line_offsets)[*nlines - 1] = off;
1707 /* Skip blob object header first time around. */
1708 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1709 if (n != len - hdrlen)
1710 return got_ferror(outfile, GOT_ERR_IO);
1711 total_len += len - hdrlen;
1715 if (fflush(outfile) != 0)
1716 return got_error_from_errno("fflush");
1720 *filesize = total_len;
1725 static const struct got_error *
1726 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1727 int pack_idx, struct got_object_id *id)
1729 const struct got_error *err = NULL;
1731 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1736 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1739 static const struct got_error *
1740 read_packed_tag_privsep(struct got_tag_object **tag,
1741 struct got_pack *pack, struct got_packidx *packidx, int idx,
1742 struct got_object_id *id)
1744 const struct got_error *err = NULL;
1746 if (pack->privsep_child)
1747 return request_packed_tag(tag, pack, idx, id);
1749 err = start_pack_privsep_child(pack, packidx);
1753 return request_packed_tag(tag, pack, idx, id);
1756 static const struct got_error *
1757 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1758 int fd, struct got_object_id *id)
1760 const struct got_error *err = NULL;
1761 struct imsgbuf *ibuf;
1763 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1765 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1769 return got_privsep_recv_tag(tag, ibuf);
1772 static const struct got_error *
1773 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1774 struct got_object_id *id, struct got_repository *repo)
1776 const struct got_error *err;
1779 struct imsgbuf *ibuf;
1781 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1782 return request_tag(tag, repo, obj_fd, id);
1784 ibuf = calloc(1, sizeof(*ibuf));
1786 return got_error_from_errno("calloc");
1788 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1789 err = got_error_from_errno("socketpair");
1796 err = got_error_from_errno("fork");
1800 else if (pid == 0) {
1801 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1806 if (close(imsg_fds[1]) == -1) {
1807 err = got_error_from_errno("close");
1811 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1814 imsg_init(ibuf, imsg_fds[0]);
1815 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1817 return request_tag(tag, repo, obj_fd, id);
1820 static const struct got_error *
1821 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1822 struct got_object_id *id, int check_cache)
1824 const struct got_error *err = NULL;
1825 struct got_packidx *packidx = NULL;
1827 char *path_packfile = NULL;
1828 struct got_object *obj = NULL;
1829 int obj_type = GOT_OBJ_TYPE_ANY;
1832 *tag = got_repo_get_cached_tag(repo, id);
1840 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1842 struct got_pack *pack = NULL;
1844 err = got_packidx_get_packfile_path(&path_packfile,
1845 packidx->path_packidx);
1849 pack = got_repo_get_cached_pack(repo, path_packfile);
1851 err = got_repo_cache_pack(&pack, repo, path_packfile,
1857 /* Beware of "lightweight" tags: Check object type first. */
1858 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1862 obj_type = obj->type;
1863 got_object_close(obj);
1864 if (obj_type != GOT_OBJ_TYPE_TAG) {
1865 err = got_error(GOT_ERR_OBJ_TYPE);
1868 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1869 } else if (err->code == GOT_ERR_NO_OBJ) {
1872 err = got_object_open_loose_fd(&fd, id, repo);
1875 err = got_object_read_header_privsep(&obj, id, repo, fd);
1878 obj_type = obj->type;
1879 got_object_close(obj);
1880 if (obj_type != GOT_OBJ_TYPE_TAG)
1881 return got_error(GOT_ERR_OBJ_TYPE);
1883 err = got_object_open_loose_fd(&fd, id, repo);
1886 err = read_tag_privsep(tag, fd, id, repo);
1891 err = got_repo_cache_tag(repo, id, *tag);
1894 free(path_packfile);
1898 const struct got_error *
1899 got_object_open_as_tag(struct got_tag_object **tag,
1900 struct got_repository *repo, struct got_object_id *id)
1902 *tag = got_repo_get_cached_tag(repo, id);
1908 return open_tag(tag, repo, id, 0);
1911 const struct got_error *
1912 got_object_tag_open(struct got_tag_object **tag,
1913 struct got_repository *repo, struct got_object *obj)
1915 return open_tag(tag, repo, got_object_get_id(obj), 1);
1919 got_object_tag_get_name(struct got_tag_object *tag)
1925 got_object_tag_get_object_type(struct got_tag_object *tag)
1927 return tag->obj_type;
1930 struct got_object_id *
1931 got_object_tag_get_object_id(struct got_tag_object *tag)
1937 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1939 return tag->tagger_time;
1943 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1945 return tag->tagger_gmtoff;
1949 got_object_tag_get_tagger(struct got_tag_object *tag)
1955 got_object_tag_get_message(struct got_tag_object *tag)
1960 static struct got_tree_entry *
1961 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1965 /* Note that tree entries are sorted in strncmp() order. */
1966 for (i = 0; i < tree->nentries; i++) {
1967 struct got_tree_entry *te = &tree->entries[i];
1968 int cmp = strncmp(te->name, name, len);
1973 if (te->name[len] == '\0')
1979 struct got_tree_entry *
1980 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1982 return find_entry_by_name(tree, name, strlen(name));
1985 const struct got_error *
1986 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1987 struct got_repository *repo, struct got_tree_object *tree,
1990 const struct got_error *err = NULL;
1991 struct got_tree_object *subtree = NULL;
1992 struct got_tree_entry *te = NULL;
1993 const char *seg, *s;
2005 struct got_tree_object *next_tree;
2014 te = find_entry_by_name(subtree, seg, seglen);
2016 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2027 err = got_object_open_as_tree(&next_tree, repo,
2032 if (subtree != tree)
2033 got_object_tree_close(subtree);
2034 subtree = next_tree;
2039 *id = got_object_id_dup(&te->id);
2041 return got_error_from_errno("got_object_id_dup");
2045 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2047 if (subtree && subtree != tree)
2048 got_object_tree_close(subtree);
2051 const struct got_error *
2052 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
2053 struct got_commit_object *commit, const char *path)
2055 const struct got_error *err = NULL;
2056 struct got_tree_object *tree = NULL;
2060 /* Handle opening of root of commit's tree. */
2061 if (got_path_is_root_dir(path)) {
2062 *id = got_object_id_dup(commit->tree_id);
2064 err = got_error_from_errno("got_object_id_dup");
2066 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2069 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2073 got_object_tree_close(tree);
2078 * Normalize file mode bits to avoid false positive tree entry differences
2079 * in case tree entries have unexpected mode bits set.
2082 normalize_mode_for_comparison(mode_t mode)
2085 * For directories, the only relevant bit is the IFDIR bit.
2086 * This allows us to detect paths changing from a directory
2087 * to a file and vice versa.
2090 return mode & S_IFDIR;
2093 * For symlinks, the only relevant bit is the IFLNK bit.
2094 * This allows us to detect paths changing from a symlinks
2095 * to a file or directory and vice versa.
2098 return mode & S_IFLNK;
2100 /* For files, the only change we care about is the executable bit. */
2101 return mode & S_IXUSR;
2104 const struct got_error *
2105 got_object_tree_path_changed(int *changed,
2106 struct got_tree_object *tree01, struct got_tree_object *tree02,
2107 const char *path, struct got_repository *repo)
2109 const struct got_error *err = NULL;
2110 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2111 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2112 const char *seg, *s;
2117 /* We not do support comparing the root path. */
2118 if (got_path_is_root_dir(path))
2119 return got_error_path(path, GOT_ERR_BAD_PATH);
2129 struct got_tree_object *next_tree1, *next_tree2;
2130 mode_t mode1, mode2;
2139 te1 = find_entry_by_name(tree1, seg, seglen);
2141 err = got_error(GOT_ERR_NO_OBJ);
2146 te2 = find_entry_by_name(tree2, seg, seglen);
2149 mode1 = normalize_mode_for_comparison(te1->mode);
2150 mode2 = normalize_mode_for_comparison(te2->mode);
2151 if (mode1 != mode2) {
2156 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2162 if (*s == '\0') { /* final path element */
2171 err = got_object_open_as_tree(&next_tree1, repo,
2176 if (tree1 != tree01)
2177 got_object_tree_close(tree1);
2181 err = got_object_open_as_tree(&next_tree2, repo,
2186 if (tree2 != tree02)
2187 got_object_tree_close(tree2);
2190 if (tree2 != tree02)
2191 got_object_tree_close(tree2);
2197 if (tree1 && tree1 != tree01)
2198 got_object_tree_close(tree1);
2199 if (tree2 && tree2 != tree02)
2200 got_object_tree_close(tree2);
2204 const struct got_error *
2205 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2206 struct got_tree_entry *te)
2208 const struct got_error *err = NULL;
2210 *new_te = calloc(1, sizeof(**new_te));
2211 if (*new_te == NULL)
2212 return got_error_from_errno("calloc");
2214 (*new_te)->mode = te->mode;
2215 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2216 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2221 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2223 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2227 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2229 /* S_IFDIR check avoids confusing symlinks with submodules. */
2230 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2233 static const struct got_error *
2234 resolve_symlink(char **link_target, const char *path,
2235 struct got_commit_object *commit, struct got_repository *repo)
2237 const struct got_error *err = NULL;
2239 char *name, *parent_path = NULL;
2240 struct got_object_id *tree_obj_id = NULL;
2241 struct got_tree_object *tree = NULL;
2242 struct got_tree_entry *te = NULL;
2244 *link_target = NULL;
2246 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2247 return got_error(GOT_ERR_NO_SPACE);
2249 name = basename(buf);
2251 return got_error_from_errno2("basename", path);
2253 err = got_path_dirname(&parent_path, path);
2257 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2260 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2261 /* Display the complete path in error message. */
2262 err = got_error_path(path, err->code);
2267 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2271 te = got_object_tree_find_entry(tree, name);
2273 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2277 if (got_object_tree_entry_is_symlink(te)) {
2278 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2281 if (!got_path_is_absolute(*link_target)) {
2283 if (asprintf(&abspath, "%s/%s", parent_path,
2284 *link_target) == -1) {
2285 err = got_error_from_errno("asprintf");
2289 *link_target = malloc(PATH_MAX);
2290 if (*link_target == NULL) {
2291 err = got_error_from_errno("malloc");
2294 err = got_canonpath(abspath, *link_target, PATH_MAX);
2303 got_object_tree_close(tree);
2306 *link_target = NULL;
2311 const struct got_error *
2312 got_object_resolve_symlinks(char **link_target, const char *path,
2313 struct got_commit_object *commit, struct got_repository *repo)
2315 const struct got_error *err = NULL;
2316 char *next_target = NULL;
2317 int max_recursion = 40; /* matches Git */
2319 *link_target = NULL;
2322 err = resolve_symlink(&next_target,
2323 *link_target ? *link_target : path, commit, repo);
2328 if (--max_recursion == 0) {
2329 err = got_error_path(path, GOT_ERR_RECURSION);
2330 *link_target = NULL;
2333 *link_target = next_target;
2335 } while (next_target);
2340 const struct got_error *
2341 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2342 struct got_object_id *commit_id, const char *path,
2343 struct got_repository *repo)
2345 const struct got_error *err = NULL;
2346 struct got_pack *pack = NULL;
2347 struct got_packidx *packidx = NULL;
2348 char *path_packfile = NULL;
2349 struct got_commit_object *changed_commit = NULL;
2350 struct got_object_id *changed_commit_id = NULL;
2353 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2355 if (err->code != GOT_ERR_NO_OBJ)
2360 err = got_packidx_get_packfile_path(&path_packfile,
2361 packidx->path_packidx);
2365 pack = got_repo_get_cached_pack(repo, path_packfile);
2367 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2372 if (pack->privsep_child == NULL) {
2373 err = start_pack_privsep_child(pack, packidx);
2378 err = got_privsep_send_commit_traversal_request(
2379 pack->privsep_child->ibuf, commit_id, idx, path);
2383 err = got_privsep_recv_traversed_commits(&changed_commit,
2384 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2388 if (changed_commit) {
2390 * Cache the commit in which the path was changed.
2391 * This commit might be opened again soon.
2393 changed_commit->refcnt++;
2394 err = got_repo_cache_commit(repo, changed_commit_id,
2396 got_object_commit_close(changed_commit);
2399 free(path_packfile);
2400 free(changed_commit_id);
2404 const struct got_error *
2405 got_object_enumerate(int *found_all_objects,
2406 got_object_enumerate_commit_cb cb_commit,
2407 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2408 struct got_object_id **ours, int nours,
2409 struct got_object_id **theirs, int ntheirs,
2410 struct got_packidx *packidx, struct got_repository *repo)
2412 const struct got_error *err = NULL;
2413 struct got_pack *pack;
2414 char *path_packfile = NULL;
2416 err = got_packidx_get_packfile_path(&path_packfile,
2417 packidx->path_packidx);
2421 pack = got_repo_get_cached_pack(repo, path_packfile);
2423 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2428 if (pack->privsep_child == NULL) {
2429 err = start_pack_privsep_child(pack, packidx);
2434 err = got_privsep_send_object_enumeration_request(
2435 pack->privsep_child->ibuf);
2439 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2443 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2447 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2451 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2455 err = got_privsep_recv_enumerated_objects(found_all_objects,
2456 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2458 free(path_packfile);