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>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_object_idcache.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_repository.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
67 memcpy(id2, id1, sizeof(*id2));
71 struct got_object_id *
72 got_object_get_id(struct got_object *obj)
77 const struct got_error *
78 got_object_get_id_str(char **outbuf, struct got_object *obj)
80 return got_object_id_str(outbuf, &obj->id);
83 const struct got_error *
84 got_object_get_type(int *type, struct got_repository *repo,
85 struct got_object_id *id)
87 const struct got_error *err = NULL;
88 struct got_object *obj;
90 err = got_object_open(&obj, repo, id);
95 case GOT_OBJ_TYPE_COMMIT:
96 case GOT_OBJ_TYPE_TREE:
97 case GOT_OBJ_TYPE_BLOB:
98 case GOT_OBJ_TYPE_TAG:
102 err = got_error(GOT_ERR_OBJ_TYPE);
106 got_object_close(obj);
110 const struct got_error *
111 got_object_get_path(char **path, struct got_object_id *id,
112 struct got_repository *repo)
114 const struct got_error *err = NULL;
116 char *path_objects = got_repo_get_path_objects(repo);
120 if (path_objects == NULL)
121 return got_error_from_errno("got_repo_get_path_objects");
123 err = got_object_id_str(&hex, id);
127 if (asprintf(path, "%s/%.2x/%s", path_objects,
128 id->sha1[0], hex + 2) == -1)
129 err = got_error_from_errno("asprintf");
137 static const struct got_error *
138 open_loose_object(int *fd, struct got_object_id *id,
139 struct got_repository *repo)
141 const struct got_error *err = NULL;
144 err = got_object_get_path(&path, id, repo);
147 *fd = open(path, O_RDONLY | O_NOFOLLOW);
149 err = got_error_from_errno2("open", path);
157 static const struct got_error *
158 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
162 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
163 size = strlen(packidx->path_packidx) + 2;
164 if (size < GOT_PACKFILE_NAMELEN + 1)
165 return got_error(GOT_ERR_BAD_PATH);
167 *path_packfile = malloc(size);
168 if (*path_packfile == NULL)
169 return got_error_from_errno("malloc");
171 /* Copy up to and excluding ".idx". */
172 if (strlcpy(*path_packfile, packidx->path_packidx,
173 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
174 return got_error(GOT_ERR_NO_SPACE);
176 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
177 return got_error(GOT_ERR_NO_SPACE);
183 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
185 if (close(imsg_fds[0]) != 0) {
186 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
190 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
191 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
194 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
195 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
199 if (execl(path, path, repo_path, (char *)NULL) == -1) {
200 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
206 static const struct got_error *
207 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
208 struct got_object_id *id)
210 const struct got_error *err = NULL;
211 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
213 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
217 err = got_privsep_recv_obj(obj, ibuf);
221 (*obj)->path_packfile = strdup(pack->path_packfile);
222 if ((*obj)->path_packfile == NULL) {
223 err = got_error_from_errno("strdup");
226 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
231 static const struct got_error *
232 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
234 const struct got_error *err = NULL;
237 struct imsgbuf *ibuf;
239 ibuf = calloc(1, sizeof(*ibuf));
241 return got_error_from_errno("calloc");
243 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
244 if (pack->privsep_child == NULL) {
245 err = got_error_from_errno("calloc");
250 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
251 err = got_error_from_errno("socketpair");
257 err = got_error_from_errno("fork");
259 } else if (pid == 0) {
260 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
261 pack->path_packfile);
265 if (close(imsg_fds[1]) != 0)
266 return got_error_from_errno("close");
267 pack->privsep_child->imsg_fd = imsg_fds[0];
268 pack->privsep_child->pid = pid;
269 imsg_init(ibuf, imsg_fds[0]);
270 pack->privsep_child->ibuf = ibuf;
272 err = got_privsep_init_pack_child(ibuf, pack, packidx);
274 const struct got_error *child_err;
275 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
276 child_err = got_privsep_wait_for_child(
277 pack->privsep_child->pid);
278 if (child_err && err == NULL)
284 free(pack->privsep_child);
285 pack->privsep_child = NULL;
290 static const struct got_error *
291 read_packed_object_privsep(struct got_object **obj,
292 struct got_repository *repo, struct got_pack *pack,
293 struct got_packidx *packidx, int idx, struct got_object_id *id)
295 const struct got_error *err = NULL;
297 if (pack->privsep_child)
298 return request_packed_object(obj, pack, idx, id);
300 err = start_pack_privsep_child(pack, packidx);
304 return request_packed_object(obj, pack, idx, id);
308 static const struct got_error *
309 open_packed_object(struct got_object **obj, struct got_object_id *id,
310 struct got_repository *repo)
312 const struct got_error *err = NULL;
313 struct got_pack *pack = NULL;
314 struct got_packidx *packidx = NULL;
318 err = got_repo_search_packidx(&packidx, &idx, repo, id);
322 err = get_packfile_path(&path_packfile, packidx);
326 pack = got_repo_get_cached_pack(repo, path_packfile);
328 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
333 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
337 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
343 static const struct got_error *
344 request_object(struct got_object **obj, struct got_repository *repo, int fd)
346 const struct got_error *err = NULL;
347 struct imsgbuf *ibuf;
349 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
351 err = got_privsep_send_obj_req(ibuf, fd);
355 return got_privsep_recv_obj(obj, ibuf);
358 static const struct got_error *
359 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
364 struct imsgbuf *ibuf;
366 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
367 return request_object(obj, repo, obj_fd);
369 ibuf = calloc(1, sizeof(*ibuf));
371 return got_error_from_errno("calloc");
373 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
374 return got_error_from_errno("socketpair");
378 return got_error_from_errno("fork");
380 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
385 if (close(imsg_fds[1]) != 0)
386 return got_error_from_errno("close");
387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
389 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
390 imsg_init(ibuf, imsg_fds[0]);
391 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
393 return request_object(obj, repo, obj_fd);
397 const struct got_error *
398 got_object_open(struct got_object **obj, struct got_repository *repo,
399 struct got_object_id *id)
401 const struct got_error *err = NULL;
405 *obj = got_repo_get_cached_object(repo, id);
411 err = open_packed_object(obj, id, repo);
412 if (err && err->code != GOT_ERR_NO_OBJ)
416 return got_repo_cache_object(repo, id, *obj);
419 err = got_object_get_path(&path, id, repo);
423 fd = open(path, O_RDONLY | O_NOFOLLOW);
426 err = got_error_no_obj(id);
428 err = got_error_from_errno2("open", path);
431 err = read_object_header_privsep(obj, repo, fd);
434 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
438 err = got_repo_cache_object(repo, id, *obj);
445 const struct got_error *
446 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
449 struct got_object_id id;
451 if (!got_parse_sha1_digest(id.sha1, id_str))
452 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
454 return got_object_open(obj, repo, &id);
457 const struct got_error *
458 got_object_resolve_id_str(struct got_object_id **id,
459 struct got_repository *repo, const char *id_str)
461 const struct got_error *err = NULL;
462 struct got_object *obj;
464 err = got_object_open_by_id_str(&obj, repo, id_str);
468 *id = got_object_id_dup(got_object_get_id(obj));
469 got_object_close(obj);
471 return got_error_from_errno("got_object_id_dup");
476 static const struct got_error *
477 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
478 int pack_idx, struct got_object_id *id)
480 const struct got_error *err = NULL;
482 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
487 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
490 static const struct got_error *
491 read_packed_commit_privsep(struct got_commit_object **commit,
492 struct got_pack *pack, struct got_packidx *packidx, int idx,
493 struct got_object_id *id)
495 const struct got_error *err = NULL;
497 if (pack->privsep_child)
498 return request_packed_commit(commit, pack, idx, id);
500 err = start_pack_privsep_child(pack, packidx);
504 return request_packed_commit(commit, pack, idx, id);
507 static const struct got_error *
508 request_commit(struct got_commit_object **commit, struct got_repository *repo,
511 const struct got_error *err = NULL;
512 struct imsgbuf *ibuf;
514 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
516 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
520 return got_privsep_recv_commit(commit, ibuf);
523 static const struct got_error *
524 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
525 struct got_repository *repo)
529 struct imsgbuf *ibuf;
531 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
532 return request_commit(commit, repo, obj_fd);
534 ibuf = calloc(1, sizeof(*ibuf));
536 return got_error_from_errno("calloc");
538 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
539 return got_error_from_errno("socketpair");
543 return got_error_from_errno("fork");
545 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
550 if (close(imsg_fds[1]) != 0)
551 return got_error_from_errno("close");
552 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
554 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
555 imsg_init(ibuf, imsg_fds[0]);
556 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
558 return request_commit(commit, repo, obj_fd);
562 static const struct got_error *
563 open_commit(struct got_commit_object **commit,
564 struct got_repository *repo, struct got_object_id *id, int check_cache)
566 const struct got_error *err = NULL;
567 struct got_packidx *packidx = NULL;
572 *commit = got_repo_get_cached_commit(repo, id);
573 if (*commit != NULL) {
580 err = got_repo_search_packidx(&packidx, &idx, repo, id);
582 struct got_pack *pack = NULL;
584 err = get_packfile_path(&path_packfile, packidx);
588 pack = got_repo_get_cached_pack(repo, path_packfile);
590 err = got_repo_cache_pack(&pack, repo, path_packfile,
595 err = read_packed_commit_privsep(commit, pack,
597 } else if (err->code == GOT_ERR_NO_OBJ) {
600 err = open_loose_object(&fd, id, repo);
603 err = read_commit_privsep(commit, fd, repo);
608 err = got_repo_cache_commit(repo, id, *commit);
614 const struct got_error *
615 got_object_open_as_commit(struct got_commit_object **commit,
616 struct got_repository *repo, struct got_object_id *id)
618 *commit = got_repo_get_cached_commit(repo, id);
619 if (*commit != NULL) {
624 return open_commit(commit, repo, id, 0);
627 const struct got_error *
628 got_object_commit_open(struct got_commit_object **commit,
629 struct got_repository *repo, struct got_object *obj)
631 return open_commit(commit, repo, got_object_get_id(obj), 1);
634 const struct got_error *
635 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
637 const struct got_error *err = NULL;
639 *qid = calloc(1, sizeof(**qid));
641 return got_error_from_errno("calloc");
643 (*qid)->id = got_object_id_dup(id);
644 if ((*qid)->id == NULL) {
645 err = got_error_from_errno("got_object_id_dup");
646 got_object_qid_free(*qid);
654 static const struct got_error *
655 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
656 int pack_idx, struct got_object_id *id)
658 const struct got_error *err = NULL;
660 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
665 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
668 static const struct got_error *
669 read_packed_tree_privsep(struct got_tree_object **tree,
670 struct got_pack *pack, struct got_packidx *packidx, int idx,
671 struct got_object_id *id)
673 const struct got_error *err = NULL;
675 if (pack->privsep_child)
676 return request_packed_tree(tree, pack, idx, id);
678 err = start_pack_privsep_child(pack, packidx);
682 return request_packed_tree(tree, pack, idx, id);
685 static const struct got_error *
686 request_tree(struct got_tree_object **tree, struct got_repository *repo,
689 const struct got_error *err = NULL;
690 struct imsgbuf *ibuf;
692 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
694 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
698 return got_privsep_recv_tree(tree, ibuf);
701 const struct got_error *
702 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
703 struct got_repository *repo)
707 struct imsgbuf *ibuf;
709 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
710 return request_tree(tree, repo, obj_fd);
712 ibuf = calloc(1, sizeof(*ibuf));
714 return got_error_from_errno("calloc");
716 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
717 return got_error_from_errno("socketpair");
721 return got_error_from_errno("fork");
723 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
728 if (close(imsg_fds[1]) != 0)
729 return got_error_from_errno("close");
730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
733 imsg_init(ibuf, imsg_fds[0]);
734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
737 return request_tree(tree, repo, obj_fd);
740 static const struct got_error *
741 open_tree(struct got_tree_object **tree, struct got_repository *repo,
742 struct got_object_id *id, int check_cache)
744 const struct got_error *err = NULL;
745 struct got_packidx *packidx = NULL;
750 *tree = got_repo_get_cached_tree(repo, id);
758 err = got_repo_search_packidx(&packidx, &idx, repo, id);
760 struct got_pack *pack = NULL;
762 err = get_packfile_path(&path_packfile, packidx);
766 pack = got_repo_get_cached_pack(repo, path_packfile);
768 err = got_repo_cache_pack(&pack, repo, path_packfile,
773 err = read_packed_tree_privsep(tree, pack,
775 } else if (err->code == GOT_ERR_NO_OBJ) {
778 err = open_loose_object(&fd, id, repo);
781 err = read_tree_privsep(tree, fd, repo);
786 err = got_repo_cache_tree(repo, id, *tree);
792 const struct got_error *
793 got_object_open_as_tree(struct got_tree_object **tree,
794 struct got_repository *repo, struct got_object_id *id)
796 *tree = got_repo_get_cached_tree(repo, id);
802 return open_tree(tree, repo, id, 0);
805 const struct got_error *
806 got_object_tree_open(struct got_tree_object **tree,
807 struct got_repository *repo, struct got_object *obj)
809 return open_tree(tree, repo, got_object_get_id(obj), 1);
812 const struct got_tree_entries *
813 got_object_tree_get_entries(struct got_tree_object *tree)
815 return &tree->entries;
818 static const struct got_error *
819 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
820 struct got_pack *pack, struct got_packidx *packidx, int idx,
821 struct got_object_id *id)
823 const struct got_error *err = NULL;
825 int basefd, accumfd; /* temporary files for delta application */
827 basefd = got_opentempfd();
829 return got_error_from_errno("got_opentempfd");
830 accumfd = got_opentempfd();
832 return got_error_from_errno("got_opentempfd");
834 outfd_child = dup(outfd);
835 if (outfd_child == -1)
836 return got_error_from_errno("dup");
838 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
842 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
850 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
857 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
862 err = got_privsep_recv_blob(outbuf, size, hdrlen,
863 pack->privsep_child->ibuf);
867 if (lseek(outfd, SEEK_SET, 0) == -1)
868 err = got_error_from_errno("lseek");
873 static const struct got_error *
874 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
875 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
876 struct got_object_id *id)
878 const struct got_error *err = NULL;
880 if (pack->privsep_child == NULL) {
881 err = start_pack_privsep_child(pack, packidx);
886 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
890 static const struct got_error *
891 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
892 int infd, struct imsgbuf *ibuf)
894 const struct got_error *err = NULL;
897 outfd_child = dup(outfd);
898 if (outfd_child == -1)
899 return got_error_from_errno("dup");
901 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
905 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
909 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
913 if (lseek(outfd, SEEK_SET, 0) == -1)
914 return got_error_from_errno("lseek");
919 static const struct got_error *
920 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
921 int outfd, int infd, struct got_repository *repo)
925 struct imsgbuf *ibuf;
927 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
928 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
929 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
932 ibuf = calloc(1, sizeof(*ibuf));
934 return got_error_from_errno("calloc");
936 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
937 return got_error_from_errno("socketpair");
941 return got_error_from_errno("fork");
943 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
948 if (close(imsg_fds[1]) != 0)
949 return got_error_from_errno("close");
950 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
952 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
953 imsg_init(ibuf, imsg_fds[0]);
954 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
956 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
959 static const struct got_error *
960 open_blob(struct got_blob_object **blob, struct got_repository *repo,
961 struct got_object_id *id, size_t blocksize)
963 const struct got_error *err = NULL;
964 struct got_packidx *packidx = NULL;
972 *blob = calloc(1, sizeof(**blob));
974 return got_error_from_errno("calloc");
976 outfd = got_opentempfd();
978 return got_error_from_errno("got_opentempfd");
980 (*blob)->read_buf = malloc(blocksize);
981 if ((*blob)->read_buf == NULL) {
982 err = got_error_from_errno("malloc");
986 err = got_repo_search_packidx(&packidx, &idx, repo, id);
988 struct got_pack *pack = NULL;
990 err = get_packfile_path(&path_packfile, packidx);
994 pack = got_repo_get_cached_pack(repo, path_packfile);
996 err = got_repo_cache_pack(&pack, repo, path_packfile,
1001 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1002 pack, packidx, idx, id);
1003 } else if (err->code == GOT_ERR_NO_OBJ) {
1006 err = open_loose_object(&infd, id, repo);
1009 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1015 if (hdrlen > size) {
1016 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1021 if (close(outfd) != 0 && err == NULL)
1022 err = got_error_from_errno("close");
1024 (*blob)->f = fmemopen(outbuf, size, "rb");
1025 if ((*blob)->f == NULL) {
1026 err = got_error_from_errno("fmemopen");
1030 (*blob)->data = outbuf;
1032 if (fstat(outfd, &sb) == -1) {
1033 err = got_error_from_errno("fstat");
1037 if (sb.st_size != size) {
1038 err = got_error(GOT_ERR_PRIVSEP_LEN);
1042 (*blob)->f = fdopen(outfd, "rb");
1043 if ((*blob)->f == NULL) {
1044 err = got_error_from_errno("fdopen");
1051 (*blob)->hdrlen = hdrlen;
1052 (*blob)->blocksize = blocksize;
1053 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1058 got_object_blob_close(*blob);
1060 } else if (outfd != -1)
1066 const struct got_error *
1067 got_object_open_as_blob(struct got_blob_object **blob,
1068 struct got_repository *repo, struct got_object_id *id,
1071 return open_blob(blob, repo, id, blocksize);
1074 const struct got_error *
1075 got_object_blob_open(struct got_blob_object **blob,
1076 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1078 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1081 const struct got_error *
1082 got_object_blob_close(struct got_blob_object *blob)
1084 const struct got_error *err = NULL;
1085 free(blob->read_buf);
1086 if (blob->f && fclose(blob->f) != 0)
1087 err = got_error_from_errno("fclose");
1094 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1096 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1100 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1102 return blob->hdrlen;
1106 got_object_blob_get_read_buf(struct got_blob_object *blob)
1108 return blob->read_buf;
1111 const struct got_error *
1112 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1116 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1117 if (n == 0 && ferror(blob->f))
1118 return got_ferror(blob->f, GOT_ERR_IO);
1123 const struct got_error *
1124 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1125 FILE *outfile, struct got_blob_object *blob)
1127 const struct got_error *err = NULL;
1128 size_t n, len, hdrlen;
1137 hdrlen = got_object_blob_get_hdrlen(blob);
1139 err = got_object_blob_read_block(&len, blob);
1146 buf = got_object_blob_get_read_buf(blob);
1148 for (i = 0; i < len; i++) {
1153 /* Skip blob object header first time around. */
1154 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1155 if (n != len - hdrlen)
1156 return got_ferror(outfile, GOT_ERR_IO);
1160 if (fflush(outfile) != 0)
1161 return got_error_from_errno("fflush");
1167 static const struct got_error *
1168 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1169 int pack_idx, struct got_object_id *id)
1171 const struct got_error *err = NULL;
1173 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1178 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1181 static const struct got_error *
1182 read_packed_tag_privsep(struct got_tag_object **tag,
1183 struct got_pack *pack, struct got_packidx *packidx, int idx,
1184 struct got_object_id *id)
1186 const struct got_error *err = NULL;
1188 if (pack->privsep_child)
1189 return request_packed_tag(tag, pack, idx, id);
1191 err = start_pack_privsep_child(pack, packidx);
1195 return request_packed_tag(tag, pack, idx, id);
1198 static const struct got_error *
1199 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1202 const struct got_error *err = NULL;
1203 struct imsgbuf *ibuf;
1205 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1207 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1211 return got_privsep_recv_tag(tag, ibuf);
1214 static const struct got_error *
1215 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1216 struct got_repository *repo)
1220 struct imsgbuf *ibuf;
1222 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1223 return request_tag(tag, repo, obj_fd);
1225 ibuf = calloc(1, sizeof(*ibuf));
1227 return got_error_from_errno("calloc");
1229 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1230 return got_error_from_errno("socketpair");
1234 return got_error_from_errno("fork");
1235 else if (pid == 0) {
1236 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1241 if (close(imsg_fds[1]) != 0)
1242 return got_error_from_errno("close");
1243 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1245 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1246 imsg_init(ibuf, imsg_fds[0]);
1247 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1249 return request_tag(tag, repo, obj_fd);
1252 static const struct got_error *
1253 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1254 struct got_object_id *id, int check_cache)
1256 const struct got_error *err = NULL;
1257 struct got_packidx *packidx = NULL;
1259 char *path_packfile;
1262 *tag = got_repo_get_cached_tag(repo, id);
1270 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1272 struct got_pack *pack = NULL;
1274 err = get_packfile_path(&path_packfile, packidx);
1278 pack = got_repo_get_cached_pack(repo, path_packfile);
1280 err = got_repo_cache_pack(&pack, repo, path_packfile,
1285 err = read_packed_tag_privsep(tag, pack,
1287 } else if (err->code == GOT_ERR_NO_OBJ) {
1290 err = open_loose_object(&fd, id, repo);
1293 err = read_tag_privsep(tag, fd, repo);
1298 err = got_repo_cache_tag(repo, id, *tag);
1304 const struct got_error *
1305 got_object_open_as_tag(struct got_tag_object **tag,
1306 struct got_repository *repo, struct got_object_id *id)
1308 *tag = got_repo_get_cached_tag(repo, id);
1314 return open_tag(tag, repo, id, 0);
1317 const struct got_error *
1318 got_object_tag_open(struct got_tag_object **tag,
1319 struct got_repository *repo, struct got_object *obj)
1321 return open_tag(tag, repo, got_object_get_id(obj), 1);
1325 got_object_tag_get_object_type(struct got_tag_object *tag)
1327 return tag->obj_type;
1330 struct got_object_id *
1331 got_object_tag_get_object_id(struct got_tag_object *tag)
1336 static struct got_tree_entry *
1337 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1339 struct got_tree_entry *te;
1341 /* Note that tree entries are sorted in strncmp() order. */
1342 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1343 int cmp = strncmp(te->name, name, len);
1348 if (te->name[len] == '\0')
1354 const struct got_tree_entry *
1355 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1357 return find_entry_by_name(tree, name, strlen(name));
1360 const struct got_error *
1361 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1362 struct got_object_id *commit_id, const char *path)
1364 const struct got_error *err = NULL;
1365 struct got_commit_object *commit = NULL;
1366 struct got_tree_object *tree = NULL;
1367 struct got_tree_entry *te = NULL;
1368 const char *seg, *s;
1373 /* We are expecting an absolute in-repository path. */
1375 return got_error(GOT_ERR_NOT_ABSPATH);
1377 err = got_object_open_as_commit(&commit, repo, commit_id);
1381 /* Handle opening of root of commit's tree. */
1382 if (path[1] == '\0') {
1383 *id = got_object_id_dup(commit->tree_id);
1385 err = got_error_from_errno("got_object_id_dup");
1389 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1394 s++; /* skip leading '/' */
1398 struct got_tree_object *next_tree;
1407 te = find_entry_by_name(tree, seg, seglen);
1409 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1420 err = got_object_open_as_tree(&next_tree, repo,
1425 got_object_tree_close(tree);
1431 *id = got_object_id_dup(te->id);
1433 return got_error_from_errno("got_object_id_dup");
1435 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1438 got_object_commit_close(commit);
1440 got_object_tree_close(tree);
1444 const struct got_error *
1445 got_object_tree_path_changed(int *changed,
1446 struct got_tree_object *tree01, struct got_tree_object *tree02,
1447 const char *path, struct got_repository *repo)
1449 const struct got_error *err = NULL;
1450 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1451 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1452 const char *seg, *s;
1457 /* We are expecting an absolute in-repository path. */
1459 return got_error(GOT_ERR_NOT_ABSPATH);
1461 /* We not do support comparing the root path. */
1462 if (path[1] == '\0')
1463 return got_error(GOT_ERR_BAD_PATH);
1468 s++; /* skip leading '/' */
1472 struct got_tree_object *next_tree1, *next_tree2;
1481 te1 = find_entry_by_name(tree1, seg, seglen);
1483 err = got_error(GOT_ERR_NO_OBJ);
1487 te2 = find_entry_by_name(tree2, seg, seglen);
1493 if (te1->mode != te2->mode) {
1498 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1503 if (*s == '\0') { /* final path element */
1512 err = got_object_open_as_tree(&next_tree1, repo,
1517 if (tree1 != tree01)
1518 got_object_tree_close(tree1);
1521 err = got_object_open_as_tree(&next_tree2, repo,
1526 if (tree2 != tree02)
1527 got_object_tree_close(tree2);
1532 if (tree1 && tree1 != tree01)
1533 got_object_tree_close(tree1);
1534 if (tree2 && tree2 != tree02)
1535 got_object_tree_close(tree2);
1539 const struct got_error *
1540 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1541 struct got_tree_entry *te)
1543 const struct got_error *err = NULL;
1545 *new_te = calloc(1, sizeof(**new_te));
1546 if (*new_te == NULL)
1547 return got_error_from_errno("calloc");
1549 (*new_te)->mode = te->mode;
1550 (*new_te)->name = strdup(te->name);
1551 if ((*new_te)->name == NULL) {
1552 err = got_error_from_errno("strdup");
1556 (*new_te)->id = got_object_id_dup(te->id);
1557 if ((*new_te)->id == NULL) {
1558 err = got_error_from_errno("got_object_id_dup");
1563 got_object_tree_entry_close(*new_te);