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,
353 const struct got_error *err;
356 struct imsgbuf *ibuf;
358 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
359 return request_object(obj, repo, obj_fd);
361 ibuf = calloc(1, sizeof(*ibuf));
363 return got_error_from_errno("calloc");
365 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
366 err = got_error_from_errno("socketpair");
373 err = got_error_from_errno("fork");
378 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
383 if (close(imsg_fds[1]) != 0) {
384 err = got_error_from_errno("close");
388 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
390 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
391 imsg_init(ibuf, imsg_fds[0]);
392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
394 return request_object(obj, repo, obj_fd);
398 const struct got_error *
399 got_object_open(struct got_object **obj, struct got_repository *repo,
400 struct got_object_id *id)
402 const struct got_error *err = NULL;
406 *obj = got_repo_get_cached_object(repo, id);
412 err = open_packed_object(obj, id, repo);
413 if (err && err->code != GOT_ERR_NO_OBJ)
417 return got_repo_cache_object(repo, id, *obj);
420 err = got_object_get_path(&path, id, repo);
424 fd = open(path, O_RDONLY | O_NOFOLLOW);
427 err = got_error_no_obj(id);
429 err = got_error_from_errno2("open", path);
432 err = read_object_header_privsep(obj, repo, fd);
435 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
439 err = got_repo_cache_object(repo, id, *obj);
446 const struct got_error *
447 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
450 struct got_object_id id;
452 if (!got_parse_sha1_digest(id.sha1, id_str))
453 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
455 return got_object_open(obj, repo, &id);
458 const struct got_error *
459 got_object_resolve_id_str(struct got_object_id **id,
460 struct got_repository *repo, const char *id_str)
462 const struct got_error *err = NULL;
463 struct got_object *obj;
465 err = got_object_open_by_id_str(&obj, repo, id_str);
469 *id = got_object_id_dup(got_object_get_id(obj));
470 got_object_close(obj);
472 return got_error_from_errno("got_object_id_dup");
477 static const struct got_error *
478 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
479 int pack_idx, struct got_object_id *id)
481 const struct got_error *err = NULL;
483 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
488 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
491 static const struct got_error *
492 read_packed_commit_privsep(struct got_commit_object **commit,
493 struct got_pack *pack, struct got_packidx *packidx, int idx,
494 struct got_object_id *id)
496 const struct got_error *err = NULL;
498 if (pack->privsep_child)
499 return request_packed_commit(commit, pack, idx, id);
501 err = start_pack_privsep_child(pack, packidx);
505 return request_packed_commit(commit, pack, idx, id);
508 static const struct got_error *
509 request_commit(struct got_commit_object **commit, struct got_repository *repo,
512 const struct got_error *err = NULL;
513 struct imsgbuf *ibuf;
515 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
517 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
521 return got_privsep_recv_commit(commit, ibuf);
524 static const struct got_error *
525 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
526 struct got_repository *repo)
528 const struct got_error *err;
531 struct imsgbuf *ibuf;
533 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
534 return request_commit(commit, repo, obj_fd);
536 ibuf = calloc(1, sizeof(*ibuf));
538 return got_error_from_errno("calloc");
540 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
541 err = got_error_from_errno("socketpair");
548 err = got_error_from_errno("fork");
553 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
558 if (close(imsg_fds[1]) != 0) {
559 err = got_error_from_errno("close");
563 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
565 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
566 imsg_init(ibuf, imsg_fds[0]);
567 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
569 return request_commit(commit, repo, obj_fd);
573 static const struct got_error *
574 open_commit(struct got_commit_object **commit,
575 struct got_repository *repo, struct got_object_id *id, int check_cache)
577 const struct got_error *err = NULL;
578 struct got_packidx *packidx = NULL;
580 char *path_packfile = NULL;
583 *commit = got_repo_get_cached_commit(repo, id);
584 if (*commit != NULL) {
591 err = got_repo_search_packidx(&packidx, &idx, repo, id);
593 struct got_pack *pack = NULL;
595 err = get_packfile_path(&path_packfile, packidx);
599 pack = got_repo_get_cached_pack(repo, path_packfile);
601 err = got_repo_cache_pack(&pack, repo, path_packfile,
606 err = read_packed_commit_privsep(commit, pack,
608 } else if (err->code == GOT_ERR_NO_OBJ) {
611 err = open_loose_object(&fd, id, repo);
614 err = read_commit_privsep(commit, fd, repo);
619 err = got_repo_cache_commit(repo, id, *commit);
626 const struct got_error *
627 got_object_open_as_commit(struct got_commit_object **commit,
628 struct got_repository *repo, struct got_object_id *id)
630 *commit = got_repo_get_cached_commit(repo, id);
631 if (*commit != NULL) {
636 return open_commit(commit, repo, id, 0);
639 const struct got_error *
640 got_object_commit_open(struct got_commit_object **commit,
641 struct got_repository *repo, struct got_object *obj)
643 return open_commit(commit, repo, got_object_get_id(obj), 1);
646 const struct got_error *
647 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
649 const struct got_error *err = NULL;
651 *qid = calloc(1, sizeof(**qid));
653 return got_error_from_errno("calloc");
655 (*qid)->id = got_object_id_dup(id);
656 if ((*qid)->id == NULL) {
657 err = got_error_from_errno("got_object_id_dup");
658 got_object_qid_free(*qid);
666 static const struct got_error *
667 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
668 int pack_idx, struct got_object_id *id)
670 const struct got_error *err = NULL;
672 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
677 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
680 static const struct got_error *
681 read_packed_tree_privsep(struct got_tree_object **tree,
682 struct got_pack *pack, struct got_packidx *packidx, int idx,
683 struct got_object_id *id)
685 const struct got_error *err = NULL;
687 if (pack->privsep_child)
688 return request_packed_tree(tree, pack, idx, id);
690 err = start_pack_privsep_child(pack, packidx);
694 return request_packed_tree(tree, pack, idx, id);
697 static const struct got_error *
698 request_tree(struct got_tree_object **tree, struct got_repository *repo,
701 const struct got_error *err = NULL;
702 struct imsgbuf *ibuf;
704 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
706 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
710 return got_privsep_recv_tree(tree, ibuf);
713 const struct got_error *
714 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
715 struct got_repository *repo)
717 const struct got_error *err;
720 struct imsgbuf *ibuf;
722 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
723 return request_tree(tree, repo, obj_fd);
725 ibuf = calloc(1, sizeof(*ibuf));
727 return got_error_from_errno("calloc");
729 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
730 err = got_error_from_errno("socketpair");
737 err = got_error_from_errno("fork");
742 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
747 if (close(imsg_fds[1]) != 0) {
748 err = got_error_from_errno("close");
752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
754 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
755 imsg_init(ibuf, imsg_fds[0]);
756 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
759 return request_tree(tree, repo, obj_fd);
762 static const struct got_error *
763 open_tree(struct got_tree_object **tree, struct got_repository *repo,
764 struct got_object_id *id, int check_cache)
766 const struct got_error *err = NULL;
767 struct got_packidx *packidx = NULL;
769 char *path_packfile = NULL;
772 *tree = got_repo_get_cached_tree(repo, id);
780 err = got_repo_search_packidx(&packidx, &idx, repo, id);
782 struct got_pack *pack = NULL;
784 err = get_packfile_path(&path_packfile, packidx);
788 pack = got_repo_get_cached_pack(repo, path_packfile);
790 err = got_repo_cache_pack(&pack, repo, path_packfile,
795 err = read_packed_tree_privsep(tree, pack,
797 } else if (err->code == GOT_ERR_NO_OBJ) {
800 err = open_loose_object(&fd, id, repo);
803 err = read_tree_privsep(tree, fd, repo);
808 err = got_repo_cache_tree(repo, id, *tree);
815 const struct got_error *
816 got_object_open_as_tree(struct got_tree_object **tree,
817 struct got_repository *repo, struct got_object_id *id)
819 *tree = got_repo_get_cached_tree(repo, id);
825 return open_tree(tree, repo, id, 0);
828 const struct got_error *
829 got_object_tree_open(struct got_tree_object **tree,
830 struct got_repository *repo, struct got_object *obj)
832 return open_tree(tree, repo, got_object_get_id(obj), 1);
835 const struct got_tree_entries *
836 got_object_tree_get_entries(struct got_tree_object *tree)
838 return &tree->entries;
841 static const struct got_error *
842 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
843 struct got_pack *pack, struct got_packidx *packidx, int idx,
844 struct got_object_id *id)
846 const struct got_error *err = NULL;
848 int basefd, accumfd; /* temporary files for delta application */
850 basefd = got_opentempfd();
852 return got_error_from_errno("got_opentempfd");
853 accumfd = got_opentempfd();
855 return got_error_from_errno("got_opentempfd");
857 outfd_child = dup(outfd);
858 if (outfd_child == -1)
859 return got_error_from_errno("dup");
861 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
865 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
873 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
880 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
885 err = got_privsep_recv_blob(outbuf, size, hdrlen,
886 pack->privsep_child->ibuf);
890 if (lseek(outfd, SEEK_SET, 0) == -1)
891 err = got_error_from_errno("lseek");
896 static const struct got_error *
897 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
898 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
899 struct got_object_id *id)
901 const struct got_error *err = NULL;
903 if (pack->privsep_child == NULL) {
904 err = start_pack_privsep_child(pack, packidx);
909 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
913 static const struct got_error *
914 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
915 int infd, struct imsgbuf *ibuf)
917 const struct got_error *err = NULL;
920 outfd_child = dup(outfd);
921 if (outfd_child == -1)
922 return got_error_from_errno("dup");
924 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
928 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
932 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
936 if (lseek(outfd, SEEK_SET, 0) == -1)
937 return got_error_from_errno("lseek");
942 static const struct got_error *
943 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
944 int outfd, int infd, struct got_repository *repo)
946 const struct got_error *err;
949 struct imsgbuf *ibuf;
951 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
952 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
953 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
956 ibuf = calloc(1, sizeof(*ibuf));
958 return got_error_from_errno("calloc");
960 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
961 err = got_error_from_errno("socketpair");
968 err = got_error_from_errno("fork");
973 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
978 if (close(imsg_fds[1]) != 0) {
979 err = got_error_from_errno("close");
983 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
985 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
986 imsg_init(ibuf, imsg_fds[0]);
987 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
989 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
992 static const struct got_error *
993 open_blob(struct got_blob_object **blob, struct got_repository *repo,
994 struct got_object_id *id, size_t blocksize)
996 const struct got_error *err = NULL;
997 struct got_packidx *packidx = NULL;
999 char *path_packfile = NULL;
1002 size_t size, hdrlen;
1005 *blob = calloc(1, sizeof(**blob));
1007 return got_error_from_errno("calloc");
1009 outfd = got_opentempfd();
1011 return got_error_from_errno("got_opentempfd");
1013 (*blob)->read_buf = malloc(blocksize);
1014 if ((*blob)->read_buf == NULL) {
1015 err = got_error_from_errno("malloc");
1019 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1021 struct got_pack *pack = NULL;
1023 err = get_packfile_path(&path_packfile, packidx);
1027 pack = got_repo_get_cached_pack(repo, path_packfile);
1029 err = got_repo_cache_pack(&pack, repo, path_packfile,
1034 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1035 pack, packidx, idx, id);
1036 } else if (err->code == GOT_ERR_NO_OBJ) {
1039 err = open_loose_object(&infd, id, repo);
1042 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1048 if (hdrlen > size) {
1049 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1054 if (close(outfd) != 0 && err == NULL)
1055 err = got_error_from_errno("close");
1057 (*blob)->f = fmemopen(outbuf, size, "rb");
1058 if ((*blob)->f == NULL) {
1059 err = got_error_from_errno("fmemopen");
1063 (*blob)->data = outbuf;
1065 if (fstat(outfd, &sb) == -1) {
1066 err = got_error_from_errno("fstat");
1070 if (sb.st_size != size) {
1071 err = got_error(GOT_ERR_PRIVSEP_LEN);
1075 (*blob)->f = fdopen(outfd, "rb");
1076 if ((*blob)->f == NULL) {
1077 err = got_error_from_errno("fdopen");
1084 (*blob)->hdrlen = hdrlen;
1085 (*blob)->blocksize = blocksize;
1086 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1089 free(path_packfile);
1092 got_object_blob_close(*blob);
1094 } else if (outfd != -1)
1100 const struct got_error *
1101 got_object_open_as_blob(struct got_blob_object **blob,
1102 struct got_repository *repo, struct got_object_id *id,
1105 return open_blob(blob, repo, id, blocksize);
1108 const struct got_error *
1109 got_object_blob_open(struct got_blob_object **blob,
1110 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1112 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1115 const struct got_error *
1116 got_object_blob_close(struct got_blob_object *blob)
1118 const struct got_error *err = NULL;
1119 free(blob->read_buf);
1120 if (blob->f && fclose(blob->f) != 0)
1121 err = got_error_from_errno("fclose");
1128 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1130 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1134 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1136 return blob->hdrlen;
1140 got_object_blob_get_read_buf(struct got_blob_object *blob)
1142 return blob->read_buf;
1145 const struct got_error *
1146 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1150 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1151 if (n == 0 && ferror(blob->f))
1152 return got_ferror(blob->f, GOT_ERR_IO);
1157 const struct got_error *
1158 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1159 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1161 const struct got_error *err = NULL;
1162 size_t n, len, hdrlen;
1165 size_t noffsets = 0;
1166 off_t off = 0, total_len = 0;
1169 *line_offsets = NULL;
1175 hdrlen = got_object_blob_get_hdrlen(blob);
1177 err = got_object_blob_read_block(&len, blob);
1182 buf = got_object_blob_get_read_buf(blob);
1184 if (line_offsets && nlines) {
1185 if (*line_offsets == NULL) {
1186 /* Have some data but perhaps no '\n'. */
1189 *line_offsets = calloc(1, sizeof(**line_offsets));
1190 if (*line_offsets == NULL)
1191 return got_error_from_errno("malloc");
1193 /* Skip forward over end of first line. */
1200 /* Scan '\n' offsets in remaining chunk of data. */
1202 if (buf[i] != '\n') {
1207 if (noffsets < *nlines) {
1208 off_t *o = recallocarray(*line_offsets,
1210 sizeof(**line_offsets));
1212 free(*line_offsets);
1213 *line_offsets = NULL;
1214 return got_error_from_errno(
1220 off = total_len + i - hdrlen + 1;
1221 (*line_offsets)[*nlines - 1] = off;
1225 /* Skip blob object header first time around. */
1226 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1227 if (n != len - hdrlen)
1228 return got_ferror(outfile, GOT_ERR_IO);
1229 total_len += len - hdrlen;
1233 if (fflush(outfile) != 0)
1234 return got_error_from_errno("fflush");
1238 *filesize = total_len;
1243 static const struct got_error *
1244 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1245 int pack_idx, struct got_object_id *id)
1247 const struct got_error *err = NULL;
1249 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1254 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1257 static const struct got_error *
1258 read_packed_tag_privsep(struct got_tag_object **tag,
1259 struct got_pack *pack, struct got_packidx *packidx, int idx,
1260 struct got_object_id *id)
1262 const struct got_error *err = NULL;
1264 if (pack->privsep_child)
1265 return request_packed_tag(tag, pack, idx, id);
1267 err = start_pack_privsep_child(pack, packidx);
1271 return request_packed_tag(tag, pack, idx, id);
1274 static const struct got_error *
1275 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1278 const struct got_error *err = NULL;
1279 struct imsgbuf *ibuf;
1281 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1283 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1287 return got_privsep_recv_tag(tag, ibuf);
1290 static const struct got_error *
1291 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1292 struct got_repository *repo)
1294 const struct got_error *err;
1297 struct imsgbuf *ibuf;
1299 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1300 return request_tag(tag, repo, obj_fd);
1302 ibuf = calloc(1, sizeof(*ibuf));
1304 return got_error_from_errno("calloc");
1306 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1307 err = got_error_from_errno("socketpair");
1314 err = got_error_from_errno("fork");
1318 else if (pid == 0) {
1319 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1324 if (close(imsg_fds[1]) != 0) {
1325 err = got_error_from_errno("close");
1329 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1331 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1332 imsg_init(ibuf, imsg_fds[0]);
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1335 return request_tag(tag, repo, obj_fd);
1338 static const struct got_error *
1339 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1340 struct got_object_id *id, int check_cache)
1342 const struct got_error *err = NULL;
1343 struct got_packidx *packidx = NULL;
1345 char *path_packfile = NULL;
1346 struct got_object *obj = NULL;
1347 int obj_type = GOT_OBJ_TYPE_ANY;
1350 *tag = got_repo_get_cached_tag(repo, id);
1358 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1360 struct got_pack *pack = NULL;
1362 err = get_packfile_path(&path_packfile, packidx);
1366 pack = got_repo_get_cached_pack(repo, path_packfile);
1368 err = got_repo_cache_pack(&pack, repo, path_packfile,
1374 /* Beware of "leightweight" tags: Check object type first. */
1375 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1379 obj_type = obj->type;
1380 got_object_close(obj);
1381 if (obj_type != GOT_OBJ_TYPE_TAG) {
1382 err = got_error(GOT_ERR_OBJ_TYPE);
1385 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1386 } else if (err->code == GOT_ERR_NO_OBJ) {
1389 err = open_loose_object(&fd, id, repo);
1392 err = read_object_header_privsep(&obj, repo, fd);
1395 obj_type = obj->type;
1396 got_object_close(obj);
1397 if (obj_type != GOT_OBJ_TYPE_TAG)
1398 return got_error(GOT_ERR_OBJ_TYPE);
1400 err = open_loose_object(&fd, id, repo);
1403 err = read_tag_privsep(tag, fd, repo);
1408 err = got_repo_cache_tag(repo, id, *tag);
1411 free(path_packfile);
1415 const struct got_error *
1416 got_object_open_as_tag(struct got_tag_object **tag,
1417 struct got_repository *repo, struct got_object_id *id)
1419 *tag = got_repo_get_cached_tag(repo, id);
1425 return open_tag(tag, repo, id, 0);
1428 const struct got_error *
1429 got_object_tag_open(struct got_tag_object **tag,
1430 struct got_repository *repo, struct got_object *obj)
1432 return open_tag(tag, repo, got_object_get_id(obj), 1);
1436 got_object_tag_get_name(struct got_tag_object *tag)
1442 got_object_tag_get_object_type(struct got_tag_object *tag)
1444 return tag->obj_type;
1447 struct got_object_id *
1448 got_object_tag_get_object_id(struct got_tag_object *tag)
1454 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1456 return tag->tagger_time;
1460 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1462 return tag->tagger_gmtoff;
1466 got_object_tag_get_tagger(struct got_tag_object *tag)
1472 got_object_tag_get_message(struct got_tag_object *tag)
1477 static struct got_tree_entry *
1478 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1480 struct got_tree_entry *te;
1482 /* Note that tree entries are sorted in strncmp() order. */
1483 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1484 int cmp = strncmp(te->name, name, len);
1489 if (te->name[len] == '\0')
1495 const struct got_tree_entry *
1496 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1498 return find_entry_by_name(tree, name, strlen(name));
1501 const struct got_error *
1502 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1503 struct got_object_id *commit_id, const char *path)
1505 const struct got_error *err = NULL;
1506 struct got_commit_object *commit = NULL;
1507 struct got_tree_object *tree = NULL;
1508 struct got_tree_entry *te = NULL;
1509 const char *seg, *s;
1514 err = got_object_open_as_commit(&commit, repo, commit_id);
1518 /* Handle opening of root of commit's tree. */
1519 if (got_path_is_root_dir(path)) {
1520 *id = got_object_id_dup(commit->tree_id);
1522 err = got_error_from_errno("got_object_id_dup");
1526 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1536 struct got_tree_object *next_tree;
1545 te = find_entry_by_name(tree, seg, seglen);
1547 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1558 err = got_object_open_as_tree(&next_tree, repo,
1563 got_object_tree_close(tree);
1569 *id = got_object_id_dup(te->id);
1571 return got_error_from_errno("got_object_id_dup");
1573 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1576 got_object_commit_close(commit);
1578 got_object_tree_close(tree);
1582 const struct got_error *
1583 got_object_tree_path_changed(int *changed,
1584 struct got_tree_object *tree01, struct got_tree_object *tree02,
1585 const char *path, struct got_repository *repo)
1587 const struct got_error *err = NULL;
1588 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1589 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1590 const char *seg, *s;
1595 /* We are expecting an absolute in-repository path. */
1597 return got_error(GOT_ERR_NOT_ABSPATH);
1599 /* We not do support comparing the root path. */
1600 if (path[1] == '\0')
1601 return got_error(GOT_ERR_BAD_PATH);
1606 s++; /* skip leading '/' */
1610 struct got_tree_object *next_tree1, *next_tree2;
1619 te1 = find_entry_by_name(tree1, seg, seglen);
1621 err = got_error(GOT_ERR_NO_OBJ);
1625 te2 = find_entry_by_name(tree2, seg, seglen);
1631 if (te1->mode != te2->mode) {
1636 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1641 if (*s == '\0') { /* final path element */
1650 err = got_object_open_as_tree(&next_tree1, repo,
1655 if (tree1 != tree01)
1656 got_object_tree_close(tree1);
1659 err = got_object_open_as_tree(&next_tree2, repo,
1664 if (tree2 != tree02)
1665 got_object_tree_close(tree2);
1670 if (tree1 && tree1 != tree01)
1671 got_object_tree_close(tree1);
1672 if (tree2 && tree2 != tree02)
1673 got_object_tree_close(tree2);
1677 const struct got_error *
1678 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1679 struct got_tree_entry *te)
1681 const struct got_error *err = NULL;
1683 *new_te = calloc(1, sizeof(**new_te));
1684 if (*new_te == NULL)
1685 return got_error_from_errno("calloc");
1687 (*new_te)->mode = te->mode;
1688 (*new_te)->name = strdup(te->name);
1689 if ((*new_te)->name == NULL) {
1690 err = got_error_from_errno("strdup");
1694 (*new_te)->id = got_object_id_dup(te->id);
1695 if ((*new_te)->id == NULL) {
1696 err = got_error_from_errno("got_object_id_dup");
1701 got_object_tree_entry_close(*new_te);
1708 got_object_tree_entry_is_submodule(const struct got_tree_entry *te)
1710 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);