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>
21 #include <sys/socket.h>
23 #include <sys/syslimits.h>
24 #include <sys/resource.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
71 return got_object_id_str(outbuf, &obj->id);
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
93 err = got_error(GOT_ERR_OBJ_TYPE);
97 got_object_close(obj);
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
129 static const struct got_error *
130 open_loose_object(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
136 err = got_object_get_path(&path, id, repo);
139 *fd = open(path, O_RDONLY | O_NOFOLLOW);
141 err = got_error_from_errno2("open", path);
149 static const struct got_error *
150 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
154 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
155 size = strlen(packidx->path_packidx) + 2;
156 if (size < GOT_PACKFILE_NAMELEN + 1)
157 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
159 *path_packfile = malloc(size);
160 if (*path_packfile == NULL)
161 return got_error_from_errno("malloc");
163 /* Copy up to and excluding ".idx". */
164 if (strlcpy(*path_packfile, packidx->path_packidx,
165 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
166 return got_error(GOT_ERR_NO_SPACE);
168 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
169 return got_error(GOT_ERR_NO_SPACE);
174 static const struct got_error *
175 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
176 struct got_object_id *id)
178 const struct got_error *err = NULL;
179 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
181 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
185 err = got_privsep_recv_obj(obj, ibuf);
189 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
195 set_max_datasize(void)
199 if (getrlimit(RLIMIT_DATA, &rl) != 0)
202 rl.rlim_cur = rl.rlim_max;
203 setrlimit(RLIMIT_DATA, &rl);
206 static const struct got_error *
207 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
209 const struct got_error *err = NULL;
212 struct imsgbuf *ibuf;
214 ibuf = calloc(1, sizeof(*ibuf));
216 return got_error_from_errno("calloc");
218 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
219 if (pack->privsep_child == NULL) {
220 err = got_error_from_errno("calloc");
225 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
226 err = got_error_from_errno("socketpair");
232 err = got_error_from_errno("fork");
234 } else if (pid == 0) {
236 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
237 pack->path_packfile);
241 if (close(imsg_fds[1]) != 0)
242 return got_error_from_errno("close");
243 pack->privsep_child->imsg_fd = imsg_fds[0];
244 pack->privsep_child->pid = pid;
245 imsg_init(ibuf, imsg_fds[0]);
246 pack->privsep_child->ibuf = ibuf;
248 err = got_privsep_init_pack_child(ibuf, pack, packidx);
250 const struct got_error *child_err;
251 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
252 child_err = got_privsep_wait_for_child(
253 pack->privsep_child->pid);
254 if (child_err && err == NULL)
260 free(pack->privsep_child);
261 pack->privsep_child = NULL;
266 static const struct got_error *
267 read_packed_object_privsep(struct got_object **obj,
268 struct got_repository *repo, struct got_pack *pack,
269 struct got_packidx *packidx, int idx, struct got_object_id *id)
271 const struct got_error *err = NULL;
273 if (pack->privsep_child)
274 return request_packed_object(obj, pack, idx, id);
276 err = start_pack_privsep_child(pack, packidx);
280 return request_packed_object(obj, pack, idx, id);
284 static const struct got_error *
285 open_packed_object(struct got_object **obj, struct got_object_id *id,
286 struct got_repository *repo)
288 const struct got_error *err = NULL;
289 struct got_pack *pack = NULL;
290 struct got_packidx *packidx = NULL;
294 err = got_repo_search_packidx(&packidx, &idx, repo, id);
298 err = get_packfile_path(&path_packfile, packidx);
302 pack = got_repo_get_cached_pack(repo, path_packfile);
304 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
309 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
317 static const struct got_error *
318 request_object(struct got_object **obj, struct got_repository *repo, int fd)
320 const struct got_error *err = NULL;
321 struct imsgbuf *ibuf;
323 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
325 err = got_privsep_send_obj_req(ibuf, fd);
329 return got_privsep_recv_obj(obj, ibuf);
332 static const struct got_error *
333 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
336 const struct got_error *err;
339 struct imsgbuf *ibuf;
341 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
342 return request_object(obj, repo, obj_fd);
344 ibuf = calloc(1, sizeof(*ibuf));
346 return got_error_from_errno("calloc");
348 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
349 err = got_error_from_errno("socketpair");
356 err = got_error_from_errno("fork");
361 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
366 if (close(imsg_fds[1]) != 0) {
367 err = got_error_from_errno("close");
371 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
374 imsg_init(ibuf, imsg_fds[0]);
375 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
377 return request_object(obj, repo, obj_fd);
381 const struct got_error *
382 got_object_open(struct got_object **obj, struct got_repository *repo,
383 struct got_object_id *id)
385 const struct got_error *err = NULL;
389 *obj = got_repo_get_cached_object(repo, id);
395 err = open_packed_object(obj, id, repo);
396 if (err && err->code != GOT_ERR_NO_OBJ)
400 return got_repo_cache_object(repo, id, *obj);
403 err = got_object_get_path(&path, id, repo);
407 fd = open(path, O_RDONLY | O_NOFOLLOW);
410 err = got_error_no_obj(id);
412 err = got_error_from_errno2("open", path);
415 err = read_object_header_privsep(obj, repo, fd);
418 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
422 err = got_repo_cache_object(repo, id, *obj);
429 const struct got_error *
430 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
433 struct got_object_id id;
435 if (!got_parse_sha1_digest(id.sha1, id_str))
436 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
438 return got_object_open(obj, repo, &id);
441 const struct got_error *
442 got_object_resolve_id_str(struct got_object_id **id,
443 struct got_repository *repo, const char *id_str)
445 const struct got_error *err = NULL;
446 struct got_object *obj;
448 err = got_object_open_by_id_str(&obj, repo, id_str);
452 *id = got_object_id_dup(got_object_get_id(obj));
453 got_object_close(obj);
455 return got_error_from_errno("got_object_id_dup");
460 static const struct got_error *
461 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
462 int pack_idx, struct got_object_id *id)
464 const struct got_error *err = NULL;
466 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
471 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
475 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
479 static const struct got_error *
480 read_packed_commit_privsep(struct got_commit_object **commit,
481 struct got_pack *pack, struct got_packidx *packidx, int idx,
482 struct got_object_id *id)
484 const struct got_error *err = NULL;
486 if (pack->privsep_child)
487 return request_packed_commit(commit, pack, idx, id);
489 err = start_pack_privsep_child(pack, packidx);
493 return request_packed_commit(commit, pack, idx, id);
496 static const struct got_error *
497 request_commit(struct got_commit_object **commit, struct got_repository *repo,
500 const struct got_error *err = NULL;
501 struct imsgbuf *ibuf;
503 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
505 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
509 return got_privsep_recv_commit(commit, ibuf);
512 static const struct got_error *
513 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
514 struct got_repository *repo)
516 const struct got_error *err;
519 struct imsgbuf *ibuf;
521 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
522 return request_commit(commit, repo, obj_fd);
524 ibuf = calloc(1, sizeof(*ibuf));
526 return got_error_from_errno("calloc");
528 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
529 err = got_error_from_errno("socketpair");
536 err = got_error_from_errno("fork");
541 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
546 if (close(imsg_fds[1]) != 0) {
547 err = got_error_from_errno("close");
551 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
553 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
554 imsg_init(ibuf, imsg_fds[0]);
555 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
557 return request_commit(commit, repo, obj_fd);
561 static const struct got_error *
562 open_commit(struct got_commit_object **commit,
563 struct got_repository *repo, struct got_object_id *id, int check_cache)
565 const struct got_error *err = NULL;
566 struct got_packidx *packidx = NULL;
568 char *path_packfile = NULL;
571 *commit = got_repo_get_cached_commit(repo, id);
572 if (*commit != NULL) {
579 err = got_repo_search_packidx(&packidx, &idx, repo, id);
581 struct got_pack *pack = NULL;
583 err = get_packfile_path(&path_packfile, packidx);
587 pack = got_repo_get_cached_pack(repo, path_packfile);
589 err = got_repo_cache_pack(&pack, repo, path_packfile,
594 err = read_packed_commit_privsep(commit, pack,
596 } else if (err->code == GOT_ERR_NO_OBJ) {
599 err = open_loose_object(&fd, id, repo);
602 err = read_commit_privsep(commit, fd, repo);
607 err = got_repo_cache_commit(repo, id, *commit);
614 const struct got_error *
615 got_object_open_as_commit(struct got_commit_object **commit,
616 struct got_repository *repo, struct got_object_id *id)
618 *commit = got_repo_get_cached_commit(repo, id);
619 if (*commit != NULL) {
624 return open_commit(commit, repo, id, 0);
627 const struct got_error *
628 got_object_commit_open(struct got_commit_object **commit,
629 struct got_repository *repo, struct got_object *obj)
631 return open_commit(commit, repo, got_object_get_id(obj), 1);
634 const struct got_error *
635 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
637 const struct got_error *err = NULL;
639 *qid = calloc(1, sizeof(**qid));
641 return got_error_from_errno("calloc");
643 (*qid)->id = got_object_id_dup(id);
644 if ((*qid)->id == NULL) {
645 err = got_error_from_errno("got_object_id_dup");
646 got_object_qid_free(*qid);
654 static const struct got_error *
655 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
656 int pack_idx, struct got_object_id *id)
658 const struct got_error *err = NULL;
660 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
665 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
668 static const struct got_error *
669 read_packed_tree_privsep(struct got_tree_object **tree,
670 struct got_pack *pack, struct got_packidx *packidx, int idx,
671 struct got_object_id *id)
673 const struct got_error *err = NULL;
675 if (pack->privsep_child)
676 return request_packed_tree(tree, pack, idx, id);
678 err = start_pack_privsep_child(pack, packidx);
682 return request_packed_tree(tree, pack, idx, id);
685 static const struct got_error *
686 request_tree(struct got_tree_object **tree, struct got_repository *repo,
689 const struct got_error *err = NULL;
690 struct imsgbuf *ibuf;
692 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
694 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
698 return got_privsep_recv_tree(tree, ibuf);
701 const struct got_error *
702 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
703 struct got_repository *repo)
705 const struct got_error *err;
708 struct imsgbuf *ibuf;
710 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
711 return request_tree(tree, repo, obj_fd);
713 ibuf = calloc(1, sizeof(*ibuf));
715 return got_error_from_errno("calloc");
717 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
718 err = got_error_from_errno("socketpair");
725 err = got_error_from_errno("fork");
730 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
735 if (close(imsg_fds[1]) != 0) {
736 err = got_error_from_errno("close");
740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
743 imsg_init(ibuf, imsg_fds[0]);
744 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
747 return request_tree(tree, repo, obj_fd);
750 static const struct got_error *
751 open_tree(struct got_tree_object **tree, struct got_repository *repo,
752 struct got_object_id *id, int check_cache)
754 const struct got_error *err = NULL;
755 struct got_packidx *packidx = NULL;
757 char *path_packfile = NULL;
760 *tree = got_repo_get_cached_tree(repo, id);
768 err = got_repo_search_packidx(&packidx, &idx, repo, id);
770 struct got_pack *pack = NULL;
772 err = get_packfile_path(&path_packfile, packidx);
776 pack = got_repo_get_cached_pack(repo, path_packfile);
778 err = got_repo_cache_pack(&pack, repo, path_packfile,
783 err = read_packed_tree_privsep(tree, pack,
785 } else if (err->code == GOT_ERR_NO_OBJ) {
788 err = open_loose_object(&fd, id, repo);
791 err = read_tree_privsep(tree, fd, repo);
796 err = got_repo_cache_tree(repo, id, *tree);
803 const struct got_error *
804 got_object_open_as_tree(struct got_tree_object **tree,
805 struct got_repository *repo, struct got_object_id *id)
807 *tree = got_repo_get_cached_tree(repo, id);
813 return open_tree(tree, repo, id, 0);
816 const struct got_error *
817 got_object_tree_open(struct got_tree_object **tree,
818 struct got_repository *repo, struct got_object *obj)
820 return open_tree(tree, repo, got_object_get_id(obj), 1);
824 got_object_tree_get_nentries(struct got_tree_object *tree)
826 return tree->nentries;
829 struct got_tree_entry *
830 got_object_tree_get_first_entry(struct got_tree_object *tree)
832 return got_object_tree_get_entry(tree, 0);
835 struct got_tree_entry *
836 got_object_tree_get_last_entry(struct got_tree_object *tree)
838 return got_object_tree_get_entry(tree, tree->nentries - 1);
841 struct got_tree_entry *
842 got_object_tree_get_entry(struct got_tree_object *tree, int i)
844 if (i < 0 || i >= tree->nentries)
846 return &tree->entries[i];
850 got_tree_entry_get_mode(struct got_tree_entry *te)
856 got_tree_entry_get_name(struct got_tree_entry *te)
861 struct got_object_id *
862 got_tree_entry_get_id(struct got_tree_entry *te)
867 const struct got_error *
868 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
870 const struct got_error *err = NULL;
871 size_t len, totlen, hdrlen, offset;
875 hdrlen = got_object_blob_get_hdrlen(blob);
881 err = got_object_blob_read_block(&len, blob);
888 totlen += len - hdrlen;
889 p = realloc(*s, totlen + 1);
891 err = got_error_from_errno("realloc");
897 /* Skip blob object header first time around. */
899 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
908 const struct got_error *
909 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
910 struct got_repository *repo)
912 const struct got_error *err = NULL;
913 struct got_blob_object *blob = NULL;
917 if (!got_object_tree_entry_is_symlink(te))
918 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
920 err = got_object_open_as_blob(&blob, repo,
921 got_tree_entry_get_id(te), PATH_MAX);
925 err = got_object_blob_read_to_str(link_target, blob);
926 got_object_blob_close(blob);
935 got_tree_entry_get_index(struct got_tree_entry *te)
940 struct got_tree_entry *
941 got_tree_entry_get_next(struct got_tree_object *tree,
942 struct got_tree_entry *te)
944 return got_object_tree_get_entry(tree, te->idx + 1);
947 struct got_tree_entry *
948 got_tree_entry_get_prev(struct got_tree_object *tree,
949 struct got_tree_entry *te)
951 return got_object_tree_get_entry(tree, te->idx - 1);
954 static const struct got_error *
955 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
956 struct got_pack *pack, struct got_packidx *packidx, int idx,
957 struct got_object_id *id)
959 const struct got_error *err = NULL;
961 int basefd, accumfd; /* temporary files for delta application */
963 basefd = got_opentempfd();
965 return got_error_from_errno("got_opentempfd");
966 accumfd = got_opentempfd();
968 return got_error_from_errno("got_opentempfd");
970 outfd_child = dup(outfd);
971 if (outfd_child == -1)
972 return got_error_from_errno("dup");
974 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
978 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
986 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
993 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
998 err = got_privsep_recv_blob(outbuf, size, hdrlen,
999 pack->privsep_child->ibuf);
1003 if (lseek(outfd, SEEK_SET, 0) == -1)
1004 err = got_error_from_errno("lseek");
1009 static const struct got_error *
1010 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1011 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1012 struct got_object_id *id)
1014 const struct got_error *err = NULL;
1016 if (pack->privsep_child == NULL) {
1017 err = start_pack_privsep_child(pack, packidx);
1022 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1026 static const struct got_error *
1027 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1028 int infd, struct imsgbuf *ibuf)
1030 const struct got_error *err = NULL;
1033 outfd_child = dup(outfd);
1034 if (outfd_child == -1)
1035 return got_error_from_errno("dup");
1037 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1041 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1045 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1049 if (lseek(outfd, SEEK_SET, 0) == -1)
1050 return got_error_from_errno("lseek");
1055 static const struct got_error *
1056 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1057 int outfd, int infd, struct got_repository *repo)
1059 const struct got_error *err;
1062 struct imsgbuf *ibuf;
1064 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1065 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1066 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1069 ibuf = calloc(1, sizeof(*ibuf));
1071 return got_error_from_errno("calloc");
1073 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1074 err = got_error_from_errno("socketpair");
1081 err = got_error_from_errno("fork");
1085 else if (pid == 0) {
1086 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1091 if (close(imsg_fds[1]) != 0) {
1092 err = got_error_from_errno("close");
1096 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1098 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1099 imsg_init(ibuf, imsg_fds[0]);
1100 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1102 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1105 static const struct got_error *
1106 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1107 struct got_object_id *id, size_t blocksize)
1109 const struct got_error *err = NULL;
1110 struct got_packidx *packidx = NULL;
1112 char *path_packfile = NULL;
1115 size_t size, hdrlen;
1118 *blob = calloc(1, sizeof(**blob));
1120 return got_error_from_errno("calloc");
1122 outfd = got_opentempfd();
1124 return got_error_from_errno("got_opentempfd");
1126 (*blob)->read_buf = malloc(blocksize);
1127 if ((*blob)->read_buf == NULL) {
1128 err = got_error_from_errno("malloc");
1132 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1134 struct got_pack *pack = NULL;
1136 err = get_packfile_path(&path_packfile, packidx);
1140 pack = got_repo_get_cached_pack(repo, path_packfile);
1142 err = got_repo_cache_pack(&pack, repo, path_packfile,
1147 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1148 pack, packidx, idx, id);
1149 } else if (err->code == GOT_ERR_NO_OBJ) {
1152 err = open_loose_object(&infd, id, repo);
1155 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1161 if (hdrlen > size) {
1162 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1167 if (close(outfd) != 0 && err == NULL)
1168 err = got_error_from_errno("close");
1170 (*blob)->f = fmemopen(outbuf, size, "rb");
1171 if ((*blob)->f == NULL) {
1172 err = got_error_from_errno("fmemopen");
1176 (*blob)->data = outbuf;
1178 if (fstat(outfd, &sb) == -1) {
1179 err = got_error_from_errno("fstat");
1183 if (sb.st_size != size) {
1184 err = got_error(GOT_ERR_PRIVSEP_LEN);
1188 (*blob)->f = fdopen(outfd, "rb");
1189 if ((*blob)->f == NULL) {
1190 err = got_error_from_errno("fdopen");
1197 (*blob)->hdrlen = hdrlen;
1198 (*blob)->blocksize = blocksize;
1199 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1202 free(path_packfile);
1205 got_object_blob_close(*blob);
1207 } else if (outfd != -1)
1213 const struct got_error *
1214 got_object_open_as_blob(struct got_blob_object **blob,
1215 struct got_repository *repo, struct got_object_id *id,
1218 return open_blob(blob, repo, id, blocksize);
1221 const struct got_error *
1222 got_object_blob_open(struct got_blob_object **blob,
1223 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1225 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1228 const struct got_error *
1229 got_object_blob_close(struct got_blob_object *blob)
1231 const struct got_error *err = NULL;
1232 free(blob->read_buf);
1233 if (blob->f && fclose(blob->f) != 0)
1234 err = got_error_from_errno("fclose");
1241 got_object_blob_rewind(struct got_blob_object *blob)
1248 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1250 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1254 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1256 return blob->hdrlen;
1260 got_object_blob_get_read_buf(struct got_blob_object *blob)
1262 return blob->read_buf;
1265 const struct got_error *
1266 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1270 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1271 if (n == 0 && ferror(blob->f))
1272 return got_ferror(blob->f, GOT_ERR_IO);
1277 const struct got_error *
1278 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1279 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1281 const struct got_error *err = NULL;
1282 size_t n, len, hdrlen;
1285 size_t noffsets = 0;
1286 off_t off = 0, total_len = 0;
1289 *line_offsets = NULL;
1295 hdrlen = got_object_blob_get_hdrlen(blob);
1297 err = got_object_blob_read_block(&len, blob);
1302 buf = got_object_blob_get_read_buf(blob);
1304 if (line_offsets && nlines) {
1305 if (*line_offsets == NULL) {
1306 /* Have some data but perhaps no '\n'. */
1309 *line_offsets = calloc(1, sizeof(**line_offsets));
1310 if (*line_offsets == NULL)
1311 return got_error_from_errno("calloc");
1313 /* Skip forward over end of first line. */
1320 /* Scan '\n' offsets in remaining chunk of data. */
1322 if (buf[i] != '\n') {
1327 if (noffsets < *nlines) {
1328 off_t *o = recallocarray(*line_offsets,
1330 sizeof(**line_offsets));
1332 free(*line_offsets);
1333 *line_offsets = NULL;
1334 return got_error_from_errno(
1340 off = total_len + i - hdrlen + 1;
1341 (*line_offsets)[*nlines - 1] = off;
1345 /* Skip blob object header first time around. */
1346 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1347 if (n != len - hdrlen)
1348 return got_ferror(outfile, GOT_ERR_IO);
1349 total_len += len - hdrlen;
1353 if (fflush(outfile) != 0)
1354 return got_error_from_errno("fflush");
1358 *filesize = total_len;
1363 static const struct got_error *
1364 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1365 int pack_idx, struct got_object_id *id)
1367 const struct got_error *err = NULL;
1369 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1374 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1377 static const struct got_error *
1378 read_packed_tag_privsep(struct got_tag_object **tag,
1379 struct got_pack *pack, struct got_packidx *packidx, int idx,
1380 struct got_object_id *id)
1382 const struct got_error *err = NULL;
1384 if (pack->privsep_child)
1385 return request_packed_tag(tag, pack, idx, id);
1387 err = start_pack_privsep_child(pack, packidx);
1391 return request_packed_tag(tag, pack, idx, id);
1394 static const struct got_error *
1395 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1398 const struct got_error *err = NULL;
1399 struct imsgbuf *ibuf;
1401 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1403 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1407 return got_privsep_recv_tag(tag, ibuf);
1410 static const struct got_error *
1411 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1412 struct got_repository *repo)
1414 const struct got_error *err;
1417 struct imsgbuf *ibuf;
1419 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1420 return request_tag(tag, repo, obj_fd);
1422 ibuf = calloc(1, sizeof(*ibuf));
1424 return got_error_from_errno("calloc");
1426 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1427 err = got_error_from_errno("socketpair");
1434 err = got_error_from_errno("fork");
1438 else if (pid == 0) {
1439 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1444 if (close(imsg_fds[1]) != 0) {
1445 err = got_error_from_errno("close");
1449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1452 imsg_init(ibuf, imsg_fds[0]);
1453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1455 return request_tag(tag, repo, obj_fd);
1458 static const struct got_error *
1459 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1460 struct got_object_id *id, int check_cache)
1462 const struct got_error *err = NULL;
1463 struct got_packidx *packidx = NULL;
1465 char *path_packfile = NULL;
1466 struct got_object *obj = NULL;
1467 int obj_type = GOT_OBJ_TYPE_ANY;
1470 *tag = got_repo_get_cached_tag(repo, id);
1478 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1480 struct got_pack *pack = NULL;
1482 err = get_packfile_path(&path_packfile, packidx);
1486 pack = got_repo_get_cached_pack(repo, path_packfile);
1488 err = got_repo_cache_pack(&pack, repo, path_packfile,
1494 /* Beware of "lightweight" tags: Check object type first. */
1495 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1499 obj_type = obj->type;
1500 got_object_close(obj);
1501 if (obj_type != GOT_OBJ_TYPE_TAG) {
1502 err = got_error(GOT_ERR_OBJ_TYPE);
1505 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1506 } else if (err->code == GOT_ERR_NO_OBJ) {
1509 err = open_loose_object(&fd, id, repo);
1512 err = read_object_header_privsep(&obj, repo, fd);
1515 obj_type = obj->type;
1516 got_object_close(obj);
1517 if (obj_type != GOT_OBJ_TYPE_TAG)
1518 return got_error(GOT_ERR_OBJ_TYPE);
1520 err = open_loose_object(&fd, id, repo);
1523 err = read_tag_privsep(tag, fd, repo);
1528 err = got_repo_cache_tag(repo, id, *tag);
1531 free(path_packfile);
1535 const struct got_error *
1536 got_object_open_as_tag(struct got_tag_object **tag,
1537 struct got_repository *repo, struct got_object_id *id)
1539 *tag = got_repo_get_cached_tag(repo, id);
1545 return open_tag(tag, repo, id, 0);
1548 const struct got_error *
1549 got_object_tag_open(struct got_tag_object **tag,
1550 struct got_repository *repo, struct got_object *obj)
1552 return open_tag(tag, repo, got_object_get_id(obj), 1);
1556 got_object_tag_get_name(struct got_tag_object *tag)
1562 got_object_tag_get_object_type(struct got_tag_object *tag)
1564 return tag->obj_type;
1567 struct got_object_id *
1568 got_object_tag_get_object_id(struct got_tag_object *tag)
1574 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1576 return tag->tagger_time;
1580 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1582 return tag->tagger_gmtoff;
1586 got_object_tag_get_tagger(struct got_tag_object *tag)
1592 got_object_tag_get_message(struct got_tag_object *tag)
1597 static struct got_tree_entry *
1598 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1602 /* Note that tree entries are sorted in strncmp() order. */
1603 for (i = 0; i < tree->nentries; i++) {
1604 struct got_tree_entry *te = &tree->entries[i];
1605 int cmp = strncmp(te->name, name, len);
1610 if (te->name[len] == '\0')
1616 struct got_tree_entry *
1617 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1619 return find_entry_by_name(tree, name, strlen(name));
1622 const struct got_error *
1623 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1624 struct got_object_id *commit_id, const char *path)
1626 const struct got_error *err = NULL;
1627 struct got_commit_object *commit = NULL;
1628 struct got_tree_object *tree = NULL;
1629 struct got_tree_entry *te = NULL;
1630 const char *seg, *s;
1635 err = got_object_open_as_commit(&commit, repo, commit_id);
1639 /* Handle opening of root of commit's tree. */
1640 if (got_path_is_root_dir(path)) {
1641 *id = got_object_id_dup(commit->tree_id);
1643 err = got_error_from_errno("got_object_id_dup");
1647 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1657 struct got_tree_object *next_tree;
1666 te = find_entry_by_name(tree, seg, seglen);
1668 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1679 err = got_object_open_as_tree(&next_tree, repo,
1684 got_object_tree_close(tree);
1690 *id = got_object_id_dup(&te->id);
1692 return got_error_from_errno("got_object_id_dup");
1694 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1697 got_object_commit_close(commit);
1699 got_object_tree_close(tree);
1704 * Normalize file mode bits to avoid false positive tree entry differences
1705 * in case tree entries have unexpected mode bits set.
1708 normalize_mode_for_comparison(mode_t mode)
1711 * For directories, the only relevant bit is the IFDIR bit.
1712 * This allows us to detect paths changing from a directory
1713 * to a file and vice versa.
1716 return mode & S_IFDIR;
1719 * For symlinks, the only relevant bit is the IFLNK bit.
1720 * This allows us to detect paths changing from a symlinks
1721 * to a file or directory and vice versa.
1724 return mode & S_IFLNK;
1726 /* For files, the only change we care about is the executable bit. */
1727 return mode & S_IXUSR;
1730 const struct got_error *
1731 got_object_tree_path_changed(int *changed,
1732 struct got_tree_object *tree01, struct got_tree_object *tree02,
1733 const char *path, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1737 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1738 const char *seg, *s;
1743 /* We not do support comparing the root path. */
1744 if (got_path_is_root_dir(path))
1745 return got_error_path(path, GOT_ERR_BAD_PATH);
1755 struct got_tree_object *next_tree1, *next_tree2;
1756 mode_t mode1, mode2;
1765 te1 = find_entry_by_name(tree1, seg, seglen);
1767 err = got_error(GOT_ERR_NO_OBJ);
1771 te2 = find_entry_by_name(tree2, seg, seglen);
1777 mode1 = normalize_mode_for_comparison(te1->mode);
1778 mode2 = normalize_mode_for_comparison(te2->mode);
1779 if (mode1 != mode2) {
1784 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1789 if (*s == '\0') { /* final path element */
1798 err = got_object_open_as_tree(&next_tree1, repo,
1803 if (tree1 != tree01)
1804 got_object_tree_close(tree1);
1807 err = got_object_open_as_tree(&next_tree2, repo,
1812 if (tree2 != tree02)
1813 got_object_tree_close(tree2);
1818 if (tree1 && tree1 != tree01)
1819 got_object_tree_close(tree1);
1820 if (tree2 && tree2 != tree02)
1821 got_object_tree_close(tree2);
1825 const struct got_error *
1826 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1827 struct got_tree_entry *te)
1829 const struct got_error *err = NULL;
1831 *new_te = calloc(1, sizeof(**new_te));
1832 if (*new_te == NULL)
1833 return got_error_from_errno("calloc");
1835 (*new_te)->mode = te->mode;
1836 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1837 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1842 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1844 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1848 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
1850 /* S_IFDIR check avoids confusing symlinks with submodules. */
1851 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
1854 static const struct got_error *
1855 resolve_symlink(char **link_target, const char *path,
1856 struct got_object_id *commit_id, struct got_repository *repo)
1858 const struct got_error *err = NULL;
1859 char *name, *parent_path = NULL;
1860 struct got_object_id *tree_obj_id = NULL;
1861 struct got_tree_object *tree = NULL;
1862 struct got_tree_entry *te = NULL;
1864 *link_target = NULL;
1866 name = basename(path);
1868 return got_error_from_errno2("basename", path);
1870 err = got_path_dirname(&parent_path, path);
1874 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
1877 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
1878 /* Display the complete path in error message. */
1879 err = got_error_path(path, err->code);
1884 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
1888 te = got_object_tree_find_entry(tree, name);
1890 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1894 if (got_object_tree_entry_is_symlink(te)) {
1895 err = got_tree_entry_get_symlink_target(link_target, te, repo);
1898 if (!got_path_is_absolute(*link_target)) {
1900 if (asprintf(&abspath, "%s/%s", parent_path,
1901 *link_target) == -1) {
1902 err = got_error_from_errno("asprintf");
1906 *link_target = malloc(PATH_MAX);
1907 if (*link_target == NULL) {
1908 err = got_error_from_errno("malloc");
1911 err = got_canonpath(abspath, *link_target, PATH_MAX);
1920 got_object_tree_close(tree);
1923 *link_target = NULL;
1928 const struct got_error *
1929 got_object_resolve_symlinks(char **link_target, const char *path,
1930 struct got_object_id *commit_id, struct got_repository *repo)
1932 const struct got_error *err = NULL;
1933 char *next_target = NULL;
1934 int max_recursion = 40; /* matches Git */
1936 *link_target = NULL;
1939 err = resolve_symlink(&next_target,
1940 *link_target ? *link_target : path, commit_id, repo);
1945 if (--max_recursion == 0) {
1946 err = got_error_path(path, GOT_ERR_RECURSION);
1947 *link_target = NULL;
1950 *link_target = next_target;
1952 } while (next_target);
1957 const struct got_error *
1958 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1959 struct got_object_id *commit_id, const char *path,
1960 struct got_repository *repo)
1962 const struct got_error *err = NULL;
1963 struct got_pack *pack = NULL;
1964 struct got_packidx *packidx = NULL;
1965 char *path_packfile = NULL;
1966 struct got_commit_object *changed_commit = NULL;
1967 struct got_object_id *changed_commit_id = NULL;
1970 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1972 if (err->code != GOT_ERR_NO_OBJ)
1977 err = get_packfile_path(&path_packfile, packidx);
1981 pack = got_repo_get_cached_pack(repo, path_packfile);
1983 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1988 if (pack->privsep_child == NULL) {
1989 err = start_pack_privsep_child(pack, packidx);
1994 err = got_privsep_send_commit_traversal_request(
1995 pack->privsep_child->ibuf, commit_id, idx, path);
1999 err = got_privsep_recv_traversed_commits(&changed_commit,
2000 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2004 if (changed_commit) {
2006 * Cache the commit in which the path was changed.
2007 * This commit might be opened again soon.
2009 changed_commit->refcnt++;
2010 err = got_repo_cache_commit(repo, changed_commit_id,
2012 got_object_commit_close(changed_commit);
2015 free(path_packfile);
2016 free(changed_commit_id);