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"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_path.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 static const struct got_error *
111 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
113 const struct got_error *err = NULL;
115 char *path_objects = got_repo_get_path_objects(repo);
119 if (path_objects == NULL)
120 return got_error_from_errno();
122 err = got_object_id_str(&hex, id);
126 if (asprintf(path, "%s/%.2x/%s", path_objects,
127 id->sha1[0], hex + 2) == -1)
128 err = got_error_from_errno();
136 static const struct got_error *
137 open_loose_object(int *fd, struct got_object_id *id,
138 struct got_repository *repo)
140 const struct got_error *err = NULL;
143 err = object_path(&path, id, repo);
146 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
148 err = got_error_from_errno();
156 static const struct got_error *
157 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
161 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
162 size = strlen(packidx->path_packidx) + 2;
163 if (size < GOT_PACKFILE_NAMELEN + 1)
164 return got_error(GOT_ERR_BAD_PATH);
166 *path_packfile = malloc(size);
167 if (*path_packfile == NULL)
168 return got_error_from_errno();
170 /* Copy up to and excluding ".idx". */
171 if (strlcpy(*path_packfile, packidx->path_packidx,
172 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
173 return got_error(GOT_ERR_NO_SPACE);
175 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
182 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
184 if (close(imsg_fds[0]) != 0) {
185 fprintf(stderr, "%s: %s\n", getprogname(),
190 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
191 fprintf(stderr, "%s: %s\n", getprogname(),
195 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
196 fprintf(stderr, "%s: %s\n", getprogname(),
201 if (execl(path, path, repo_path, (char *)NULL) == -1) {
202 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
208 static const struct got_error *
209 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
210 struct got_object_id *id)
212 const struct got_error *err = NULL;
213 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
215 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
219 err = got_privsep_recv_obj(obj, ibuf);
223 (*obj)->path_packfile = strdup(pack->path_packfile);
224 if ((*obj)->path_packfile == NULL) {
225 err = got_error_from_errno();
228 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
233 static const struct got_error *
234 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
236 const struct got_error *err = NULL;
239 struct imsgbuf *ibuf;
241 ibuf = calloc(1, sizeof(*ibuf));
243 return got_error_from_errno();
245 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
246 if (pack->privsep_child == NULL) {
247 err = got_error_from_errno();
252 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
253 err = got_error_from_errno();
259 err = got_error_from_errno();
261 } else if (pid == 0) {
262 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
263 pack->path_packfile);
267 if (close(imsg_fds[1]) != 0)
268 return got_error_from_errno();
269 pack->privsep_child->imsg_fd = imsg_fds[0];
270 pack->privsep_child->pid = pid;
271 imsg_init(ibuf, imsg_fds[0]);
272 pack->privsep_child->ibuf = ibuf;
274 err = got_privsep_init_pack_child(ibuf, pack, packidx);
276 const struct got_error *child_err;
277 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
278 child_err = got_privsep_wait_for_child(
279 pack->privsep_child->pid);
280 if (child_err && err == NULL)
286 free(pack->privsep_child);
287 pack->privsep_child = NULL;
292 static const struct got_error *
293 read_packed_object_privsep(struct got_object **obj,
294 struct got_repository *repo, struct got_pack *pack,
295 struct got_packidx *packidx, int idx, struct got_object_id *id)
297 const struct got_error *err = NULL;
299 if (pack->privsep_child)
300 return request_packed_object(obj, pack, idx, id);
302 err = start_pack_privsep_child(pack, packidx);
306 return request_packed_object(obj, pack, idx, id);
310 static const struct got_error *
311 open_packed_object(struct got_object **obj, struct got_object_id *id,
312 struct got_repository *repo)
314 const struct got_error *err = NULL;
315 struct got_pack *pack = NULL;
316 struct got_packidx *packidx = NULL;
320 err = got_repo_search_packidx(&packidx, &idx, repo, id);
324 err = get_packfile_path(&path_packfile, packidx);
328 pack = got_repo_get_cached_pack(repo, path_packfile);
330 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
335 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
339 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
345 static const struct got_error *
346 request_object(struct got_object **obj, struct got_repository *repo, int fd)
348 const struct got_error *err = NULL;
349 struct imsgbuf *ibuf;
351 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
353 err = got_privsep_send_obj_req(ibuf, fd);
357 return got_privsep_recv_obj(obj, ibuf);
360 static const struct got_error *
361 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
366 struct imsgbuf *ibuf;
368 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
369 return request_object(obj, repo, obj_fd);
371 ibuf = calloc(1, sizeof(*ibuf));
373 return got_error_from_errno();
375 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
376 return got_error_from_errno();
380 return got_error_from_errno();
382 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
387 if (close(imsg_fds[1]) != 0)
388 return got_error_from_errno();
389 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
391 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
392 imsg_init(ibuf, imsg_fds[0]);
393 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
395 return request_object(obj, repo, obj_fd);
399 const struct got_error *
400 got_object_open(struct got_object **obj, struct got_repository *repo,
401 struct got_object_id *id)
403 const struct got_error *err = NULL;
407 *obj = got_repo_get_cached_object(repo, id);
413 err = open_packed_object(obj, id, repo);
414 if (err && err->code != GOT_ERR_NO_OBJ)
418 return got_repo_cache_object(repo, id, *obj);
421 err = object_path(&path, id, repo);
425 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
428 err = got_error_no_obj(id);
430 err = got_error_from_errno();
433 err = read_object_header_privsep(obj, repo, fd);
436 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
440 err = got_repo_cache_object(repo, id, *obj);
443 if (fd != -1 && close(fd) != 0 && errno != EBADF && err == NULL)
444 err = got_error_from_errno();
449 const struct got_error *
450 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
453 struct got_object_id id;
455 if (!got_parse_sha1_digest(id.sha1, id_str))
456 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
458 return got_object_open(obj, repo, &id);
461 const struct got_error *
462 got_object_resolve_id_str(struct got_object_id **id,
463 struct got_repository *repo, const char *id_str)
465 const struct got_error *err = NULL;
466 struct got_object *obj;
468 err = got_object_open_by_id_str(&obj, repo, id_str);
472 *id = got_object_id_dup(got_object_get_id(obj));
473 got_object_close(obj);
475 return got_error_from_errno();
480 static const struct got_error *
481 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
482 int pack_idx, struct got_object_id *id)
484 const struct got_error *err = NULL;
486 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
491 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
494 static const struct got_error *
495 read_packed_commit_privsep(struct got_commit_object **commit,
496 struct got_pack *pack, struct got_packidx *packidx, int idx,
497 struct got_object_id *id)
499 const struct got_error *err = NULL;
501 if (pack->privsep_child)
502 return request_packed_commit(commit, pack, idx, id);
504 err = start_pack_privsep_child(pack, packidx);
508 return request_packed_commit(commit, pack, idx, id);
511 static const struct got_error *
512 request_commit(struct got_commit_object **commit, struct got_repository *repo,
515 const struct got_error *err = NULL;
516 struct imsgbuf *ibuf;
518 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
520 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
524 return got_privsep_recv_commit(commit, ibuf);
527 static const struct got_error *
528 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
529 struct got_repository *repo)
533 struct imsgbuf *ibuf;
535 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
536 return request_commit(commit, repo, obj_fd);
538 ibuf = calloc(1, sizeof(*ibuf));
540 return got_error_from_errno();
542 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
543 return got_error_from_errno();
547 return got_error_from_errno();
549 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
554 if (close(imsg_fds[1]) != 0)
555 return got_error_from_errno();
556 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
558 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
559 imsg_init(ibuf, imsg_fds[0]);
560 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
562 return request_commit(commit, repo, obj_fd);
566 static const struct got_error *
567 open_commit(struct got_commit_object **commit,
568 struct got_repository *repo, struct got_object_id *id, int check_cache)
570 const struct got_error *err = NULL;
571 struct got_packidx *packidx = NULL;
576 *commit = got_repo_get_cached_commit(repo, id);
577 if (*commit != NULL) {
584 err = got_repo_search_packidx(&packidx, &idx, repo, id);
586 struct got_pack *pack = NULL;
588 err = get_packfile_path(&path_packfile, packidx);
592 pack = got_repo_get_cached_pack(repo, path_packfile);
594 err = got_repo_cache_pack(&pack, repo, path_packfile,
599 err = read_packed_commit_privsep(commit, pack,
601 } else if (err->code == GOT_ERR_NO_OBJ) {
604 err = open_loose_object(&fd, id, repo);
607 err = read_commit_privsep(commit, fd, repo);
608 if (close(fd) != 0 && errno != EBADF && err == NULL)
609 err = got_error_from_errno();
614 err = got_repo_cache_commit(repo, id, *commit);
620 const struct got_error *
621 got_object_open_as_commit(struct got_commit_object **commit,
622 struct got_repository *repo, struct got_object_id *id)
624 *commit = got_repo_get_cached_commit(repo, id);
625 if (*commit != NULL) {
630 return open_commit(commit, repo, id, 0);
633 const struct got_error *
634 got_object_commit_open(struct got_commit_object **commit,
635 struct got_repository *repo, struct got_object *obj)
637 return open_commit(commit, repo, got_object_get_id(obj), 1);
640 const struct got_error *
641 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
643 const struct got_error *err = NULL;
645 *qid = calloc(1, sizeof(**qid));
647 return got_error_from_errno();
649 (*qid)->id = got_object_id_dup(id);
650 if ((*qid)->id == NULL) {
651 err = got_error_from_errno();
652 got_object_qid_free(*qid);
660 static const struct got_error *
661 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
662 int pack_idx, struct got_object_id *id)
664 const struct got_error *err = NULL;
666 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
671 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
674 static const struct got_error *
675 read_packed_tree_privsep(struct got_tree_object **tree,
676 struct got_pack *pack, struct got_packidx *packidx, int idx,
677 struct got_object_id *id)
679 const struct got_error *err = NULL;
681 if (pack->privsep_child)
682 return request_packed_tree(tree, pack, idx, id);
684 err = start_pack_privsep_child(pack, packidx);
688 return request_packed_tree(tree, pack, idx, id);
691 static const struct got_error *
692 request_tree(struct got_tree_object **tree, struct got_repository *repo,
695 const struct got_error *err = NULL;
696 struct imsgbuf *ibuf;
698 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
700 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
704 return got_privsep_recv_tree(tree, ibuf);
707 const struct got_error *
708 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
709 struct got_repository *repo)
713 struct imsgbuf *ibuf;
715 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
716 return request_tree(tree, repo, obj_fd);
718 ibuf = calloc(1, sizeof(*ibuf));
720 return got_error_from_errno();
722 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
723 return got_error_from_errno();
727 return got_error_from_errno();
729 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
734 if (close(imsg_fds[1]) != 0)
735 return got_error_from_errno();
736 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
739 imsg_init(ibuf, imsg_fds[0]);
740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
743 return request_tree(tree, repo, obj_fd);
746 static const struct got_error *
747 open_tree(struct got_tree_object **tree, struct got_repository *repo,
748 struct got_object_id *id, int check_cache)
750 const struct got_error *err = NULL;
751 struct got_packidx *packidx = NULL;
756 *tree = got_repo_get_cached_tree(repo, id);
764 err = got_repo_search_packidx(&packidx, &idx, repo, id);
766 struct got_pack *pack = NULL;
768 err = get_packfile_path(&path_packfile, packidx);
772 pack = got_repo_get_cached_pack(repo, path_packfile);
774 err = got_repo_cache_pack(&pack, repo, path_packfile,
779 err = read_packed_tree_privsep(tree, pack,
781 } else if (err->code == GOT_ERR_NO_OBJ) {
784 err = open_loose_object(&fd, id, repo);
787 err = read_tree_privsep(tree, fd, repo);
788 if (close(fd) != 0 && errno != EBADF && err == NULL)
789 err = got_error_from_errno();
794 err = got_repo_cache_tree(repo, id, *tree);
800 const struct got_error *
801 got_object_open_as_tree(struct got_tree_object **tree,
802 struct got_repository *repo, struct got_object_id *id)
804 *tree = got_repo_get_cached_tree(repo, id);
810 return open_tree(tree, repo, id, 0);
813 const struct got_error *
814 got_object_tree_open(struct got_tree_object **tree,
815 struct got_repository *repo, struct got_object *obj)
817 return open_tree(tree, repo, got_object_get_id(obj), 1);
820 const struct got_tree_entries *
821 got_object_tree_get_entries(struct got_tree_object *tree)
823 return &tree->entries;
826 static const struct got_error *
827 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
828 struct got_pack *pack, struct got_packidx *packidx, int idx,
829 struct got_object_id *id)
831 const struct got_error *err = NULL;
833 int basefd, accumfd; /* temporary files for delta application */
835 basefd = got_opentempfd();
837 return got_error_from_errno();
838 accumfd = got_opentempfd();
840 return got_error_from_errno();
842 outfd_child = dup(outfd);
843 if (outfd_child == -1)
844 return got_error_from_errno();
846 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
850 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
856 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
865 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
873 err = got_privsep_recv_blob(outbuf, size, hdrlen,
874 pack->privsep_child->ibuf);
878 if (lseek(outfd, SEEK_SET, 0) == -1)
879 err = got_error_from_errno();
884 static const struct got_error *
885 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
886 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
887 struct got_object_id *id)
889 const struct got_error *err = NULL;
891 if (pack->privsep_child == NULL) {
892 err = start_pack_privsep_child(pack, packidx);
897 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
901 static const struct got_error *
902 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
903 int infd, struct imsgbuf *ibuf)
905 const struct got_error *err = NULL;
908 outfd_child = dup(outfd);
909 if (outfd_child == -1)
910 return got_error_from_errno();
912 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
916 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
922 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
926 if (lseek(outfd, SEEK_SET, 0) == -1)
927 return got_error_from_errno();
932 static const struct got_error *
933 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
934 int outfd, int infd, struct got_repository *repo)
938 struct imsgbuf *ibuf;
940 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
941 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
942 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
945 ibuf = calloc(1, sizeof(*ibuf));
947 return got_error_from_errno();
949 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
950 return got_error_from_errno();
954 return got_error_from_errno();
956 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
961 if (close(imsg_fds[1]) != 0)
962 return got_error_from_errno();
963 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
965 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
966 imsg_init(ibuf, imsg_fds[0]);
967 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
969 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
972 static const struct got_error *
973 open_blob(struct got_blob_object **blob, struct got_repository *repo,
974 struct got_object_id *id, size_t blocksize)
976 const struct got_error *err = NULL;
977 struct got_packidx *packidx = NULL;
985 *blob = calloc(1, sizeof(**blob));
987 return got_error_from_errno();
989 outfd = got_opentempfd();
991 return got_error_from_errno();
993 (*blob)->read_buf = malloc(blocksize);
994 if ((*blob)->read_buf == NULL) {
995 err = got_error_from_errno();
999 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1001 struct got_pack *pack = NULL;
1003 err = get_packfile_path(&path_packfile, packidx);
1007 pack = got_repo_get_cached_pack(repo, path_packfile);
1009 err = got_repo_cache_pack(&pack, repo, path_packfile,
1014 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1015 pack, packidx, idx, id);
1016 } else if (err->code == GOT_ERR_NO_OBJ) {
1019 err = open_loose_object(&infd, id, repo);
1022 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1024 if (close(infd) != 0 && errno != EBADF && err == NULL)
1025 err = got_error_from_errno();
1030 if (hdrlen > size) {
1031 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1036 if (close(outfd) != 0 && err == NULL)
1037 err = got_error_from_errno();
1039 (*blob)->f = fmemopen(outbuf, size, "rb");
1040 if ((*blob)->f == NULL) {
1041 err = got_error_from_errno();
1045 (*blob)->data = outbuf;
1047 if (fstat(outfd, &sb) == -1) {
1048 err = got_error_from_errno();
1052 if (sb.st_size != size) {
1053 err = got_error(GOT_ERR_PRIVSEP_LEN);
1057 (*blob)->f = fdopen(outfd, "rb");
1058 if ((*blob)->f == NULL) {
1059 err = got_error_from_errno();
1066 (*blob)->hdrlen = hdrlen;
1067 (*blob)->blocksize = blocksize;
1068 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1075 free((*blob)->read_buf);
1076 free((*blob)->data);
1079 } else if (outfd != -1)
1085 const struct got_error *
1086 got_object_open_as_blob(struct got_blob_object **blob,
1087 struct got_repository *repo, struct got_object_id *id,
1090 return open_blob(blob, repo, id, blocksize);
1093 const struct got_error *
1094 got_object_blob_open(struct got_blob_object **blob,
1095 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1097 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1100 const struct got_error *
1101 got_object_blob_close(struct got_blob_object *blob)
1103 const struct got_error *err = NULL;
1104 free(blob->read_buf);
1105 if (fclose(blob->f) != 0)
1106 err = got_error_from_errno();
1113 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1115 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1119 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1121 return blob->hdrlen;
1125 got_object_blob_get_read_buf(struct got_blob_object *blob)
1127 return blob->read_buf;
1130 const struct got_error *
1131 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1135 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1136 if (n == 0 && ferror(blob->f))
1137 return got_ferror(blob->f, GOT_ERR_IO);
1142 const struct got_error *
1143 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1144 FILE *outfile, struct got_blob_object *blob)
1146 const struct got_error *err = NULL;
1147 size_t n, len, hdrlen;
1156 hdrlen = got_object_blob_get_hdrlen(blob);
1158 err = got_object_blob_read_block(&len, blob);
1165 buf = got_object_blob_get_read_buf(blob);
1167 for (i = 0; i < len; i++) {
1172 /* Skip blob object header first time around. */
1173 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1174 if (n != len - hdrlen)
1175 return got_ferror(outfile, GOT_ERR_IO);
1179 if (fflush(outfile) != 0)
1180 return got_error_from_errno();
1186 static const struct got_error *
1187 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1188 int pack_idx, struct got_object_id *id)
1190 const struct got_error *err = NULL;
1192 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1197 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1200 static const struct got_error *
1201 read_packed_tag_privsep(struct got_tag_object **tag,
1202 struct got_pack *pack, struct got_packidx *packidx, int idx,
1203 struct got_object_id *id)
1205 const struct got_error *err = NULL;
1207 if (pack->privsep_child)
1208 return request_packed_tag(tag, pack, idx, id);
1210 err = start_pack_privsep_child(pack, packidx);
1214 return request_packed_tag(tag, pack, idx, id);
1217 static const struct got_error *
1218 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1221 const struct got_error *err = NULL;
1222 struct imsgbuf *ibuf;
1224 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1226 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1230 return got_privsep_recv_tag(tag, ibuf);
1233 static const struct got_error *
1234 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1235 struct got_repository *repo)
1239 struct imsgbuf *ibuf;
1241 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1242 return request_tag(tag, repo, obj_fd);
1244 ibuf = calloc(1, sizeof(*ibuf));
1246 return got_error_from_errno();
1248 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1249 return got_error_from_errno();
1253 return got_error_from_errno();
1254 else if (pid == 0) {
1255 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1260 if (close(imsg_fds[1]) != 0)
1261 return got_error_from_errno();
1262 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1264 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1265 imsg_init(ibuf, imsg_fds[0]);
1266 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1268 return request_tag(tag, repo, obj_fd);
1271 static const struct got_error *
1272 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1273 struct got_object_id *id, int check_cache)
1275 const struct got_error *err = NULL;
1276 struct got_packidx *packidx = NULL;
1278 char *path_packfile;
1281 *tag = got_repo_get_cached_tag(repo, id);
1289 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1291 struct got_pack *pack = NULL;
1293 err = get_packfile_path(&path_packfile, packidx);
1297 pack = got_repo_get_cached_pack(repo, path_packfile);
1299 err = got_repo_cache_pack(&pack, repo, path_packfile,
1304 err = read_packed_tag_privsep(tag, pack,
1306 } else if (err->code == GOT_ERR_NO_OBJ) {
1309 err = open_loose_object(&fd, id, repo);
1312 err = read_tag_privsep(tag, fd, repo);
1313 if (close(fd) != 0 && errno != EBADF && err == NULL)
1314 err = got_error_from_errno();
1319 err = got_repo_cache_tag(repo, id, *tag);
1325 const struct got_error *
1326 got_object_open_as_tag(struct got_tag_object **tag,
1327 struct got_repository *repo, struct got_object_id *id)
1329 *tag = got_repo_get_cached_tag(repo, id);
1335 return open_tag(tag, repo, id, 0);
1338 const struct got_error *
1339 got_object_tag_open(struct got_tag_object **tag,
1340 struct got_repository *repo, struct got_object *obj)
1342 return open_tag(tag, repo, got_object_get_id(obj), 1);
1346 got_object_tag_get_object_type(struct got_tag_object *tag)
1348 return tag->obj_type;
1351 struct got_object_id *
1352 got_object_tag_get_object_id(struct got_tag_object *tag)
1357 static struct got_tree_entry *
1358 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1360 struct got_tree_entry *te;
1362 /* Note that tree entries are sorted in strncmp() order. */
1363 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1364 int cmp = strncmp(te->name, name, len);
1369 if (te->name[len] == '\0')
1375 const struct got_error *
1376 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1377 struct got_object_id *commit_id, const char *path)
1379 const struct got_error *err = NULL;
1380 struct got_commit_object *commit = NULL;
1381 struct got_tree_object *tree = NULL;
1382 struct got_tree_entry *te = NULL;
1383 const char *seg, *s;
1388 /* We are expecting an absolute in-repository path. */
1390 return got_error(GOT_ERR_NOT_ABSPATH);
1392 err = got_object_open_as_commit(&commit, repo, commit_id);
1396 /* Handle opening of root of commit's tree. */
1397 if (path[1] == '\0') {
1398 *id = got_object_id_dup(commit->tree_id);
1400 err = got_error_from_errno();
1404 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1409 s++; /* skip leading '/' */
1413 struct got_tree_object *next_tree;
1422 te = find_entry_by_name(tree, seg, seglen);
1424 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1435 err = got_object_open_as_tree(&next_tree, repo,
1440 got_object_tree_close(tree);
1446 *id = got_object_id_dup(te->id);
1448 return got_error_from_errno();
1450 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1453 got_object_commit_close(commit);
1455 got_object_tree_close(tree);
1459 const struct got_error *
1460 got_object_tree_path_changed(int *changed,
1461 struct got_tree_object *tree01, struct got_tree_object *tree02,
1462 const char *path, struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1466 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1467 const char *seg, *s;
1472 /* We are expecting an absolute in-repository path. */
1474 return got_error(GOT_ERR_NOT_ABSPATH);
1476 /* We not do support comparing the root path. */
1477 if (path[1] == '\0')
1478 return got_error(GOT_ERR_BAD_PATH);
1483 s++; /* skip leading '/' */
1487 struct got_tree_object *next_tree1, *next_tree2;
1496 te1 = find_entry_by_name(tree1, seg, seglen);
1498 err = got_error(GOT_ERR_NO_OBJ);
1502 te2 = find_entry_by_name(tree2, seg, seglen);
1508 if (te1->mode != te2->mode) {
1513 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1518 if (*s == '\0') { /* final path element */
1527 err = got_object_open_as_tree(&next_tree1, repo,
1532 if (tree1 != tree01)
1533 got_object_tree_close(tree1);
1536 err = got_object_open_as_tree(&next_tree2, repo,
1541 if (tree2 != tree02)
1542 got_object_tree_close(tree2);
1547 if (tree1 && tree1 != tree01)
1548 got_object_tree_close(tree1);
1549 if (tree2 && tree2 != tree02)
1550 got_object_tree_close(tree2);