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>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 struct got_object_id *
61 got_object_get_id(struct got_object *obj)
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 return got_object_id_str(outbuf, &obj->id);
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
76 const struct got_error *err = NULL;
77 struct got_object *obj;
79 err = got_object_open(&obj, repo, id);
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
91 err = got_error(GOT_ERR_OBJ_TYPE);
95 got_object_close(obj);
99 const struct got_error *
100 got_object_get_path(char **path, struct got_object_id *id,
101 struct got_repository *repo)
103 const struct got_error *err = NULL;
109 path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno("got_repo_get_path_objects");
113 err = got_object_id_str(&hex, id);
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
127 static const struct got_error *
128 open_loose_object(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
134 err = got_object_get_path(&path, id, repo);
137 *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_from_errno2("open", path);
147 static const struct got_error *
148 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
152 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
153 size = strlen(packidx->path_packidx) + 2;
154 if (size < GOT_PACKFILE_NAMELEN + 1)
155 return got_error(GOT_ERR_BAD_PATH);
157 *path_packfile = malloc(size);
158 if (*path_packfile == NULL)
159 return got_error_from_errno("malloc");
161 /* Copy up to and excluding ".idx". */
162 if (strlcpy(*path_packfile, packidx->path_packidx,
163 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
164 return got_error(GOT_ERR_NO_SPACE);
166 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
167 return got_error(GOT_ERR_NO_SPACE);
172 static const struct got_error *
173 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
174 struct got_object_id *id)
176 const struct got_error *err = NULL;
177 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
179 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
183 err = got_privsep_recv_obj(obj, ibuf);
187 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
193 set_max_datasize(void)
197 if (getrlimit(RLIMIT_DATA, &rl) != 0)
200 rl.rlim_cur = rl.rlim_max;
201 setrlimit(RLIMIT_DATA, &rl);
204 static const struct got_error *
205 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
207 const struct got_error *err = NULL;
210 struct imsgbuf *ibuf;
212 ibuf = calloc(1, sizeof(*ibuf));
214 return got_error_from_errno("calloc");
216 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
217 if (pack->privsep_child == NULL) {
218 err = got_error_from_errno("calloc");
223 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
224 err = got_error_from_errno("socketpair");
230 err = got_error_from_errno("fork");
232 } else if (pid == 0) {
234 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
235 pack->path_packfile);
239 if (close(imsg_fds[1]) != 0)
240 return got_error_from_errno("close");
241 pack->privsep_child->imsg_fd = imsg_fds[0];
242 pack->privsep_child->pid = pid;
243 imsg_init(ibuf, imsg_fds[0]);
244 pack->privsep_child->ibuf = ibuf;
246 err = got_privsep_init_pack_child(ibuf, pack, packidx);
248 const struct got_error *child_err;
249 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
250 child_err = got_privsep_wait_for_child(
251 pack->privsep_child->pid);
252 if (child_err && err == NULL)
258 free(pack->privsep_child);
259 pack->privsep_child = NULL;
264 static const struct got_error *
265 read_packed_object_privsep(struct got_object **obj,
266 struct got_repository *repo, struct got_pack *pack,
267 struct got_packidx *packidx, int idx, struct got_object_id *id)
269 const struct got_error *err = NULL;
271 if (pack->privsep_child)
272 return request_packed_object(obj, pack, idx, id);
274 err = start_pack_privsep_child(pack, packidx);
278 return request_packed_object(obj, pack, idx, id);
282 static const struct got_error *
283 open_packed_object(struct got_object **obj, struct got_object_id *id,
284 struct got_repository *repo)
286 const struct got_error *err = NULL;
287 struct got_pack *pack = NULL;
288 struct got_packidx *packidx = NULL;
292 err = got_repo_search_packidx(&packidx, &idx, repo, id);
296 err = get_packfile_path(&path_packfile, packidx);
300 pack = got_repo_get_cached_pack(repo, path_packfile);
302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
307 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
311 err = got_repo_cache_pack(NULL, repo, path_packfile, packidx);
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)
868 got_tree_entry_get_index(struct got_tree_entry *te)
873 struct got_tree_entry *
874 got_tree_entry_get_next(struct got_tree_object *tree,
875 struct got_tree_entry *te)
877 return got_object_tree_get_entry(tree, te->idx + 1);
880 struct got_tree_entry *
881 got_tree_entry_get_prev(struct got_tree_object *tree,
882 struct got_tree_entry *te)
884 return got_object_tree_get_entry(tree, te->idx - 1);
887 static const struct got_error *
888 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
889 struct got_pack *pack, struct got_packidx *packidx, int idx,
890 struct got_object_id *id)
892 const struct got_error *err = NULL;
894 int basefd, accumfd; /* temporary files for delta application */
896 basefd = got_opentempfd();
898 return got_error_from_errno("got_opentempfd");
899 accumfd = got_opentempfd();
901 return got_error_from_errno("got_opentempfd");
903 outfd_child = dup(outfd);
904 if (outfd_child == -1)
905 return got_error_from_errno("dup");
907 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
911 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
919 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
926 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
931 err = got_privsep_recv_blob(outbuf, size, hdrlen,
932 pack->privsep_child->ibuf);
936 if (lseek(outfd, SEEK_SET, 0) == -1)
937 err = got_error_from_errno("lseek");
942 static const struct got_error *
943 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
944 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
945 struct got_object_id *id)
947 const struct got_error *err = NULL;
949 if (pack->privsep_child == NULL) {
950 err = start_pack_privsep_child(pack, packidx);
955 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
959 static const struct got_error *
960 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
961 int infd, struct imsgbuf *ibuf)
963 const struct got_error *err = NULL;
966 outfd_child = dup(outfd);
967 if (outfd_child == -1)
968 return got_error_from_errno("dup");
970 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
974 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
978 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
982 if (lseek(outfd, SEEK_SET, 0) == -1)
983 return got_error_from_errno("lseek");
988 static const struct got_error *
989 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
990 int outfd, int infd, struct got_repository *repo)
992 const struct got_error *err;
995 struct imsgbuf *ibuf;
997 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
998 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
999 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1002 ibuf = calloc(1, sizeof(*ibuf));
1004 return got_error_from_errno("calloc");
1006 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1007 err = got_error_from_errno("socketpair");
1014 err = got_error_from_errno("fork");
1018 else if (pid == 0) {
1019 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1024 if (close(imsg_fds[1]) != 0) {
1025 err = got_error_from_errno("close");
1029 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1031 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1032 imsg_init(ibuf, imsg_fds[0]);
1033 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1035 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1038 static const struct got_error *
1039 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1040 struct got_object_id *id, size_t blocksize)
1042 const struct got_error *err = NULL;
1043 struct got_packidx *packidx = NULL;
1045 char *path_packfile = NULL;
1048 size_t size, hdrlen;
1051 *blob = calloc(1, sizeof(**blob));
1053 return got_error_from_errno("calloc");
1055 outfd = got_opentempfd();
1057 return got_error_from_errno("got_opentempfd");
1059 (*blob)->read_buf = malloc(blocksize);
1060 if ((*blob)->read_buf == NULL) {
1061 err = got_error_from_errno("malloc");
1065 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1067 struct got_pack *pack = NULL;
1069 err = get_packfile_path(&path_packfile, packidx);
1073 pack = got_repo_get_cached_pack(repo, path_packfile);
1075 err = got_repo_cache_pack(&pack, repo, path_packfile,
1080 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1081 pack, packidx, idx, id);
1082 } else if (err->code == GOT_ERR_NO_OBJ) {
1085 err = open_loose_object(&infd, id, repo);
1088 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1094 if (hdrlen > size) {
1095 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1100 if (close(outfd) != 0 && err == NULL)
1101 err = got_error_from_errno("close");
1103 (*blob)->f = fmemopen(outbuf, size, "rb");
1104 if ((*blob)->f == NULL) {
1105 err = got_error_from_errno("fmemopen");
1109 (*blob)->data = outbuf;
1111 if (fstat(outfd, &sb) == -1) {
1112 err = got_error_from_errno("fstat");
1116 if (sb.st_size != size) {
1117 err = got_error(GOT_ERR_PRIVSEP_LEN);
1121 (*blob)->f = fdopen(outfd, "rb");
1122 if ((*blob)->f == NULL) {
1123 err = got_error_from_errno("fdopen");
1130 (*blob)->hdrlen = hdrlen;
1131 (*blob)->blocksize = blocksize;
1132 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1135 free(path_packfile);
1138 got_object_blob_close(*blob);
1140 } else if (outfd != -1)
1146 const struct got_error *
1147 got_object_open_as_blob(struct got_blob_object **blob,
1148 struct got_repository *repo, struct got_object_id *id,
1151 return open_blob(blob, repo, id, blocksize);
1154 const struct got_error *
1155 got_object_blob_open(struct got_blob_object **blob,
1156 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1158 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1161 const struct got_error *
1162 got_object_blob_close(struct got_blob_object *blob)
1164 const struct got_error *err = NULL;
1165 free(blob->read_buf);
1166 if (blob->f && fclose(blob->f) != 0)
1167 err = got_error_from_errno("fclose");
1174 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1176 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1180 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1182 return blob->hdrlen;
1186 got_object_blob_get_read_buf(struct got_blob_object *blob)
1188 return blob->read_buf;
1191 const struct got_error *
1192 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1196 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1197 if (n == 0 && ferror(blob->f))
1198 return got_ferror(blob->f, GOT_ERR_IO);
1203 const struct got_error *
1204 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1205 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1207 const struct got_error *err = NULL;
1208 size_t n, len, hdrlen;
1211 size_t noffsets = 0;
1212 off_t off = 0, total_len = 0;
1215 *line_offsets = NULL;
1221 hdrlen = got_object_blob_get_hdrlen(blob);
1223 err = got_object_blob_read_block(&len, blob);
1228 buf = got_object_blob_get_read_buf(blob);
1230 if (line_offsets && nlines) {
1231 if (*line_offsets == NULL) {
1232 /* Have some data but perhaps no '\n'. */
1235 *line_offsets = calloc(1, sizeof(**line_offsets));
1236 if (*line_offsets == NULL)
1237 return got_error_from_errno("malloc");
1239 /* Skip forward over end of first line. */
1246 /* Scan '\n' offsets in remaining chunk of data. */
1248 if (buf[i] != '\n') {
1253 if (noffsets < *nlines) {
1254 off_t *o = recallocarray(*line_offsets,
1256 sizeof(**line_offsets));
1258 free(*line_offsets);
1259 *line_offsets = NULL;
1260 return got_error_from_errno(
1266 off = total_len + i - hdrlen + 1;
1267 (*line_offsets)[*nlines - 1] = off;
1271 /* Skip blob object header first time around. */
1272 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1273 if (n != len - hdrlen)
1274 return got_ferror(outfile, GOT_ERR_IO);
1275 total_len += len - hdrlen;
1279 if (fflush(outfile) != 0)
1280 return got_error_from_errno("fflush");
1284 *filesize = total_len;
1289 static const struct got_error *
1290 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1291 int pack_idx, struct got_object_id *id)
1293 const struct got_error *err = NULL;
1295 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1300 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1303 static const struct got_error *
1304 read_packed_tag_privsep(struct got_tag_object **tag,
1305 struct got_pack *pack, struct got_packidx *packidx, int idx,
1306 struct got_object_id *id)
1308 const struct got_error *err = NULL;
1310 if (pack->privsep_child)
1311 return request_packed_tag(tag, pack, idx, id);
1313 err = start_pack_privsep_child(pack, packidx);
1317 return request_packed_tag(tag, pack, idx, id);
1320 static const struct got_error *
1321 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1324 const struct got_error *err = NULL;
1325 struct imsgbuf *ibuf;
1327 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1329 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1333 return got_privsep_recv_tag(tag, ibuf);
1336 static const struct got_error *
1337 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1338 struct got_repository *repo)
1340 const struct got_error *err;
1343 struct imsgbuf *ibuf;
1345 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1346 return request_tag(tag, repo, obj_fd);
1348 ibuf = calloc(1, sizeof(*ibuf));
1350 return got_error_from_errno("calloc");
1352 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1353 err = got_error_from_errno("socketpair");
1360 err = got_error_from_errno("fork");
1364 else if (pid == 0) {
1365 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1370 if (close(imsg_fds[1]) != 0) {
1371 err = got_error_from_errno("close");
1375 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1377 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1378 imsg_init(ibuf, imsg_fds[0]);
1379 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1381 return request_tag(tag, repo, obj_fd);
1384 static const struct got_error *
1385 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1386 struct got_object_id *id, int check_cache)
1388 const struct got_error *err = NULL;
1389 struct got_packidx *packidx = NULL;
1391 char *path_packfile = NULL;
1392 struct got_object *obj = NULL;
1393 int obj_type = GOT_OBJ_TYPE_ANY;
1396 *tag = got_repo_get_cached_tag(repo, id);
1404 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1406 struct got_pack *pack = NULL;
1408 err = get_packfile_path(&path_packfile, packidx);
1412 pack = got_repo_get_cached_pack(repo, path_packfile);
1414 err = got_repo_cache_pack(&pack, repo, path_packfile,
1420 /* Beware of "leightweight" tags: Check object type first. */
1421 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1425 obj_type = obj->type;
1426 got_object_close(obj);
1427 if (obj_type != GOT_OBJ_TYPE_TAG) {
1428 err = got_error(GOT_ERR_OBJ_TYPE);
1431 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1432 } else if (err->code == GOT_ERR_NO_OBJ) {
1435 err = open_loose_object(&fd, id, repo);
1438 err = read_object_header_privsep(&obj, repo, fd);
1441 obj_type = obj->type;
1442 got_object_close(obj);
1443 if (obj_type != GOT_OBJ_TYPE_TAG)
1444 return got_error(GOT_ERR_OBJ_TYPE);
1446 err = open_loose_object(&fd, id, repo);
1449 err = read_tag_privsep(tag, fd, repo);
1454 err = got_repo_cache_tag(repo, id, *tag);
1457 free(path_packfile);
1461 const struct got_error *
1462 got_object_open_as_tag(struct got_tag_object **tag,
1463 struct got_repository *repo, struct got_object_id *id)
1465 *tag = got_repo_get_cached_tag(repo, id);
1471 return open_tag(tag, repo, id, 0);
1474 const struct got_error *
1475 got_object_tag_open(struct got_tag_object **tag,
1476 struct got_repository *repo, struct got_object *obj)
1478 return open_tag(tag, repo, got_object_get_id(obj), 1);
1482 got_object_tag_get_name(struct got_tag_object *tag)
1488 got_object_tag_get_object_type(struct got_tag_object *tag)
1490 return tag->obj_type;
1493 struct got_object_id *
1494 got_object_tag_get_object_id(struct got_tag_object *tag)
1500 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1502 return tag->tagger_time;
1506 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1508 return tag->tagger_gmtoff;
1512 got_object_tag_get_tagger(struct got_tag_object *tag)
1518 got_object_tag_get_message(struct got_tag_object *tag)
1523 static struct got_tree_entry *
1524 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1528 /* Note that tree entries are sorted in strncmp() order. */
1529 for (i = 0; i < tree->nentries; i++) {
1530 struct got_tree_entry *te = &tree->entries[i];
1531 int cmp = strncmp(te->name, name, len);
1536 if (te->name[len] == '\0')
1542 struct got_tree_entry *
1543 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1545 return find_entry_by_name(tree, name, strlen(name));
1548 const struct got_error *
1549 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1550 struct got_object_id *commit_id, const char *path)
1552 const struct got_error *err = NULL;
1553 struct got_commit_object *commit = NULL;
1554 struct got_tree_object *tree = NULL;
1555 struct got_tree_entry *te = NULL;
1556 const char *seg, *s;
1561 err = got_object_open_as_commit(&commit, repo, commit_id);
1565 /* Handle opening of root of commit's tree. */
1566 if (got_path_is_root_dir(path)) {
1567 *id = got_object_id_dup(commit->tree_id);
1569 err = got_error_from_errno("got_object_id_dup");
1573 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1583 struct got_tree_object *next_tree;
1592 te = find_entry_by_name(tree, seg, seglen);
1594 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1605 err = got_object_open_as_tree(&next_tree, repo,
1610 got_object_tree_close(tree);
1616 *id = got_object_id_dup(&te->id);
1618 return got_error_from_errno("got_object_id_dup");
1620 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1623 got_object_commit_close(commit);
1625 got_object_tree_close(tree);
1629 const struct got_error *
1630 got_object_tree_path_changed(int *changed,
1631 struct got_tree_object *tree01, struct got_tree_object *tree02,
1632 const char *path, struct got_repository *repo)
1634 const struct got_error *err = NULL;
1635 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1636 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1637 const char *seg, *s;
1642 /* We are expecting an absolute in-repository path. */
1644 return got_error(GOT_ERR_NOT_ABSPATH);
1646 /* We not do support comparing the root path. */
1647 if (path[1] == '\0')
1648 return got_error(GOT_ERR_BAD_PATH);
1653 s++; /* skip leading '/' */
1657 struct got_tree_object *next_tree1, *next_tree2;
1666 te1 = find_entry_by_name(tree1, seg, seglen);
1668 err = got_error(GOT_ERR_NO_OBJ);
1672 te2 = find_entry_by_name(tree2, seg, seglen);
1678 if (te1->mode != te2->mode) {
1683 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1688 if (*s == '\0') { /* final path element */
1697 err = got_object_open_as_tree(&next_tree1, repo,
1702 if (tree1 != tree01)
1703 got_object_tree_close(tree1);
1706 err = got_object_open_as_tree(&next_tree2, repo,
1711 if (tree2 != tree02)
1712 got_object_tree_close(tree2);
1717 if (tree1 && tree1 != tree01)
1718 got_object_tree_close(tree1);
1719 if (tree2 && tree2 != tree02)
1720 got_object_tree_close(tree2);
1724 const struct got_error *
1725 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1726 struct got_tree_entry *te)
1728 const struct got_error *err = NULL;
1730 *new_te = calloc(1, sizeof(**new_te));
1731 if (*new_te == NULL)
1732 return got_error_from_errno("calloc");
1734 (*new_te)->mode = te->mode;
1735 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1736 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1741 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1743 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1746 const struct got_error *
1747 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1748 struct got_object_id *commit_id, const char *path,
1749 struct got_repository *repo)
1751 const struct got_error *err = NULL;
1752 struct got_pack *pack = NULL;
1753 struct got_packidx *packidx = NULL;
1754 char *path_packfile = NULL;
1755 struct got_commit_object *changed_commit = NULL;
1756 struct got_object_id *changed_commit_id = NULL;
1759 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1761 if (err->code != GOT_ERR_NO_OBJ)
1766 err = get_packfile_path(&path_packfile, packidx);
1770 pack = got_repo_get_cached_pack(repo, path_packfile);
1772 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1777 if (pack->privsep_child == NULL) {
1778 err = start_pack_privsep_child(pack, packidx);
1783 err = got_privsep_send_commit_traversal_request(
1784 pack->privsep_child->ibuf, commit_id, idx, path);
1788 err = got_privsep_recv_traversed_commits(&changed_commit,
1789 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1793 if (changed_commit) {
1795 * Cache the commit in which the path was changed.
1796 * This commit might be opened again soon.
1798 changed_commit->refcnt++;
1799 err = got_repo_cache_commit(repo, changed_commit_id,
1801 got_object_commit_close(changed_commit);
1804 free(path_packfile);
1805 free(changed_commit_id);