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;
121 path_objects = got_repo_get_path_objects(repo);
122 if (path_objects == NULL)
123 return got_error_from_errno("got_repo_get_path_objects");
125 err = got_object_id_str(&hex, id);
129 if (asprintf(path, "%s/%.2x/%s", path_objects,
130 id->sha1[0], hex + 2) == -1)
131 err = got_error_from_errno("asprintf");
139 static const struct got_error *
140 open_loose_object(int *fd, struct got_object_id *id,
141 struct got_repository *repo)
143 const struct got_error *err = NULL;
146 err = got_object_get_path(&path, id, repo);
149 *fd = open(path, O_RDONLY | O_NOFOLLOW);
151 err = got_error_from_errno2("open", path);
159 static const struct got_error *
160 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
164 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
165 size = strlen(packidx->path_packidx) + 2;
166 if (size < GOT_PACKFILE_NAMELEN + 1)
167 return got_error(GOT_ERR_BAD_PATH);
169 *path_packfile = malloc(size);
170 if (*path_packfile == NULL)
171 return got_error_from_errno("malloc");
173 /* Copy up to and excluding ".idx". */
174 if (strlcpy(*path_packfile, packidx->path_packidx,
175 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
178 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
179 return got_error(GOT_ERR_NO_SPACE);
184 static const struct got_error *
185 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
186 struct got_object_id *id)
188 const struct got_error *err = NULL;
189 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
191 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
195 err = got_privsep_recv_obj(obj, ibuf);
199 (*obj)->path_packfile = strdup(pack->path_packfile);
200 if ((*obj)->path_packfile == NULL) {
201 err = got_error_from_errno("strdup");
204 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
210 set_max_datasize(void)
214 if (getrlimit(RLIMIT_DATA, &rl) != 0)
217 rl.rlim_cur = rl.rlim_max;
218 setrlimit(RLIMIT_DATA, &rl);
221 static const struct got_error *
222 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
224 const struct got_error *err = NULL;
227 struct imsgbuf *ibuf;
229 ibuf = calloc(1, sizeof(*ibuf));
231 return got_error_from_errno("calloc");
233 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
234 if (pack->privsep_child == NULL) {
235 err = got_error_from_errno("calloc");
240 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
241 err = got_error_from_errno("socketpair");
247 err = got_error_from_errno("fork");
249 } else if (pid == 0) {
251 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
252 pack->path_packfile);
256 if (close(imsg_fds[1]) != 0)
257 return got_error_from_errno("close");
258 pack->privsep_child->imsg_fd = imsg_fds[0];
259 pack->privsep_child->pid = pid;
260 imsg_init(ibuf, imsg_fds[0]);
261 pack->privsep_child->ibuf = ibuf;
263 err = got_privsep_init_pack_child(ibuf, pack, packidx);
265 const struct got_error *child_err;
266 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
267 child_err = got_privsep_wait_for_child(
268 pack->privsep_child->pid);
269 if (child_err && err == NULL)
275 free(pack->privsep_child);
276 pack->privsep_child = NULL;
281 static const struct got_error *
282 read_packed_object_privsep(struct got_object **obj,
283 struct got_repository *repo, struct got_pack *pack,
284 struct got_packidx *packidx, int idx, struct got_object_id *id)
286 const struct got_error *err = NULL;
288 if (pack->privsep_child)
289 return request_packed_object(obj, pack, idx, id);
291 err = start_pack_privsep_child(pack, packidx);
295 return request_packed_object(obj, pack, idx, id);
299 static const struct got_error *
300 open_packed_object(struct got_object **obj, struct got_object_id *id,
301 struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_pack *pack = NULL;
305 struct got_packidx *packidx = NULL;
309 err = got_repo_search_packidx(&packidx, &idx, repo, id);
313 err = get_packfile_path(&path_packfile, packidx);
317 pack = got_repo_get_cached_pack(repo, path_packfile);
319 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
324 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
328 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
334 static const struct got_error *
335 request_object(struct got_object **obj, struct got_repository *repo, int fd)
337 const struct got_error *err = NULL;
338 struct imsgbuf *ibuf;
340 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
342 err = got_privsep_send_obj_req(ibuf, fd);
346 return got_privsep_recv_obj(obj, ibuf);
349 static const struct got_error *
350 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
355 struct imsgbuf *ibuf;
357 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
358 return request_object(obj, repo, obj_fd);
360 ibuf = calloc(1, sizeof(*ibuf));
362 return got_error_from_errno("calloc");
364 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
365 return got_error_from_errno("socketpair");
369 return got_error_from_errno("fork");
371 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
376 if (close(imsg_fds[1]) != 0)
377 return got_error_from_errno("close");
378 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
380 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
381 imsg_init(ibuf, imsg_fds[0]);
382 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
384 return request_object(obj, repo, obj_fd);
388 const struct got_error *
389 got_object_open(struct got_object **obj, struct got_repository *repo,
390 struct got_object_id *id)
392 const struct got_error *err = NULL;
396 *obj = got_repo_get_cached_object(repo, id);
402 err = open_packed_object(obj, id, repo);
403 if (err && err->code != GOT_ERR_NO_OBJ)
407 return got_repo_cache_object(repo, id, *obj);
410 err = got_object_get_path(&path, id, repo);
414 fd = open(path, O_RDONLY | O_NOFOLLOW);
417 err = got_error_no_obj(id);
419 err = got_error_from_errno2("open", path);
422 err = read_object_header_privsep(obj, repo, fd);
425 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
429 err = got_repo_cache_object(repo, id, *obj);
436 const struct got_error *
437 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
440 struct got_object_id id;
442 if (!got_parse_sha1_digest(id.sha1, id_str))
443 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
445 return got_object_open(obj, repo, &id);
448 const struct got_error *
449 got_object_resolve_id_str(struct got_object_id **id,
450 struct got_repository *repo, const char *id_str)
452 const struct got_error *err = NULL;
453 struct got_object *obj;
455 err = got_object_open_by_id_str(&obj, repo, id_str);
459 *id = got_object_id_dup(got_object_get_id(obj));
460 got_object_close(obj);
462 return got_error_from_errno("got_object_id_dup");
467 static const struct got_error *
468 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
469 int pack_idx, struct got_object_id *id)
471 const struct got_error *err = NULL;
473 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
478 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
481 static const struct got_error *
482 read_packed_commit_privsep(struct got_commit_object **commit,
483 struct got_pack *pack, struct got_packidx *packidx, int idx,
484 struct got_object_id *id)
486 const struct got_error *err = NULL;
488 if (pack->privsep_child)
489 return request_packed_commit(commit, pack, idx, id);
491 err = start_pack_privsep_child(pack, packidx);
495 return request_packed_commit(commit, pack, idx, id);
498 static const struct got_error *
499 request_commit(struct got_commit_object **commit, struct got_repository *repo,
502 const struct got_error *err = NULL;
503 struct imsgbuf *ibuf;
505 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
507 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
511 return got_privsep_recv_commit(commit, ibuf);
514 static const struct got_error *
515 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
516 struct got_repository *repo)
520 struct imsgbuf *ibuf;
522 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
523 return request_commit(commit, repo, obj_fd);
525 ibuf = calloc(1, sizeof(*ibuf));
527 return got_error_from_errno("calloc");
529 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
530 return got_error_from_errno("socketpair");
534 return got_error_from_errno("fork");
536 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
541 if (close(imsg_fds[1]) != 0)
542 return got_error_from_errno("close");
543 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
545 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
546 imsg_init(ibuf, imsg_fds[0]);
547 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
549 return request_commit(commit, repo, obj_fd);
553 static const struct got_error *
554 open_commit(struct got_commit_object **commit,
555 struct got_repository *repo, struct got_object_id *id, int check_cache)
557 const struct got_error *err = NULL;
558 struct got_packidx *packidx = NULL;
560 char *path_packfile = NULL;
563 *commit = got_repo_get_cached_commit(repo, id);
564 if (*commit != NULL) {
571 err = got_repo_search_packidx(&packidx, &idx, repo, id);
573 struct got_pack *pack = NULL;
575 err = get_packfile_path(&path_packfile, packidx);
579 pack = got_repo_get_cached_pack(repo, path_packfile);
581 err = got_repo_cache_pack(&pack, repo, path_packfile,
586 err = read_packed_commit_privsep(commit, pack,
588 } else if (err->code == GOT_ERR_NO_OBJ) {
591 err = open_loose_object(&fd, id, repo);
594 err = read_commit_privsep(commit, fd, repo);
599 err = got_repo_cache_commit(repo, id, *commit);
606 const struct got_error *
607 got_object_open_as_commit(struct got_commit_object **commit,
608 struct got_repository *repo, struct got_object_id *id)
610 *commit = got_repo_get_cached_commit(repo, id);
611 if (*commit != NULL) {
616 return open_commit(commit, repo, id, 0);
619 const struct got_error *
620 got_object_commit_open(struct got_commit_object **commit,
621 struct got_repository *repo, struct got_object *obj)
623 return open_commit(commit, repo, got_object_get_id(obj), 1);
626 const struct got_error *
627 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
629 const struct got_error *err = NULL;
631 *qid = calloc(1, sizeof(**qid));
633 return got_error_from_errno("calloc");
635 (*qid)->id = got_object_id_dup(id);
636 if ((*qid)->id == NULL) {
637 err = got_error_from_errno("got_object_id_dup");
638 got_object_qid_free(*qid);
646 static const struct got_error *
647 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
648 int pack_idx, struct got_object_id *id)
650 const struct got_error *err = NULL;
652 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
657 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
660 static const struct got_error *
661 read_packed_tree_privsep(struct got_tree_object **tree,
662 struct got_pack *pack, struct got_packidx *packidx, int idx,
663 struct got_object_id *id)
665 const struct got_error *err = NULL;
667 if (pack->privsep_child)
668 return request_packed_tree(tree, pack, idx, id);
670 err = start_pack_privsep_child(pack, packidx);
674 return request_packed_tree(tree, pack, idx, id);
677 static const struct got_error *
678 request_tree(struct got_tree_object **tree, struct got_repository *repo,
681 const struct got_error *err = NULL;
682 struct imsgbuf *ibuf;
684 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
686 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
690 return got_privsep_recv_tree(tree, ibuf);
693 const struct got_error *
694 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
695 struct got_repository *repo)
699 struct imsgbuf *ibuf;
701 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
702 return request_tree(tree, repo, obj_fd);
704 ibuf = calloc(1, sizeof(*ibuf));
706 return got_error_from_errno("calloc");
708 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
709 return got_error_from_errno("socketpair");
713 return got_error_from_errno("fork");
715 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
720 if (close(imsg_fds[1]) != 0)
721 return got_error_from_errno("close");
722 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
724 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
725 imsg_init(ibuf, imsg_fds[0]);
726 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
729 return request_tree(tree, repo, obj_fd);
732 static const struct got_error *
733 open_tree(struct got_tree_object **tree, struct got_repository *repo,
734 struct got_object_id *id, int check_cache)
736 const struct got_error *err = NULL;
737 struct got_packidx *packidx = NULL;
739 char *path_packfile = NULL;
742 *tree = got_repo_get_cached_tree(repo, id);
750 err = got_repo_search_packidx(&packidx, &idx, repo, id);
752 struct got_pack *pack = NULL;
754 err = get_packfile_path(&path_packfile, packidx);
758 pack = got_repo_get_cached_pack(repo, path_packfile);
760 err = got_repo_cache_pack(&pack, repo, path_packfile,
765 err = read_packed_tree_privsep(tree, pack,
767 } else if (err->code == GOT_ERR_NO_OBJ) {
770 err = open_loose_object(&fd, id, repo);
773 err = read_tree_privsep(tree, fd, repo);
778 err = got_repo_cache_tree(repo, id, *tree);
785 const struct got_error *
786 got_object_open_as_tree(struct got_tree_object **tree,
787 struct got_repository *repo, struct got_object_id *id)
789 *tree = got_repo_get_cached_tree(repo, id);
795 return open_tree(tree, repo, id, 0);
798 const struct got_error *
799 got_object_tree_open(struct got_tree_object **tree,
800 struct got_repository *repo, struct got_object *obj)
802 return open_tree(tree, repo, got_object_get_id(obj), 1);
805 const struct got_tree_entries *
806 got_object_tree_get_entries(struct got_tree_object *tree)
808 return &tree->entries;
811 static const struct got_error *
812 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
813 struct got_pack *pack, struct got_packidx *packidx, int idx,
814 struct got_object_id *id)
816 const struct got_error *err = NULL;
818 int basefd, accumfd; /* temporary files for delta application */
820 basefd = got_opentempfd();
822 return got_error_from_errno("got_opentempfd");
823 accumfd = got_opentempfd();
825 return got_error_from_errno("got_opentempfd");
827 outfd_child = dup(outfd);
828 if (outfd_child == -1)
829 return got_error_from_errno("dup");
831 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
835 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
843 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
850 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
855 err = got_privsep_recv_blob(outbuf, size, hdrlen,
856 pack->privsep_child->ibuf);
860 if (lseek(outfd, SEEK_SET, 0) == -1)
861 err = got_error_from_errno("lseek");
866 static const struct got_error *
867 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
868 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
869 struct got_object_id *id)
871 const struct got_error *err = NULL;
873 if (pack->privsep_child == NULL) {
874 err = start_pack_privsep_child(pack, packidx);
879 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
883 static const struct got_error *
884 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
885 int infd, struct imsgbuf *ibuf)
887 const struct got_error *err = NULL;
890 outfd_child = dup(outfd);
891 if (outfd_child == -1)
892 return got_error_from_errno("dup");
894 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
898 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
902 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
906 if (lseek(outfd, SEEK_SET, 0) == -1)
907 return got_error_from_errno("lseek");
912 static const struct got_error *
913 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
914 int outfd, int infd, struct got_repository *repo)
918 struct imsgbuf *ibuf;
920 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
921 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
922 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
925 ibuf = calloc(1, sizeof(*ibuf));
927 return got_error_from_errno("calloc");
929 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
930 return got_error_from_errno("socketpair");
934 return got_error_from_errno("fork");
936 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
941 if (close(imsg_fds[1]) != 0)
942 return got_error_from_errno("close");
943 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
945 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
946 imsg_init(ibuf, imsg_fds[0]);
947 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
949 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
952 static const struct got_error *
953 open_blob(struct got_blob_object **blob, struct got_repository *repo,
954 struct got_object_id *id, size_t blocksize)
956 const struct got_error *err = NULL;
957 struct got_packidx *packidx = NULL;
959 char *path_packfile = NULL;
965 *blob = calloc(1, sizeof(**blob));
967 return got_error_from_errno("calloc");
969 outfd = got_opentempfd();
971 return got_error_from_errno("got_opentempfd");
973 (*blob)->read_buf = malloc(blocksize);
974 if ((*blob)->read_buf == NULL) {
975 err = got_error_from_errno("malloc");
979 err = got_repo_search_packidx(&packidx, &idx, repo, id);
981 struct got_pack *pack = NULL;
983 err = get_packfile_path(&path_packfile, packidx);
987 pack = got_repo_get_cached_pack(repo, path_packfile);
989 err = got_repo_cache_pack(&pack, repo, path_packfile,
994 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
995 pack, packidx, idx, id);
996 } else if (err->code == GOT_ERR_NO_OBJ) {
999 err = open_loose_object(&infd, id, repo);
1002 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1008 if (hdrlen > size) {
1009 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1014 if (close(outfd) != 0 && err == NULL)
1015 err = got_error_from_errno("close");
1017 (*blob)->f = fmemopen(outbuf, size, "rb");
1018 if ((*blob)->f == NULL) {
1019 err = got_error_from_errno("fmemopen");
1023 (*blob)->data = outbuf;
1025 if (fstat(outfd, &sb) == -1) {
1026 err = got_error_from_errno("fstat");
1030 if (sb.st_size != size) {
1031 err = got_error(GOT_ERR_PRIVSEP_LEN);
1035 (*blob)->f = fdopen(outfd, "rb");
1036 if ((*blob)->f == NULL) {
1037 err = got_error_from_errno("fdopen");
1044 (*blob)->hdrlen = hdrlen;
1045 (*blob)->blocksize = blocksize;
1046 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1049 free(path_packfile);
1052 got_object_blob_close(*blob);
1054 } else if (outfd != -1)
1060 const struct got_error *
1061 got_object_open_as_blob(struct got_blob_object **blob,
1062 struct got_repository *repo, struct got_object_id *id,
1065 return open_blob(blob, repo, id, blocksize);
1068 const struct got_error *
1069 got_object_blob_open(struct got_blob_object **blob,
1070 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1072 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1075 const struct got_error *
1076 got_object_blob_close(struct got_blob_object *blob)
1078 const struct got_error *err = NULL;
1079 free(blob->read_buf);
1080 if (blob->f && fclose(blob->f) != 0)
1081 err = got_error_from_errno("fclose");
1088 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1090 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1094 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1096 return blob->hdrlen;
1100 got_object_blob_get_read_buf(struct got_blob_object *blob)
1102 return blob->read_buf;
1105 const struct got_error *
1106 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1110 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1111 if (n == 0 && ferror(blob->f))
1112 return got_ferror(blob->f, GOT_ERR_IO);
1117 const struct got_error *
1118 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1119 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1121 const struct got_error *err = NULL;
1122 size_t n, len, hdrlen;
1125 size_t noffsets = 0;
1126 off_t off = 0, total_len = 0;
1129 *line_offsets = NULL;
1135 hdrlen = got_object_blob_get_hdrlen(blob);
1137 err = got_object_blob_read_block(&len, blob);
1142 buf = got_object_blob_get_read_buf(blob);
1144 if (line_offsets && nlines) {
1145 if (*line_offsets == NULL) {
1146 /* Have some data but perhaps no '\n'. */
1149 *line_offsets = calloc(1, sizeof(**line_offsets));
1150 if (*line_offsets == NULL)
1151 return got_error_from_errno("malloc");
1153 /* Skip forward over end of first line. */
1160 /* Scan '\n' offsets in remaining chunk of data. */
1162 if (buf[i] != '\n') {
1167 if (noffsets < *nlines) {
1168 off_t *o = recallocarray(*line_offsets,
1170 sizeof(**line_offsets));
1172 free(*line_offsets);
1173 *line_offsets = NULL;
1174 return got_error_from_errno(
1180 off = total_len + i - hdrlen + 1;
1181 (*line_offsets)[*nlines - 1] = off;
1185 /* Skip blob object header first time around. */
1186 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1187 if (n != len - hdrlen)
1188 return got_ferror(outfile, GOT_ERR_IO);
1189 total_len += len - hdrlen;
1193 if (fflush(outfile) != 0)
1194 return got_error_from_errno("fflush");
1198 *filesize = total_len;
1203 static const struct got_error *
1204 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1205 int pack_idx, struct got_object_id *id)
1207 const struct got_error *err = NULL;
1209 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1214 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1217 static const struct got_error *
1218 read_packed_tag_privsep(struct got_tag_object **tag,
1219 struct got_pack *pack, struct got_packidx *packidx, int idx,
1220 struct got_object_id *id)
1222 const struct got_error *err = NULL;
1224 if (pack->privsep_child)
1225 return request_packed_tag(tag, pack, idx, id);
1227 err = start_pack_privsep_child(pack, packidx);
1231 return request_packed_tag(tag, pack, idx, id);
1234 static const struct got_error *
1235 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1238 const struct got_error *err = NULL;
1239 struct imsgbuf *ibuf;
1241 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1243 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1247 return got_privsep_recv_tag(tag, ibuf);
1250 static const struct got_error *
1251 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1252 struct got_repository *repo)
1256 struct imsgbuf *ibuf;
1258 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1259 return request_tag(tag, repo, obj_fd);
1261 ibuf = calloc(1, sizeof(*ibuf));
1263 return got_error_from_errno("calloc");
1265 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1266 return got_error_from_errno("socketpair");
1270 return got_error_from_errno("fork");
1271 else if (pid == 0) {
1272 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1277 if (close(imsg_fds[1]) != 0)
1278 return got_error_from_errno("close");
1279 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1281 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1282 imsg_init(ibuf, imsg_fds[0]);
1283 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1285 return request_tag(tag, repo, obj_fd);
1288 static const struct got_error *
1289 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1290 struct got_object_id *id, int check_cache)
1292 const struct got_error *err = NULL;
1293 struct got_packidx *packidx = NULL;
1295 char *path_packfile = NULL;
1296 struct got_object *obj = NULL;
1297 int obj_type = GOT_OBJ_TYPE_ANY;
1300 *tag = got_repo_get_cached_tag(repo, id);
1308 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1310 struct got_pack *pack = NULL;
1312 err = get_packfile_path(&path_packfile, packidx);
1316 pack = got_repo_get_cached_pack(repo, path_packfile);
1318 err = got_repo_cache_pack(&pack, repo, path_packfile,
1324 /* Beware of "leightweight" tags: Check object type first. */
1325 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1329 obj_type = obj->type;
1330 got_object_close(obj);
1331 if (obj_type != GOT_OBJ_TYPE_TAG) {
1332 err = got_error(GOT_ERR_OBJ_TYPE);
1335 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1336 } else if (err->code == GOT_ERR_NO_OBJ) {
1339 err = open_loose_object(&fd, id, repo);
1342 err = read_object_header_privsep(&obj, repo, fd);
1345 obj_type = obj->type;
1346 got_object_close(obj);
1347 if (obj_type != GOT_OBJ_TYPE_TAG)
1348 return got_error(GOT_ERR_OBJ_TYPE);
1350 err = open_loose_object(&fd, id, repo);
1353 err = read_tag_privsep(tag, fd, repo);
1358 err = got_repo_cache_tag(repo, id, *tag);
1361 free(path_packfile);
1365 const struct got_error *
1366 got_object_open_as_tag(struct got_tag_object **tag,
1367 struct got_repository *repo, struct got_object_id *id)
1369 *tag = got_repo_get_cached_tag(repo, id);
1375 return open_tag(tag, repo, id, 0);
1378 const struct got_error *
1379 got_object_tag_open(struct got_tag_object **tag,
1380 struct got_repository *repo, struct got_object *obj)
1382 return open_tag(tag, repo, got_object_get_id(obj), 1);
1386 got_object_tag_get_name(struct got_tag_object *tag)
1392 got_object_tag_get_object_type(struct got_tag_object *tag)
1394 return tag->obj_type;
1397 struct got_object_id *
1398 got_object_tag_get_object_id(struct got_tag_object *tag)
1404 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1406 return tag->tagger_time;
1410 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1412 return tag->tagger_gmtoff;
1416 got_object_tag_get_tagger(struct got_tag_object *tag)
1422 got_object_tag_get_message(struct got_tag_object *tag)
1427 static struct got_tree_entry *
1428 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1430 struct got_tree_entry *te;
1432 /* Note that tree entries are sorted in strncmp() order. */
1433 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1434 int cmp = strncmp(te->name, name, len);
1439 if (te->name[len] == '\0')
1445 const struct got_tree_entry *
1446 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1448 return find_entry_by_name(tree, name, strlen(name));
1451 const struct got_error *
1452 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1453 struct got_object_id *commit_id, const char *path)
1455 const struct got_error *err = NULL;
1456 struct got_commit_object *commit = NULL;
1457 struct got_tree_object *tree = NULL;
1458 struct got_tree_entry *te = NULL;
1459 const char *seg, *s;
1464 err = got_object_open_as_commit(&commit, repo, commit_id);
1468 /* Handle opening of root of commit's tree. */
1469 if (got_path_is_root_dir(path)) {
1470 *id = got_object_id_dup(commit->tree_id);
1472 err = got_error_from_errno("got_object_id_dup");
1476 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1486 struct got_tree_object *next_tree;
1495 te = find_entry_by_name(tree, seg, seglen);
1497 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1508 err = got_object_open_as_tree(&next_tree, repo,
1513 got_object_tree_close(tree);
1519 *id = got_object_id_dup(te->id);
1521 return got_error_from_errno("got_object_id_dup");
1523 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1526 got_object_commit_close(commit);
1528 got_object_tree_close(tree);
1532 const struct got_error *
1533 got_object_tree_path_changed(int *changed,
1534 struct got_tree_object *tree01, struct got_tree_object *tree02,
1535 const char *path, struct got_repository *repo)
1537 const struct got_error *err = NULL;
1538 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1539 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1540 const char *seg, *s;
1545 /* We are expecting an absolute in-repository path. */
1547 return got_error(GOT_ERR_NOT_ABSPATH);
1549 /* We not do support comparing the root path. */
1550 if (path[1] == '\0')
1551 return got_error(GOT_ERR_BAD_PATH);
1556 s++; /* skip leading '/' */
1560 struct got_tree_object *next_tree1, *next_tree2;
1569 te1 = find_entry_by_name(tree1, seg, seglen);
1571 err = got_error(GOT_ERR_NO_OBJ);
1575 te2 = find_entry_by_name(tree2, seg, seglen);
1581 if (te1->mode != te2->mode) {
1586 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1591 if (*s == '\0') { /* final path element */
1600 err = got_object_open_as_tree(&next_tree1, repo,
1605 if (tree1 != tree01)
1606 got_object_tree_close(tree1);
1609 err = got_object_open_as_tree(&next_tree2, repo,
1614 if (tree2 != tree02)
1615 got_object_tree_close(tree2);
1620 if (tree1 && tree1 != tree01)
1621 got_object_tree_close(tree1);
1622 if (tree2 && tree2 != tree02)
1623 got_object_tree_close(tree2);
1627 const struct got_error *
1628 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1629 struct got_tree_entry *te)
1631 const struct got_error *err = NULL;
1633 *new_te = calloc(1, sizeof(**new_te));
1634 if (*new_te == NULL)
1635 return got_error_from_errno("calloc");
1637 (*new_te)->mode = te->mode;
1638 (*new_te)->name = strdup(te->name);
1639 if ((*new_te)->name == NULL) {
1640 err = got_error_from_errno("strdup");
1644 (*new_te)->id = got_object_id_dup(te->id);
1645 if ((*new_te)->id == NULL) {
1646 err = got_error_from_errno("got_object_id_dup");
1651 got_object_tree_entry_close(*new_te);
1658 got_object_tree_entry_is_submodule(const struct got_tree_entry *te)
1660 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);