2 * Copyright (c) 2018 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)
186 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
187 fprintf(stderr, "%s: %s\n", getprogname(),
191 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(),
197 if (execl(path, path, repo_path, (char *)NULL) == -1) {
198 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
204 static const struct got_error *
205 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
206 struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
215 err = got_privsep_recv_obj(obj, ibuf);
219 (*obj)->path_packfile = strdup(pack->path_packfile);
220 if ((*obj)->path_packfile == NULL) {
221 err = got_error_from_errno();
224 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
229 static const struct got_error *
230 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
232 const struct got_error *err = NULL;
235 struct imsgbuf *ibuf;
237 ibuf = calloc(1, sizeof(*ibuf));
239 return got_error_from_errno();
241 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
242 if (pack->privsep_child == NULL) {
243 err = got_error_from_errno();
248 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
249 err = got_error_from_errno();
255 err = got_error_from_errno();
257 } else if (pid == 0) {
258 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
259 pack->path_packfile);
264 pack->privsep_child->imsg_fd = imsg_fds[0];
265 pack->privsep_child->pid = pid;
266 imsg_init(ibuf, imsg_fds[0]);
267 pack->privsep_child->ibuf = ibuf;
269 err = got_privsep_init_pack_child(ibuf, pack, packidx);
271 const struct got_error *child_err;
272 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
273 child_err = got_privsep_wait_for_child(
274 pack->privsep_child->pid);
275 if (child_err && err == NULL)
281 free(pack->privsep_child);
282 pack->privsep_child = NULL;
287 static const struct got_error *
288 read_packed_object_privsep(struct got_object **obj,
289 struct got_repository *repo, struct got_pack *pack,
290 struct got_packidx *packidx, int idx, struct got_object_id *id)
292 const struct got_error *err = NULL;
294 if (pack->privsep_child)
295 return request_packed_object(obj, pack, idx, id);
297 err = start_pack_privsep_child(pack, packidx);
301 return request_packed_object(obj, pack, idx, id);
305 static const struct got_error *
306 open_packed_object(struct got_object **obj, struct got_object_id *id,
307 struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_pack *pack = NULL;
311 struct got_packidx *packidx = NULL;
315 err = got_repo_search_packidx(&packidx, &idx, repo, id);
319 err = get_packfile_path(&path_packfile, packidx);
323 pack = got_repo_get_cached_pack(repo, path_packfile);
325 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
330 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
334 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
340 static const struct got_error *
341 request_object(struct got_object **obj, struct got_repository *repo, int fd)
343 const struct got_error *err = NULL;
344 struct imsgbuf *ibuf;
346 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
348 err = got_privsep_send_obj_req(ibuf, fd);
352 return got_privsep_recv_obj(obj, ibuf);
355 static const struct got_error *
356 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
361 struct imsgbuf *ibuf;
363 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
364 return request_object(obj, repo, obj_fd);
366 ibuf = calloc(1, sizeof(*ibuf));
368 return got_error_from_errno();
370 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
371 return got_error_from_errno();
375 return got_error_from_errno();
377 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
386 imsg_init(ibuf, imsg_fds[0]);
387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
389 return request_object(obj, repo, obj_fd);
393 const struct got_error *
394 got_object_open(struct got_object **obj, struct got_repository *repo,
395 struct got_object_id *id)
397 const struct got_error *err = NULL;
401 *obj = got_repo_get_cached_object(repo, id);
407 err = open_packed_object(obj, id, repo);
408 if (err && err->code != GOT_ERR_NO_OBJ)
412 return got_repo_cache_object(repo, id, *obj);
415 err = object_path(&path, id, repo);
419 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
422 err = got_error_no_obj(id);
424 err = got_error_from_errno();
427 err = read_object_header_privsep(obj, repo, fd);
430 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
434 err = got_repo_cache_object(repo, id, *obj);
443 const struct got_error *
444 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
447 struct got_object_id id;
449 if (!got_parse_sha1_digest(id.sha1, id_str))
450 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
452 return got_object_open(obj, repo, &id);
455 const struct got_error *
456 got_object_resolve_id_str(struct got_object_id **id,
457 struct got_repository *repo, const char *id_str)
459 const struct got_error *err = NULL;
460 struct got_object *obj;
462 err = got_object_open_by_id_str(&obj, repo, id_str);
466 *id = got_object_id_dup(got_object_get_id(obj));
467 got_object_close(obj);
469 return got_error_from_errno();
474 static const struct got_error *
475 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
476 int pack_idx, struct got_object_id *id)
478 const struct got_error *err = NULL;
480 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
485 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
488 static const struct got_error *
489 read_packed_commit_privsep(struct got_commit_object **commit,
490 struct got_pack *pack, struct got_packidx *packidx, int idx,
491 struct got_object_id *id)
493 const struct got_error *err = NULL;
495 if (pack->privsep_child)
496 return request_packed_commit(commit, pack, idx, id);
498 err = start_pack_privsep_child(pack, packidx);
502 return request_packed_commit(commit, pack, idx, id);
505 static const struct got_error *
506 request_commit(struct got_commit_object **commit, struct got_repository *repo,
509 const struct got_error *err = NULL;
510 struct imsgbuf *ibuf;
512 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
514 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
518 return got_privsep_recv_commit(commit, ibuf);
521 static const struct got_error *
522 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
523 struct got_repository *repo)
527 struct imsgbuf *ibuf;
529 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
530 return request_commit(commit, repo, obj_fd);
532 ibuf = calloc(1, sizeof(*ibuf));
534 return got_error_from_errno();
536 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
537 return got_error_from_errno();
541 return got_error_from_errno();
543 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
549 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
551 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
552 imsg_init(ibuf, imsg_fds[0]);
553 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
555 return request_commit(commit, repo, obj_fd);
559 static const struct got_error *
560 open_commit(struct got_commit_object **commit,
561 struct got_repository *repo, struct got_object_id *id, int check_cache)
563 const struct got_error *err = NULL;
564 struct got_packidx *packidx = NULL;
569 *commit = got_repo_get_cached_commit(repo, id);
570 if (*commit != NULL) {
577 err = got_repo_search_packidx(&packidx, &idx, repo, id);
579 struct got_pack *pack = NULL;
581 err = get_packfile_path(&path_packfile, packidx);
585 pack = got_repo_get_cached_pack(repo, path_packfile);
587 err = got_repo_cache_pack(&pack, repo, path_packfile,
592 err = read_packed_commit_privsep(commit, pack,
594 } else if (err->code == GOT_ERR_NO_OBJ) {
597 err = open_loose_object(&fd, id, repo);
600 err = read_commit_privsep(commit, fd, repo);
606 err = got_repo_cache_commit(repo, id, *commit);
612 const struct got_error *
613 got_object_open_as_commit(struct got_commit_object **commit,
614 struct got_repository *repo, struct got_object_id *id)
616 *commit = got_repo_get_cached_commit(repo, id);
617 if (*commit != NULL) {
622 return open_commit(commit, repo, id, 0);
625 const struct got_error *
626 got_object_commit_open(struct got_commit_object **commit,
627 struct got_repository *repo, struct got_object *obj)
629 return open_commit(commit, repo, got_object_get_id(obj), 1);
632 const struct got_error *
633 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
635 const struct got_error *err = NULL;
637 *qid = calloc(1, sizeof(**qid));
639 return got_error_from_errno();
641 (*qid)->id = got_object_id_dup(id);
642 if ((*qid)->id == NULL) {
643 err = got_error_from_errno();
644 got_object_qid_free(*qid);
652 static const struct got_error *
653 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
654 int pack_idx, struct got_object_id *id)
656 const struct got_error *err = NULL;
658 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
663 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
666 static const struct got_error *
667 read_packed_tree_privsep(struct got_tree_object **tree,
668 struct got_pack *pack, struct got_packidx *packidx, int idx,
669 struct got_object_id *id)
671 const struct got_error *err = NULL;
673 if (pack->privsep_child)
674 return request_packed_tree(tree, pack, idx, id);
676 err = start_pack_privsep_child(pack, packidx);
680 return request_packed_tree(tree, pack, idx, id);
683 static const struct got_error *
684 request_tree(struct got_tree_object **tree, struct got_repository *repo,
687 const struct got_error *err = NULL;
688 struct imsgbuf *ibuf;
690 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
692 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
696 return got_privsep_recv_tree(tree, ibuf);
699 const struct got_error *
700 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
701 struct got_repository *repo)
705 struct imsgbuf *ibuf;
707 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
708 return request_tree(tree, repo, obj_fd);
710 ibuf = calloc(1, sizeof(*ibuf));
712 return got_error_from_errno();
714 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
715 return got_error_from_errno();
719 return got_error_from_errno();
721 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
728 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
731 imsg_init(ibuf, imsg_fds[0]);
732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
735 return request_tree(tree, repo, obj_fd);
738 static const struct got_error *
739 open_tree(struct got_tree_object **tree, struct got_repository *repo,
740 struct got_object_id *id, int check_cache)
742 const struct got_error *err = NULL;
743 struct got_packidx *packidx = NULL;
748 *tree = got_repo_get_cached_tree(repo, id);
756 err = got_repo_search_packidx(&packidx, &idx, repo, id);
758 struct got_pack *pack = NULL;
760 err = get_packfile_path(&path_packfile, packidx);
764 pack = got_repo_get_cached_pack(repo, path_packfile);
766 err = got_repo_cache_pack(&pack, repo, path_packfile,
771 err = read_packed_tree_privsep(tree, pack,
773 } else if (err->code == GOT_ERR_NO_OBJ) {
776 err = open_loose_object(&fd, id, repo);
779 err = read_tree_privsep(tree, fd, repo);
785 err = got_repo_cache_tree(repo, id, *tree);
791 const struct got_error *
792 got_object_open_as_tree(struct got_tree_object **tree,
793 struct got_repository *repo, struct got_object_id *id)
795 *tree = got_repo_get_cached_tree(repo, id);
801 return open_tree(tree, repo, id, 0);
804 const struct got_error *
805 got_object_tree_open(struct got_tree_object **tree,
806 struct got_repository *repo, struct got_object *obj)
808 return open_tree(tree, repo, got_object_get_id(obj), 1);
811 const struct got_tree_entries *
812 got_object_tree_get_entries(struct got_tree_object *tree)
814 return &tree->entries;
817 static const struct got_error *
818 request_packed_blob(size_t *size, size_t *hdrlen, int outfd,
819 struct got_pack *pack, struct got_packidx *packidx, int idx,
820 struct got_object_id *id)
822 const struct got_error *err = NULL;
824 int basefd, accumfd; /* temporary files for delta application */
826 basefd = got_opentempfd();
828 return got_error_from_errno();
829 accumfd = got_opentempfd();
831 return got_error_from_errno();
833 outfd_child = dup(outfd);
834 if (outfd_child == -1)
835 return got_error_from_errno();
837 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
841 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
847 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
856 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
864 err = got_privsep_recv_blob(size, hdrlen,
865 pack->privsep_child->ibuf);
869 if (lseek(outfd, SEEK_SET, 0) == -1)
870 err = got_error_from_errno();
875 static const struct got_error *
876 read_packed_blob_privsep(size_t *size, size_t *hdrlen, int outfd,
877 struct got_pack *pack, struct got_packidx *packidx, int idx,
878 struct got_object_id *id)
880 const struct got_error *err = NULL;
882 if (pack->privsep_child == NULL) {
883 err = start_pack_privsep_child(pack, packidx);
888 return request_packed_blob(size, hdrlen, outfd, pack, packidx, idx, id);
891 static const struct got_error *
892 request_blob(size_t *size, size_t *hdrlen, int outfd, int infd,
893 struct imsgbuf *ibuf)
895 const struct got_error *err = NULL;
898 outfd_child = dup(outfd);
899 if (outfd_child == -1)
900 return got_error_from_errno();
902 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
906 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
912 err = got_privsep_recv_blob(size, hdrlen, ibuf);
916 if (lseek(outfd, SEEK_SET, 0) == -1)
917 return got_error_from_errno();
922 static const struct got_error *
923 read_blob_privsep(size_t *size, size_t *hdrlen,
924 int outfd, int infd, struct got_repository *repo)
928 struct imsgbuf *ibuf;
930 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
931 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
932 return request_blob(size, hdrlen, outfd, infd, ibuf);
935 ibuf = calloc(1, sizeof(*ibuf));
937 return got_error_from_errno();
939 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
940 return got_error_from_errno();
944 return got_error_from_errno();
946 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
952 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
954 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
955 imsg_init(ibuf, imsg_fds[0]);
956 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
958 return request_blob(size, hdrlen, outfd, infd, ibuf);
961 static const struct got_error *
962 open_blob(struct got_blob_object **blob, struct got_repository *repo,
963 struct got_object_id *id, size_t blocksize)
965 const struct got_error *err = NULL;
966 struct got_packidx *packidx = NULL;
973 *blob = calloc(1, sizeof(**blob));
975 return got_error_from_errno();
977 outfd = got_opentempfd();
979 return got_error_from_errno();
981 (*blob)->read_buf = malloc(blocksize);
982 if ((*blob)->read_buf == NULL) {
983 err = got_error_from_errno();
987 err = got_repo_search_packidx(&packidx, &idx, repo, id);
989 struct got_pack *pack = NULL;
991 err = get_packfile_path(&path_packfile, packidx);
995 pack = got_repo_get_cached_pack(repo, path_packfile);
997 err = got_repo_cache_pack(&pack, repo, path_packfile,
1002 err = read_packed_blob_privsep(&size, &hdrlen, outfd, pack,
1004 } else if (err->code == GOT_ERR_NO_OBJ) {
1007 err = open_loose_object(&infd, id, repo);
1010 err = read_blob_privsep(&size, &hdrlen, outfd, infd, repo);
1016 if (fstat(outfd, &sb) == -1) {
1017 err = got_error_from_errno();
1021 if (sb.st_size != size) {
1022 err = got_error(GOT_ERR_PRIVSEP_LEN);
1026 if (hdrlen >= size) {
1027 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1031 (*blob)->f = fdopen(outfd, "rb");
1032 if ((*blob)->f == NULL) {
1033 err = got_error_from_errno();
1038 (*blob)->hdrlen = hdrlen;
1039 (*blob)->blocksize = blocksize;
1040 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1047 free((*blob)->read_buf);
1050 } else if (outfd != -1)
1056 const struct got_error *
1057 got_object_open_as_blob(struct got_blob_object **blob,
1058 struct got_repository *repo, struct got_object_id *id,
1061 return open_blob(blob, repo, id, blocksize);
1064 const struct got_error *
1065 got_object_blob_open(struct got_blob_object **blob,
1066 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1068 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1072 got_object_blob_close(struct got_blob_object *blob)
1074 free(blob->read_buf);
1080 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1082 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1086 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1088 return blob->hdrlen;
1092 got_object_blob_get_read_buf(struct got_blob_object *blob)
1094 return blob->read_buf;
1097 const struct got_error *
1098 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1102 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1103 if (n == 0 && ferror(blob->f))
1104 return got_ferror(blob->f, GOT_ERR_IO);
1109 const struct got_error *
1110 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1111 FILE *outfile, struct got_blob_object *blob)
1113 const struct got_error *err = NULL;
1123 hdrlen = got_object_blob_get_hdrlen(blob);
1125 err = got_object_blob_read_block(&len, blob);
1132 buf = got_object_blob_get_read_buf(blob);
1134 for (i = 0; i < len; i++) {
1139 /* Skip blob object header first time around. */
1140 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
1150 static const struct got_error *
1151 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1152 int pack_idx, struct got_object_id *id)
1154 const struct got_error *err = NULL;
1156 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1161 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1164 static const struct got_error *
1165 read_packed_tag_privsep(struct got_tag_object **tag,
1166 struct got_pack *pack, struct got_packidx *packidx, int idx,
1167 struct got_object_id *id)
1169 const struct got_error *err = NULL;
1171 if (pack->privsep_child)
1172 return request_packed_tag(tag, pack, idx, id);
1174 err = start_pack_privsep_child(pack, packidx);
1178 return request_packed_tag(tag, pack, idx, id);
1181 static const struct got_error *
1182 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1185 const struct got_error *err = NULL;
1186 struct imsgbuf *ibuf;
1188 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1190 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1194 return got_privsep_recv_tag(tag, ibuf);
1197 static const struct got_error *
1198 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1199 struct got_repository *repo)
1203 struct imsgbuf *ibuf;
1205 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1206 return request_tag(tag, repo, obj_fd);
1208 ibuf = calloc(1, sizeof(*ibuf));
1210 return got_error_from_errno();
1212 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1213 return got_error_from_errno();
1217 return got_error_from_errno();
1218 else if (pid == 0) {
1219 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1225 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1227 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1228 imsg_init(ibuf, imsg_fds[0]);
1229 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1231 return request_tag(tag, repo, obj_fd);
1234 static const struct got_error *
1235 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1236 struct got_object_id *id, int check_cache)
1238 const struct got_error *err = NULL;
1239 struct got_packidx *packidx = NULL;
1241 char *path_packfile;
1244 *tag = got_repo_get_cached_tag(repo, id);
1252 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1254 struct got_pack *pack = NULL;
1256 err = get_packfile_path(&path_packfile, packidx);
1260 pack = got_repo_get_cached_pack(repo, path_packfile);
1262 err = got_repo_cache_pack(&pack, repo, path_packfile,
1267 err = read_packed_tag_privsep(tag, pack,
1269 } else if (err->code == GOT_ERR_NO_OBJ) {
1272 err = open_loose_object(&fd, id, repo);
1275 err = read_tag_privsep(tag, fd, repo);
1281 err = got_repo_cache_tag(repo, id, *tag);
1287 const struct got_error *
1288 got_object_open_as_tag(struct got_tag_object **tag,
1289 struct got_repository *repo, struct got_object_id *id)
1291 *tag = got_repo_get_cached_tag(repo, id);
1297 return open_tag(tag, repo, id, 0);
1300 const struct got_error *
1301 got_object_tag_open(struct got_tag_object **tag,
1302 struct got_repository *repo, struct got_object *obj)
1304 return open_tag(tag, repo, got_object_get_id(obj), 1);
1307 static struct got_tree_entry *
1308 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1310 struct got_tree_entry *te;
1312 /* Note that tree entries are sorted in strncmp() order. */
1313 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1314 int cmp = strncmp(te->name, name, len);
1319 if (te->name[len] == '\0')
1325 const struct got_error *
1326 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1327 struct got_object_id *commit_id, const char *path)
1329 const struct got_error *err = NULL;
1330 struct got_commit_object *commit = NULL;
1331 struct got_tree_object *tree = NULL;
1332 struct got_tree_entry *te = NULL;
1333 const char *seg, *s;
1338 /* We are expecting an absolute in-repository path. */
1340 return got_error(GOT_ERR_NOT_ABSPATH);
1342 err = got_object_open_as_commit(&commit, repo, commit_id);
1346 /* Handle opening of root of commit's tree. */
1347 if (path[1] == '\0') {
1348 *id = got_object_id_dup(commit->tree_id);
1350 err = got_error_from_errno();
1354 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1359 s++; /* skip leading '/' */
1363 struct got_tree_object *next_tree;
1372 te = find_entry_by_name(tree, seg, seglen);
1374 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1385 err = got_object_open_as_tree(&next_tree, repo,
1390 got_object_tree_close(tree);
1396 *id = got_object_id_dup(te->id);
1398 return got_error_from_errno();
1400 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1403 got_object_commit_close(commit);
1405 got_object_tree_close(tree);
1409 const struct got_error *
1410 got_object_tree_path_changed(int *changed,
1411 struct got_tree_object *tree01, struct got_tree_object *tree02,
1412 const char *path, struct got_repository *repo)
1414 const struct got_error *err = NULL;
1415 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1416 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1417 const char *seg, *s;
1422 /* We are expecting an absolute in-repository path. */
1424 return got_error(GOT_ERR_NOT_ABSPATH);
1426 /* We not do support comparing the root path. */
1427 if (path[1] == '\0')
1428 return got_error(GOT_ERR_BAD_PATH);
1433 s++; /* skip leading '/' */
1437 struct got_tree_object *next_tree1, *next_tree2;
1446 te1 = find_entry_by_name(tree1, seg, seglen);
1448 err = got_error(GOT_ERR_NO_OBJ);
1452 te2 = find_entry_by_name(tree2, seg, seglen);
1458 if (te1->mode != te2->mode) {
1463 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1468 if (*s == '\0') { /* final path element */
1477 err = got_object_open_as_tree(&next_tree1, repo,
1482 if (tree1 != tree01)
1483 got_object_tree_close(tree1);
1486 err = got_object_open_as_tree(&next_tree2, repo,
1491 if (tree2 != tree02)
1492 got_object_tree_close(tree2);
1497 if (tree1 && tree1 != tree01)
1498 got_object_tree_close(tree1);
1499 if (tree2 && tree2 != tree02)
1500 got_object_tree_close(tree2);