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_id_dup(struct got_object_id *id1)
63 struct got_object_id *id2;
65 id2 = malloc(sizeof(*id2));
68 memcpy(id2, id1, sizeof(*id2));
72 struct got_object_id *
73 got_object_get_id(struct got_object *obj)
78 const struct got_error *
79 got_object_get_id_str(char **outbuf, struct got_object *obj)
81 return got_object_id_str(outbuf, &obj->id);
84 const struct got_error *
85 got_object_get_type(int *type, struct got_repository *repo,
86 struct got_object_id *id)
88 const struct got_error *err = NULL;
89 struct got_object *obj;
91 err = got_object_open(&obj, repo, id);
96 case GOT_OBJ_TYPE_COMMIT:
97 case GOT_OBJ_TYPE_TREE:
98 case GOT_OBJ_TYPE_BLOB:
99 case GOT_OBJ_TYPE_TAG:
103 err = got_error(GOT_ERR_OBJ_TYPE);
107 got_object_close(obj);
111 const struct got_error *
112 got_object_get_path(char **path, struct got_object_id *id,
113 struct got_repository *repo)
115 const struct got_error *err = NULL;
117 char *path_objects = got_repo_get_path_objects(repo);
121 if (path_objects == NULL)
122 return got_error_from_errno("got_repo_get_path_objects");
124 err = got_object_id_str(&hex, id);
128 if (asprintf(path, "%s/%.2x/%s", path_objects,
129 id->sha1[0], hex + 2) == -1)
130 err = got_error_from_errno("asprintf");
138 static const struct got_error *
139 open_loose_object(int *fd, struct got_object_id *id,
140 struct got_repository *repo)
142 const struct got_error *err = NULL;
145 err = got_object_get_path(&path, id, repo);
148 *fd = open(path, O_RDONLY | O_NOFOLLOW);
150 err = got_error_from_errno2("open", path);
158 static const struct got_error *
159 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
163 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
164 size = strlen(packidx->path_packidx) + 2;
165 if (size < GOT_PACKFILE_NAMELEN + 1)
166 return got_error(GOT_ERR_BAD_PATH);
168 *path_packfile = malloc(size);
169 if (*path_packfile == NULL)
170 return got_error_from_errno("malloc");
172 /* Copy up to and excluding ".idx". */
173 if (strlcpy(*path_packfile, packidx->path_packidx,
174 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
175 return got_error(GOT_ERR_NO_SPACE);
177 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
178 return got_error(GOT_ERR_NO_SPACE);
184 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
186 if (close(imsg_fds[0]) != 0) {
187 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
191 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
195 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
196 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
200 if (execl(path, path, repo_path, (char *)NULL) == -1) {
201 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
207 static const struct got_error *
208 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
209 struct got_object_id *id)
211 const struct got_error *err = NULL;
212 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
214 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
218 err = got_privsep_recv_obj(obj, ibuf);
222 (*obj)->path_packfile = strdup(pack->path_packfile);
223 if ((*obj)->path_packfile == NULL) {
224 err = got_error_from_errno("strdup");
227 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
233 set_max_datasize(void)
237 if (getrlimit(RLIMIT_DATA, &rl) != 0)
240 rl.rlim_cur = rl.rlim_max;
241 setrlimit(RLIMIT_DATA, &rl);
244 static const struct got_error *
245 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
247 const struct got_error *err = NULL;
250 struct imsgbuf *ibuf;
252 ibuf = calloc(1, sizeof(*ibuf));
254 return got_error_from_errno("calloc");
256 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
257 if (pack->privsep_child == NULL) {
258 err = got_error_from_errno("calloc");
263 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
264 err = got_error_from_errno("socketpair");
270 err = got_error_from_errno("fork");
272 } else if (pid == 0) {
274 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
275 pack->path_packfile);
279 if (close(imsg_fds[1]) != 0)
280 return got_error_from_errno("close");
281 pack->privsep_child->imsg_fd = imsg_fds[0];
282 pack->privsep_child->pid = pid;
283 imsg_init(ibuf, imsg_fds[0]);
284 pack->privsep_child->ibuf = ibuf;
286 err = got_privsep_init_pack_child(ibuf, pack, packidx);
288 const struct got_error *child_err;
289 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
290 child_err = got_privsep_wait_for_child(
291 pack->privsep_child->pid);
292 if (child_err && err == NULL)
298 free(pack->privsep_child);
299 pack->privsep_child = NULL;
304 static const struct got_error *
305 read_packed_object_privsep(struct got_object **obj,
306 struct got_repository *repo, struct got_pack *pack,
307 struct got_packidx *packidx, int idx, struct got_object_id *id)
309 const struct got_error *err = NULL;
311 if (pack->privsep_child)
312 return request_packed_object(obj, pack, idx, id);
314 err = start_pack_privsep_child(pack, packidx);
318 return request_packed_object(obj, pack, idx, id);
322 static const struct got_error *
323 open_packed_object(struct got_object **obj, struct got_object_id *id,
324 struct got_repository *repo)
326 const struct got_error *err = NULL;
327 struct got_pack *pack = NULL;
328 struct got_packidx *packidx = NULL;
332 err = got_repo_search_packidx(&packidx, &idx, repo, id);
336 err = get_packfile_path(&path_packfile, packidx);
340 pack = got_repo_get_cached_pack(repo, path_packfile);
342 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
347 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
351 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
357 static const struct got_error *
358 request_object(struct got_object **obj, struct got_repository *repo, int fd)
360 const struct got_error *err = NULL;
361 struct imsgbuf *ibuf;
363 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
365 err = got_privsep_send_obj_req(ibuf, fd);
369 return got_privsep_recv_obj(obj, ibuf);
372 static const struct got_error *
373 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
378 struct imsgbuf *ibuf;
380 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
381 return request_object(obj, repo, obj_fd);
383 ibuf = calloc(1, sizeof(*ibuf));
385 return got_error_from_errno("calloc");
387 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
388 return got_error_from_errno("socketpair");
392 return got_error_from_errno("fork");
394 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
399 if (close(imsg_fds[1]) != 0)
400 return got_error_from_errno("close");
401 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
403 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
404 imsg_init(ibuf, imsg_fds[0]);
405 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
407 return request_object(obj, repo, obj_fd);
411 const struct got_error *
412 got_object_open(struct got_object **obj, struct got_repository *repo,
413 struct got_object_id *id)
415 const struct got_error *err = NULL;
419 *obj = got_repo_get_cached_object(repo, id);
425 err = open_packed_object(obj, id, repo);
426 if (err && err->code != GOT_ERR_NO_OBJ)
430 return got_repo_cache_object(repo, id, *obj);
433 err = got_object_get_path(&path, id, repo);
437 fd = open(path, O_RDONLY | O_NOFOLLOW);
440 err = got_error_no_obj(id);
442 err = got_error_from_errno2("open", path);
445 err = read_object_header_privsep(obj, repo, fd);
448 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
452 err = got_repo_cache_object(repo, id, *obj);
459 const struct got_error *
460 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
463 struct got_object_id id;
465 if (!got_parse_sha1_digest(id.sha1, id_str))
466 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
468 return got_object_open(obj, repo, &id);
471 const struct got_error *
472 got_object_resolve_id_str(struct got_object_id **id,
473 struct got_repository *repo, const char *id_str)
475 const struct got_error *err = NULL;
476 struct got_object *obj;
478 err = got_object_open_by_id_str(&obj, repo, id_str);
482 *id = got_object_id_dup(got_object_get_id(obj));
483 got_object_close(obj);
485 return got_error_from_errno("got_object_id_dup");
490 static const struct got_error *
491 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
492 int pack_idx, struct got_object_id *id)
494 const struct got_error *err = NULL;
496 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
501 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
504 static const struct got_error *
505 read_packed_commit_privsep(struct got_commit_object **commit,
506 struct got_pack *pack, struct got_packidx *packidx, int idx,
507 struct got_object_id *id)
509 const struct got_error *err = NULL;
511 if (pack->privsep_child)
512 return request_packed_commit(commit, pack, idx, id);
514 err = start_pack_privsep_child(pack, packidx);
518 return request_packed_commit(commit, pack, idx, id);
521 static const struct got_error *
522 request_commit(struct got_commit_object **commit, struct got_repository *repo,
525 const struct got_error *err = NULL;
526 struct imsgbuf *ibuf;
528 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
530 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
534 return got_privsep_recv_commit(commit, ibuf);
537 static const struct got_error *
538 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
539 struct got_repository *repo)
543 struct imsgbuf *ibuf;
545 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
546 return request_commit(commit, repo, obj_fd);
548 ibuf = calloc(1, sizeof(*ibuf));
550 return got_error_from_errno("calloc");
552 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
553 return got_error_from_errno("socketpair");
557 return got_error_from_errno("fork");
559 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
564 if (close(imsg_fds[1]) != 0)
565 return got_error_from_errno("close");
566 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
568 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
569 imsg_init(ibuf, imsg_fds[0]);
570 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
572 return request_commit(commit, repo, obj_fd);
576 static const struct got_error *
577 open_commit(struct got_commit_object **commit,
578 struct got_repository *repo, struct got_object_id *id, int check_cache)
580 const struct got_error *err = NULL;
581 struct got_packidx *packidx = NULL;
586 *commit = got_repo_get_cached_commit(repo, id);
587 if (*commit != NULL) {
594 err = got_repo_search_packidx(&packidx, &idx, repo, id);
596 struct got_pack *pack = NULL;
598 err = get_packfile_path(&path_packfile, packidx);
602 pack = got_repo_get_cached_pack(repo, path_packfile);
604 err = got_repo_cache_pack(&pack, repo, path_packfile,
609 err = read_packed_commit_privsep(commit, pack,
611 } else if (err->code == GOT_ERR_NO_OBJ) {
614 err = open_loose_object(&fd, id, repo);
617 err = read_commit_privsep(commit, fd, repo);
622 err = got_repo_cache_commit(repo, id, *commit);
628 const struct got_error *
629 got_object_open_as_commit(struct got_commit_object **commit,
630 struct got_repository *repo, struct got_object_id *id)
632 *commit = got_repo_get_cached_commit(repo, id);
633 if (*commit != NULL) {
638 return open_commit(commit, repo, id, 0);
641 const struct got_error *
642 got_object_commit_open(struct got_commit_object **commit,
643 struct got_repository *repo, struct got_object *obj)
645 return open_commit(commit, repo, got_object_get_id(obj), 1);
648 const struct got_error *
649 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
651 const struct got_error *err = NULL;
653 *qid = calloc(1, sizeof(**qid));
655 return got_error_from_errno("calloc");
657 (*qid)->id = got_object_id_dup(id);
658 if ((*qid)->id == NULL) {
659 err = got_error_from_errno("got_object_id_dup");
660 got_object_qid_free(*qid);
668 static const struct got_error *
669 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
670 int pack_idx, struct got_object_id *id)
672 const struct got_error *err = NULL;
674 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
679 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
682 static const struct got_error *
683 read_packed_tree_privsep(struct got_tree_object **tree,
684 struct got_pack *pack, struct got_packidx *packidx, int idx,
685 struct got_object_id *id)
687 const struct got_error *err = NULL;
689 if (pack->privsep_child)
690 return request_packed_tree(tree, pack, idx, id);
692 err = start_pack_privsep_child(pack, packidx);
696 return request_packed_tree(tree, pack, idx, id);
699 static const struct got_error *
700 request_tree(struct got_tree_object **tree, struct got_repository *repo,
703 const struct got_error *err = NULL;
704 struct imsgbuf *ibuf;
706 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
708 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
712 return got_privsep_recv_tree(tree, ibuf);
715 const struct got_error *
716 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
717 struct got_repository *repo)
721 struct imsgbuf *ibuf;
723 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
724 return request_tree(tree, repo, obj_fd);
726 ibuf = calloc(1, sizeof(*ibuf));
728 return got_error_from_errno("calloc");
730 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
731 return got_error_from_errno("socketpair");
735 return got_error_from_errno("fork");
737 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
742 if (close(imsg_fds[1]) != 0)
743 return got_error_from_errno("close");
744 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
746 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
747 imsg_init(ibuf, imsg_fds[0]);
748 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
751 return request_tree(tree, repo, obj_fd);
754 static const struct got_error *
755 open_tree(struct got_tree_object **tree, struct got_repository *repo,
756 struct got_object_id *id, int check_cache)
758 const struct got_error *err = NULL;
759 struct got_packidx *packidx = NULL;
764 *tree = got_repo_get_cached_tree(repo, id);
772 err = got_repo_search_packidx(&packidx, &idx, repo, id);
774 struct got_pack *pack = NULL;
776 err = get_packfile_path(&path_packfile, packidx);
780 pack = got_repo_get_cached_pack(repo, path_packfile);
782 err = got_repo_cache_pack(&pack, repo, path_packfile,
787 err = read_packed_tree_privsep(tree, pack,
789 } else if (err->code == GOT_ERR_NO_OBJ) {
792 err = open_loose_object(&fd, id, repo);
795 err = read_tree_privsep(tree, fd, repo);
800 err = got_repo_cache_tree(repo, id, *tree);
806 const struct got_error *
807 got_object_open_as_tree(struct got_tree_object **tree,
808 struct got_repository *repo, struct got_object_id *id)
810 *tree = got_repo_get_cached_tree(repo, id);
816 return open_tree(tree, repo, id, 0);
819 const struct got_error *
820 got_object_tree_open(struct got_tree_object **tree,
821 struct got_repository *repo, struct got_object *obj)
823 return open_tree(tree, repo, got_object_get_id(obj), 1);
826 const struct got_tree_entries *
827 got_object_tree_get_entries(struct got_tree_object *tree)
829 return &tree->entries;
832 static const struct got_error *
833 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
834 struct got_pack *pack, struct got_packidx *packidx, int idx,
835 struct got_object_id *id)
837 const struct got_error *err = NULL;
839 int basefd, accumfd; /* temporary files for delta application */
841 basefd = got_opentempfd();
843 return got_error_from_errno("got_opentempfd");
844 accumfd = got_opentempfd();
846 return got_error_from_errno("got_opentempfd");
848 outfd_child = dup(outfd);
849 if (outfd_child == -1)
850 return got_error_from_errno("dup");
852 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
856 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
864 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
871 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
876 err = got_privsep_recv_blob(outbuf, size, hdrlen,
877 pack->privsep_child->ibuf);
881 if (lseek(outfd, SEEK_SET, 0) == -1)
882 err = got_error_from_errno("lseek");
887 static const struct got_error *
888 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
889 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
890 struct got_object_id *id)
892 const struct got_error *err = NULL;
894 if (pack->privsep_child == NULL) {
895 err = start_pack_privsep_child(pack, packidx);
900 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
904 static const struct got_error *
905 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
906 int infd, struct imsgbuf *ibuf)
908 const struct got_error *err = NULL;
911 outfd_child = dup(outfd);
912 if (outfd_child == -1)
913 return got_error_from_errno("dup");
915 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
919 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
923 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
927 if (lseek(outfd, SEEK_SET, 0) == -1)
928 return got_error_from_errno("lseek");
933 static const struct got_error *
934 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
935 int outfd, int infd, struct got_repository *repo)
939 struct imsgbuf *ibuf;
941 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
942 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
943 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
946 ibuf = calloc(1, sizeof(*ibuf));
948 return got_error_from_errno("calloc");
950 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
951 return got_error_from_errno("socketpair");
955 return got_error_from_errno("fork");
957 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
962 if (close(imsg_fds[1]) != 0)
963 return got_error_from_errno("close");
964 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
966 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
967 imsg_init(ibuf, imsg_fds[0]);
968 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
970 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
973 static const struct got_error *
974 open_blob(struct got_blob_object **blob, struct got_repository *repo,
975 struct got_object_id *id, size_t blocksize)
977 const struct got_error *err = NULL;
978 struct got_packidx *packidx = NULL;
986 *blob = calloc(1, sizeof(**blob));
988 return got_error_from_errno("calloc");
990 outfd = got_opentempfd();
992 return got_error_from_errno("got_opentempfd");
994 (*blob)->read_buf = malloc(blocksize);
995 if ((*blob)->read_buf == NULL) {
996 err = got_error_from_errno("malloc");
1000 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1002 struct got_pack *pack = NULL;
1004 err = get_packfile_path(&path_packfile, packidx);
1008 pack = got_repo_get_cached_pack(repo, path_packfile);
1010 err = got_repo_cache_pack(&pack, repo, path_packfile,
1015 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1016 pack, packidx, idx, id);
1017 } else if (err->code == GOT_ERR_NO_OBJ) {
1020 err = open_loose_object(&infd, id, repo);
1023 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1029 if (hdrlen > size) {
1030 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1035 if (close(outfd) != 0 && err == NULL)
1036 err = got_error_from_errno("close");
1038 (*blob)->f = fmemopen(outbuf, size, "rb");
1039 if ((*blob)->f == NULL) {
1040 err = got_error_from_errno("fmemopen");
1044 (*blob)->data = outbuf;
1046 if (fstat(outfd, &sb) == -1) {
1047 err = got_error_from_errno("fstat");
1051 if (sb.st_size != size) {
1052 err = got_error(GOT_ERR_PRIVSEP_LEN);
1056 (*blob)->f = fdopen(outfd, "rb");
1057 if ((*blob)->f == NULL) {
1058 err = got_error_from_errno("fdopen");
1065 (*blob)->hdrlen = hdrlen;
1066 (*blob)->blocksize = blocksize;
1067 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1072 got_object_blob_close(*blob);
1074 } else if (outfd != -1)
1080 const struct got_error *
1081 got_object_open_as_blob(struct got_blob_object **blob,
1082 struct got_repository *repo, struct got_object_id *id,
1085 return open_blob(blob, repo, id, blocksize);
1088 const struct got_error *
1089 got_object_blob_open(struct got_blob_object **blob,
1090 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1092 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1095 const struct got_error *
1096 got_object_blob_close(struct got_blob_object *blob)
1098 const struct got_error *err = NULL;
1099 free(blob->read_buf);
1100 if (blob->f && fclose(blob->f) != 0)
1101 err = got_error_from_errno("fclose");
1108 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1110 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1114 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1116 return blob->hdrlen;
1120 got_object_blob_get_read_buf(struct got_blob_object *blob)
1122 return blob->read_buf;
1125 const struct got_error *
1126 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1130 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1131 if (n == 0 && ferror(blob->f))
1132 return got_ferror(blob->f, GOT_ERR_IO);
1137 const struct got_error *
1138 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1139 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1141 const struct got_error *err = NULL;
1142 size_t n, len, hdrlen;
1145 size_t noffsets = 0;
1149 *line_offsets = NULL;
1155 hdrlen = got_object_blob_get_hdrlen(blob);
1157 err = got_object_blob_read_block(&len, blob);
1162 buf = got_object_blob_get_read_buf(blob);
1163 for (i = hdrlen; i < len; i++) {
1168 if (line_offsets && nlines && noffsets < *nlines) {
1169 off_t *o = recallocarray(*line_offsets,
1170 noffsets, *nlines, sizeof(**line_offsets));
1172 free(*line_offsets);
1173 *line_offsets = NULL;
1174 return got_error_from_errno(
1180 if (line_offsets && nlines && total_len) {
1181 (*line_offsets)[*nlines - 1] = off;
1182 off = *total_len + i + 1 - got_object_blob_get_hdrlen(blob);
1187 /* Skip blob object header first time around. */
1188 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1189 if (n != len - hdrlen)
1190 return got_ferror(outfile, GOT_ERR_IO);
1194 if (fflush(outfile) != 0)
1195 return got_error_from_errno("fflush");
1201 static const struct got_error *
1202 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1203 int pack_idx, struct got_object_id *id)
1205 const struct got_error *err = NULL;
1207 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1212 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1215 static const struct got_error *
1216 read_packed_tag_privsep(struct got_tag_object **tag,
1217 struct got_pack *pack, struct got_packidx *packidx, int idx,
1218 struct got_object_id *id)
1220 const struct got_error *err = NULL;
1222 if (pack->privsep_child)
1223 return request_packed_tag(tag, pack, idx, id);
1225 err = start_pack_privsep_child(pack, packidx);
1229 return request_packed_tag(tag, pack, idx, id);
1232 static const struct got_error *
1233 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1236 const struct got_error *err = NULL;
1237 struct imsgbuf *ibuf;
1239 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1241 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1245 return got_privsep_recv_tag(tag, ibuf);
1248 static const struct got_error *
1249 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1250 struct got_repository *repo)
1254 struct imsgbuf *ibuf;
1256 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1257 return request_tag(tag, repo, obj_fd);
1259 ibuf = calloc(1, sizeof(*ibuf));
1261 return got_error_from_errno("calloc");
1263 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1264 return got_error_from_errno("socketpair");
1268 return got_error_from_errno("fork");
1269 else if (pid == 0) {
1270 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1275 if (close(imsg_fds[1]) != 0)
1276 return got_error_from_errno("close");
1277 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1279 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1280 imsg_init(ibuf, imsg_fds[0]);
1281 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1283 return request_tag(tag, repo, obj_fd);
1286 static const struct got_error *
1287 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1288 struct got_object_id *id, int check_cache)
1290 const struct got_error *err = NULL;
1291 struct got_packidx *packidx = NULL;
1293 char *path_packfile;
1296 *tag = got_repo_get_cached_tag(repo, id);
1304 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1306 struct got_pack *pack = NULL;
1308 err = get_packfile_path(&path_packfile, packidx);
1312 pack = got_repo_get_cached_pack(repo, path_packfile);
1314 err = got_repo_cache_pack(&pack, repo, path_packfile,
1319 err = read_packed_tag_privsep(tag, pack,
1321 } else if (err->code == GOT_ERR_NO_OBJ) {
1324 err = open_loose_object(&fd, id, repo);
1327 err = read_tag_privsep(tag, fd, repo);
1332 err = got_repo_cache_tag(repo, id, *tag);
1338 const struct got_error *
1339 got_object_open_as_tag(struct got_tag_object **tag,
1340 struct got_repository *repo, struct got_object_id *id)
1342 *tag = got_repo_get_cached_tag(repo, id);
1348 return open_tag(tag, repo, id, 0);
1351 const struct got_error *
1352 got_object_tag_open(struct got_tag_object **tag,
1353 struct got_repository *repo, struct got_object *obj)
1355 return open_tag(tag, repo, got_object_get_id(obj), 1);
1359 got_object_tag_get_object_type(struct got_tag_object *tag)
1361 return tag->obj_type;
1364 struct got_object_id *
1365 got_object_tag_get_object_id(struct got_tag_object *tag)
1370 static struct got_tree_entry *
1371 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1373 struct got_tree_entry *te;
1375 /* Note that tree entries are sorted in strncmp() order. */
1376 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1377 int cmp = strncmp(te->name, name, len);
1382 if (te->name[len] == '\0')
1388 const struct got_tree_entry *
1389 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1391 return find_entry_by_name(tree, name, strlen(name));
1394 const struct got_error *
1395 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1396 struct got_object_id *commit_id, const char *path)
1398 const struct got_error *err = NULL;
1399 struct got_commit_object *commit = NULL;
1400 struct got_tree_object *tree = NULL;
1401 struct got_tree_entry *te = NULL;
1402 const char *seg, *s;
1407 err = got_object_open_as_commit(&commit, repo, commit_id);
1411 /* Handle opening of root of commit's tree. */
1412 if (got_path_is_root_dir(path)) {
1413 *id = got_object_id_dup(commit->tree_id);
1415 err = got_error_from_errno("got_object_id_dup");
1419 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1429 struct got_tree_object *next_tree;
1438 te = find_entry_by_name(tree, seg, seglen);
1440 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1451 err = got_object_open_as_tree(&next_tree, repo,
1456 got_object_tree_close(tree);
1462 *id = got_object_id_dup(te->id);
1464 return got_error_from_errno("got_object_id_dup");
1466 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1469 got_object_commit_close(commit);
1471 got_object_tree_close(tree);
1475 const struct got_error *
1476 got_object_tree_path_changed(int *changed,
1477 struct got_tree_object *tree01, struct got_tree_object *tree02,
1478 const char *path, struct got_repository *repo)
1480 const struct got_error *err = NULL;
1481 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1482 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1483 const char *seg, *s;
1488 /* We are expecting an absolute in-repository path. */
1490 return got_error(GOT_ERR_NOT_ABSPATH);
1492 /* We not do support comparing the root path. */
1493 if (path[1] == '\0')
1494 return got_error(GOT_ERR_BAD_PATH);
1499 s++; /* skip leading '/' */
1503 struct got_tree_object *next_tree1, *next_tree2;
1512 te1 = find_entry_by_name(tree1, seg, seglen);
1514 err = got_error(GOT_ERR_NO_OBJ);
1518 te2 = find_entry_by_name(tree2, seg, seglen);
1524 if (te1->mode != te2->mode) {
1529 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1534 if (*s == '\0') { /* final path element */
1543 err = got_object_open_as_tree(&next_tree1, repo,
1548 if (tree1 != tree01)
1549 got_object_tree_close(tree1);
1552 err = got_object_open_as_tree(&next_tree2, repo,
1557 if (tree2 != tree02)
1558 got_object_tree_close(tree2);
1563 if (tree1 && tree1 != tree01)
1564 got_object_tree_close(tree1);
1565 if (tree2 && tree2 != tree02)
1566 got_object_tree_close(tree2);
1570 const struct got_error *
1571 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1572 struct got_tree_entry *te)
1574 const struct got_error *err = NULL;
1576 *new_te = calloc(1, sizeof(**new_te));
1577 if (*new_te == NULL)
1578 return got_error_from_errno("calloc");
1580 (*new_te)->mode = te->mode;
1581 (*new_te)->name = strdup(te->name);
1582 if ((*new_te)->name == NULL) {
1583 err = got_error_from_errno("strdup");
1587 (*new_te)->id = got_object_id_dup(te->id);
1588 if ((*new_te)->id == NULL) {
1589 err = got_error_from_errno("got_object_id_dup");
1594 got_object_tree_entry_close(*new_te);