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);
447 const struct got_error *
448 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
451 struct got_object_id id;
453 if (!got_parse_sha1_digest(id.sha1, id_str))
454 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
456 return got_object_open(obj, repo, &id);
459 const struct got_error *
460 got_object_resolve_id_str(struct got_object_id **id,
461 struct got_repository *repo, const char *id_str)
463 const struct got_error *err = NULL;
464 struct got_object *obj;
466 err = got_object_open_by_id_str(&obj, repo, id_str);
470 *id = got_object_id_dup(got_object_get_id(obj));
471 got_object_close(obj);
473 return got_error_from_errno();
478 static const struct got_error *
479 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
480 int pack_idx, struct got_object_id *id)
482 const struct got_error *err = NULL;
484 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
489 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
492 static const struct got_error *
493 read_packed_commit_privsep(struct got_commit_object **commit,
494 struct got_pack *pack, struct got_packidx *packidx, int idx,
495 struct got_object_id *id)
497 const struct got_error *err = NULL;
499 if (pack->privsep_child)
500 return request_packed_commit(commit, pack, idx, id);
502 err = start_pack_privsep_child(pack, packidx);
506 return request_packed_commit(commit, pack, idx, id);
509 static const struct got_error *
510 request_commit(struct got_commit_object **commit, struct got_repository *repo,
513 const struct got_error *err = NULL;
514 struct imsgbuf *ibuf;
516 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
518 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
522 return got_privsep_recv_commit(commit, ibuf);
525 static const struct got_error *
526 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
527 struct got_repository *repo)
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();
540 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
541 return got_error_from_errno();
545 return got_error_from_errno();
547 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
552 if (close(imsg_fds[1]) != 0)
553 return got_error_from_errno();
554 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
556 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
557 imsg_init(ibuf, imsg_fds[0]);
558 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
560 return request_commit(commit, repo, obj_fd);
564 static const struct got_error *
565 open_commit(struct got_commit_object **commit,
566 struct got_repository *repo, struct got_object_id *id, int check_cache)
568 const struct got_error *err = NULL;
569 struct got_packidx *packidx = NULL;
574 *commit = got_repo_get_cached_commit(repo, id);
575 if (*commit != NULL) {
582 err = got_repo_search_packidx(&packidx, &idx, repo, id);
584 struct got_pack *pack = NULL;
586 err = get_packfile_path(&path_packfile, packidx);
590 pack = got_repo_get_cached_pack(repo, path_packfile);
592 err = got_repo_cache_pack(&pack, repo, path_packfile,
597 err = read_packed_commit_privsep(commit, pack,
599 } else if (err->code == GOT_ERR_NO_OBJ) {
602 err = open_loose_object(&fd, id, repo);
605 err = read_commit_privsep(commit, fd, repo);
610 err = got_repo_cache_commit(repo, id, *commit);
616 const struct got_error *
617 got_object_open_as_commit(struct got_commit_object **commit,
618 struct got_repository *repo, struct got_object_id *id)
620 *commit = got_repo_get_cached_commit(repo, id);
621 if (*commit != NULL) {
626 return open_commit(commit, repo, id, 0);
629 const struct got_error *
630 got_object_commit_open(struct got_commit_object **commit,
631 struct got_repository *repo, struct got_object *obj)
633 return open_commit(commit, repo, got_object_get_id(obj), 1);
636 const struct got_error *
637 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
639 const struct got_error *err = NULL;
641 *qid = calloc(1, sizeof(**qid));
643 return got_error_from_errno();
645 (*qid)->id = got_object_id_dup(id);
646 if ((*qid)->id == NULL) {
647 err = got_error_from_errno();
648 got_object_qid_free(*qid);
656 static const struct got_error *
657 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
658 int pack_idx, struct got_object_id *id)
660 const struct got_error *err = NULL;
662 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
667 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
670 static const struct got_error *
671 read_packed_tree_privsep(struct got_tree_object **tree,
672 struct got_pack *pack, struct got_packidx *packidx, int idx,
673 struct got_object_id *id)
675 const struct got_error *err = NULL;
677 if (pack->privsep_child)
678 return request_packed_tree(tree, pack, idx, id);
680 err = start_pack_privsep_child(pack, packidx);
684 return request_packed_tree(tree, pack, idx, id);
687 static const struct got_error *
688 request_tree(struct got_tree_object **tree, struct got_repository *repo,
691 const struct got_error *err = NULL;
692 struct imsgbuf *ibuf;
694 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
696 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
700 return got_privsep_recv_tree(tree, ibuf);
703 const struct got_error *
704 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
705 struct got_repository *repo)
709 struct imsgbuf *ibuf;
711 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
712 return request_tree(tree, repo, obj_fd);
714 ibuf = calloc(1, sizeof(*ibuf));
716 return got_error_from_errno();
718 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
719 return got_error_from_errno();
723 return got_error_from_errno();
725 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
730 if (close(imsg_fds[1]) != 0)
731 return got_error_from_errno();
732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
735 imsg_init(ibuf, imsg_fds[0]);
736 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
739 return request_tree(tree, repo, obj_fd);
742 static const struct got_error *
743 open_tree(struct got_tree_object **tree, struct got_repository *repo,
744 struct got_object_id *id, int check_cache)
746 const struct got_error *err = NULL;
747 struct got_packidx *packidx = NULL;
752 *tree = got_repo_get_cached_tree(repo, id);
760 err = got_repo_search_packidx(&packidx, &idx, repo, id);
762 struct got_pack *pack = NULL;
764 err = get_packfile_path(&path_packfile, packidx);
768 pack = got_repo_get_cached_pack(repo, path_packfile);
770 err = got_repo_cache_pack(&pack, repo, path_packfile,
775 err = read_packed_tree_privsep(tree, pack,
777 } else if (err->code == GOT_ERR_NO_OBJ) {
780 err = open_loose_object(&fd, id, repo);
783 err = read_tree_privsep(tree, fd, repo);
788 err = got_repo_cache_tree(repo, id, *tree);
794 const struct got_error *
795 got_object_open_as_tree(struct got_tree_object **tree,
796 struct got_repository *repo, struct got_object_id *id)
798 *tree = got_repo_get_cached_tree(repo, id);
804 return open_tree(tree, repo, id, 0);
807 const struct got_error *
808 got_object_tree_open(struct got_tree_object **tree,
809 struct got_repository *repo, struct got_object *obj)
811 return open_tree(tree, repo, got_object_get_id(obj), 1);
814 const struct got_tree_entries *
815 got_object_tree_get_entries(struct got_tree_object *tree)
817 return &tree->entries;
820 static const struct got_error *
821 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
822 struct got_pack *pack, struct got_packidx *packidx, int idx,
823 struct got_object_id *id)
825 const struct got_error *err = NULL;
827 int basefd, accumfd; /* temporary files for delta application */
829 basefd = got_opentempfd();
831 return got_error_from_errno();
832 accumfd = got_opentempfd();
834 return got_error_from_errno();
836 outfd_child = dup(outfd);
837 if (outfd_child == -1)
838 return got_error_from_errno();
840 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
844 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
852 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
859 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
864 err = got_privsep_recv_blob(outbuf, 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(uint8_t **outbuf, size_t *size, size_t *hdrlen,
877 int outfd, 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(outbuf, size, hdrlen, outfd, pack, packidx,
892 static const struct got_error *
893 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
894 int infd, struct imsgbuf *ibuf)
896 const struct got_error *err = NULL;
899 outfd_child = dup(outfd);
900 if (outfd_child == -1)
901 return got_error_from_errno();
903 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
907 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
911 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
915 if (lseek(outfd, SEEK_SET, 0) == -1)
916 return got_error_from_errno();
921 static const struct got_error *
922 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
923 int outfd, int infd, struct got_repository *repo)
927 struct imsgbuf *ibuf;
929 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
930 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
931 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
934 ibuf = calloc(1, sizeof(*ibuf));
936 return got_error_from_errno();
938 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
939 return got_error_from_errno();
943 return got_error_from_errno();
945 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
950 if (close(imsg_fds[1]) != 0)
951 return got_error_from_errno();
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(outbuf, 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;
974 *blob = calloc(1, sizeof(**blob));
976 return got_error_from_errno();
978 outfd = got_opentempfd();
980 return got_error_from_errno();
982 (*blob)->read_buf = malloc(blocksize);
983 if ((*blob)->read_buf == NULL) {
984 err = got_error_from_errno();
988 err = got_repo_search_packidx(&packidx, &idx, repo, id);
990 struct got_pack *pack = NULL;
992 err = get_packfile_path(&path_packfile, packidx);
996 pack = got_repo_get_cached_pack(repo, path_packfile);
998 err = got_repo_cache_pack(&pack, repo, path_packfile,
1003 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1004 pack, packidx, idx, id);
1005 } else if (err->code == GOT_ERR_NO_OBJ) {
1008 err = open_loose_object(&infd, id, repo);
1011 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1017 if (hdrlen > size) {
1018 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1023 if (close(outfd) != 0 && err == NULL)
1024 err = got_error_from_errno();
1026 (*blob)->f = fmemopen(outbuf, size, "rb");
1027 if ((*blob)->f == NULL) {
1028 err = got_error_from_errno();
1032 (*blob)->data = outbuf;
1034 if (fstat(outfd, &sb) == -1) {
1035 err = got_error_from_errno();
1039 if (sb.st_size != size) {
1040 err = got_error(GOT_ERR_PRIVSEP_LEN);
1044 (*blob)->f = fdopen(outfd, "rb");
1045 if ((*blob)->f == NULL) {
1046 err = got_error_from_errno();
1053 (*blob)->hdrlen = hdrlen;
1054 (*blob)->blocksize = blocksize;
1055 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1062 free((*blob)->read_buf);
1063 free((*blob)->data);
1066 } else if (outfd != -1)
1072 const struct got_error *
1073 got_object_open_as_blob(struct got_blob_object **blob,
1074 struct got_repository *repo, struct got_object_id *id,
1077 return open_blob(blob, repo, id, blocksize);
1080 const struct got_error *
1081 got_object_blob_open(struct got_blob_object **blob,
1082 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1084 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1087 const struct got_error *
1088 got_object_blob_close(struct got_blob_object *blob)
1090 const struct got_error *err = NULL;
1091 free(blob->read_buf);
1092 if (fclose(blob->f) != 0)
1093 err = got_error_from_errno();
1100 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1102 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1106 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1108 return blob->hdrlen;
1112 got_object_blob_get_read_buf(struct got_blob_object *blob)
1114 return blob->read_buf;
1117 const struct got_error *
1118 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1122 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1123 if (n == 0 && ferror(blob->f))
1124 return got_ferror(blob->f, GOT_ERR_IO);
1129 const struct got_error *
1130 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1131 FILE *outfile, struct got_blob_object *blob)
1133 const struct got_error *err = NULL;
1134 size_t n, len, hdrlen;
1143 hdrlen = got_object_blob_get_hdrlen(blob);
1145 err = got_object_blob_read_block(&len, blob);
1152 buf = got_object_blob_get_read_buf(blob);
1154 for (i = 0; i < len; i++) {
1159 /* Skip blob object header first time around. */
1160 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1161 if (n != len - hdrlen)
1162 return got_ferror(outfile, GOT_ERR_IO);
1166 if (fflush(outfile) != 0)
1167 return got_error_from_errno();
1173 static const struct got_error *
1174 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1175 int pack_idx, struct got_object_id *id)
1177 const struct got_error *err = NULL;
1179 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1184 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1187 static const struct got_error *
1188 read_packed_tag_privsep(struct got_tag_object **tag,
1189 struct got_pack *pack, struct got_packidx *packidx, int idx,
1190 struct got_object_id *id)
1192 const struct got_error *err = NULL;
1194 if (pack->privsep_child)
1195 return request_packed_tag(tag, pack, idx, id);
1197 err = start_pack_privsep_child(pack, packidx);
1201 return request_packed_tag(tag, pack, idx, id);
1204 static const struct got_error *
1205 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1208 const struct got_error *err = NULL;
1209 struct imsgbuf *ibuf;
1211 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1213 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1217 return got_privsep_recv_tag(tag, ibuf);
1220 static const struct got_error *
1221 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1222 struct got_repository *repo)
1226 struct imsgbuf *ibuf;
1228 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1229 return request_tag(tag, repo, obj_fd);
1231 ibuf = calloc(1, sizeof(*ibuf));
1233 return got_error_from_errno();
1235 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1236 return got_error_from_errno();
1240 return got_error_from_errno();
1241 else if (pid == 0) {
1242 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1247 if (close(imsg_fds[1]) != 0)
1248 return got_error_from_errno();
1249 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1251 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1252 imsg_init(ibuf, imsg_fds[0]);
1253 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1255 return request_tag(tag, repo, obj_fd);
1258 static const struct got_error *
1259 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1260 struct got_object_id *id, int check_cache)
1262 const struct got_error *err = NULL;
1263 struct got_packidx *packidx = NULL;
1265 char *path_packfile;
1268 *tag = got_repo_get_cached_tag(repo, id);
1276 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1278 struct got_pack *pack = NULL;
1280 err = get_packfile_path(&path_packfile, packidx);
1284 pack = got_repo_get_cached_pack(repo, path_packfile);
1286 err = got_repo_cache_pack(&pack, repo, path_packfile,
1291 err = read_packed_tag_privsep(tag, pack,
1293 } else if (err->code == GOT_ERR_NO_OBJ) {
1296 err = open_loose_object(&fd, id, repo);
1299 err = read_tag_privsep(tag, fd, repo);
1304 err = got_repo_cache_tag(repo, id, *tag);
1310 const struct got_error *
1311 got_object_open_as_tag(struct got_tag_object **tag,
1312 struct got_repository *repo, struct got_object_id *id)
1314 *tag = got_repo_get_cached_tag(repo, id);
1320 return open_tag(tag, repo, id, 0);
1323 const struct got_error *
1324 got_object_tag_open(struct got_tag_object **tag,
1325 struct got_repository *repo, struct got_object *obj)
1327 return open_tag(tag, repo, got_object_get_id(obj), 1);
1331 got_object_tag_get_object_type(struct got_tag_object *tag)
1333 return tag->obj_type;
1336 struct got_object_id *
1337 got_object_tag_get_object_id(struct got_tag_object *tag)
1342 static struct got_tree_entry *
1343 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1345 struct got_tree_entry *te;
1347 /* Note that tree entries are sorted in strncmp() order. */
1348 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1349 int cmp = strncmp(te->name, name, len);
1354 if (te->name[len] == '\0')
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();
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();
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);