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 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();
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();
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_errno();
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();
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(),
191 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(),
196 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
197 fprintf(stderr, "%s: %s\n", getprogname(),
202 if (execl(path, path, repo_path, (char *)NULL) == -1) {
203 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
209 static const struct got_error *
210 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
211 struct got_object_id *id)
213 const struct got_error *err = NULL;
214 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
216 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
220 err = got_privsep_recv_obj(obj, ibuf);
224 (*obj)->path_packfile = strdup(pack->path_packfile);
225 if ((*obj)->path_packfile == NULL) {
226 err = got_error_from_errno();
229 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
234 static const struct got_error *
235 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
237 const struct got_error *err = NULL;
240 struct imsgbuf *ibuf;
242 ibuf = calloc(1, sizeof(*ibuf));
244 return got_error_from_errno();
246 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
247 if (pack->privsep_child == NULL) {
248 err = got_error_from_errno();
253 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
254 err = got_error_from_errno();
260 err = got_error_from_errno();
262 } else if (pid == 0) {
263 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
264 pack->path_packfile);
268 if (close(imsg_fds[1]) != 0)
269 return got_error_from_errno();
270 pack->privsep_child->imsg_fd = imsg_fds[0];
271 pack->privsep_child->pid = pid;
272 imsg_init(ibuf, imsg_fds[0]);
273 pack->privsep_child->ibuf = ibuf;
275 err = got_privsep_init_pack_child(ibuf, pack, packidx);
277 const struct got_error *child_err;
278 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
279 child_err = got_privsep_wait_for_child(
280 pack->privsep_child->pid);
281 if (child_err && err == NULL)
287 free(pack->privsep_child);
288 pack->privsep_child = NULL;
293 static const struct got_error *
294 read_packed_object_privsep(struct got_object **obj,
295 struct got_repository *repo, struct got_pack *pack,
296 struct got_packidx *packidx, int idx, struct got_object_id *id)
298 const struct got_error *err = NULL;
300 if (pack->privsep_child)
301 return request_packed_object(obj, pack, idx, id);
303 err = start_pack_privsep_child(pack, packidx);
307 return request_packed_object(obj, pack, idx, id);
311 static const struct got_error *
312 open_packed_object(struct got_object **obj, struct got_object_id *id,
313 struct got_repository *repo)
315 const struct got_error *err = NULL;
316 struct got_pack *pack = NULL;
317 struct got_packidx *packidx = NULL;
321 err = got_repo_search_packidx(&packidx, &idx, repo, id);
325 err = get_packfile_path(&path_packfile, packidx);
329 pack = got_repo_get_cached_pack(repo, path_packfile);
331 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
336 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
340 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
346 static const struct got_error *
347 request_object(struct got_object **obj, struct got_repository *repo, int fd)
349 const struct got_error *err = NULL;
350 struct imsgbuf *ibuf;
352 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
354 err = got_privsep_send_obj_req(ibuf, fd);
358 return got_privsep_recv_obj(obj, ibuf);
361 static const struct got_error *
362 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
367 struct imsgbuf *ibuf;
369 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
370 return request_object(obj, repo, obj_fd);
372 ibuf = calloc(1, sizeof(*ibuf));
374 return got_error_from_errno();
376 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
377 return got_error_from_errno();
381 return got_error_from_errno();
383 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
388 if (close(imsg_fds[1]) != 0)
389 return got_error_from_errno();
390 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
393 imsg_init(ibuf, imsg_fds[0]);
394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
396 return request_object(obj, repo, obj_fd);
400 const struct got_error *
401 got_object_open(struct got_object **obj, struct got_repository *repo,
402 struct got_object_id *id)
404 const struct got_error *err = NULL;
408 *obj = got_repo_get_cached_object(repo, id);
414 err = open_packed_object(obj, id, repo);
415 if (err && err->code != GOT_ERR_NO_OBJ)
419 return got_repo_cache_object(repo, id, *obj);
422 err = got_object_get_path(&path, id, repo);
426 fd = open(path, O_RDONLY | O_NOFOLLOW);
429 err = got_error_no_obj(id);
431 err = got_error_from_errno();
434 err = read_object_header_privsep(obj, repo, fd);
437 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
441 err = got_repo_cache_object(repo, id, *obj);
448 const struct got_error *
449 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
452 struct got_object_id id;
454 if (!got_parse_sha1_digest(id.sha1, id_str))
455 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
457 return got_object_open(obj, repo, &id);
460 const struct got_error *
461 got_object_resolve_id_str(struct got_object_id **id,
462 struct got_repository *repo, const char *id_str)
464 const struct got_error *err = NULL;
465 struct got_object *obj;
467 err = got_object_open_by_id_str(&obj, repo, id_str);
471 *id = got_object_id_dup(got_object_get_id(obj));
472 got_object_close(obj);
474 return got_error_from_errno();
479 static const struct got_error *
480 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
481 int pack_idx, struct got_object_id *id)
483 const struct got_error *err = NULL;
485 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
490 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
493 static const struct got_error *
494 read_packed_commit_privsep(struct got_commit_object **commit,
495 struct got_pack *pack, struct got_packidx *packidx, int idx,
496 struct got_object_id *id)
498 const struct got_error *err = NULL;
500 if (pack->privsep_child)
501 return request_packed_commit(commit, pack, idx, id);
503 err = start_pack_privsep_child(pack, packidx);
507 return request_packed_commit(commit, pack, idx, id);
510 static const struct got_error *
511 request_commit(struct got_commit_object **commit, struct got_repository *repo,
514 const struct got_error *err = NULL;
515 struct imsgbuf *ibuf;
517 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
519 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
523 return got_privsep_recv_commit(commit, ibuf);
526 static const struct got_error *
527 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
528 struct got_repository *repo)
532 struct imsgbuf *ibuf;
534 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
535 return request_commit(commit, repo, obj_fd);
537 ibuf = calloc(1, sizeof(*ibuf));
539 return got_error_from_errno();
541 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
542 return got_error_from_errno();
546 return got_error_from_errno();
548 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
553 if (close(imsg_fds[1]) != 0)
554 return got_error_from_errno();
555 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
557 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
558 imsg_init(ibuf, imsg_fds[0]);
559 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
561 return request_commit(commit, repo, obj_fd);
565 static const struct got_error *
566 open_commit(struct got_commit_object **commit,
567 struct got_repository *repo, struct got_object_id *id, int check_cache)
569 const struct got_error *err = NULL;
570 struct got_packidx *packidx = NULL;
575 *commit = got_repo_get_cached_commit(repo, id);
576 if (*commit != NULL) {
583 err = got_repo_search_packidx(&packidx, &idx, repo, id);
585 struct got_pack *pack = NULL;
587 err = get_packfile_path(&path_packfile, packidx);
591 pack = got_repo_get_cached_pack(repo, path_packfile);
593 err = got_repo_cache_pack(&pack, repo, path_packfile,
598 err = read_packed_commit_privsep(commit, pack,
600 } else if (err->code == GOT_ERR_NO_OBJ) {
603 err = open_loose_object(&fd, id, repo);
606 err = read_commit_privsep(commit, fd, repo);
611 err = got_repo_cache_commit(repo, id, *commit);
617 const struct got_error *
618 got_object_open_as_commit(struct got_commit_object **commit,
619 struct got_repository *repo, struct got_object_id *id)
621 *commit = got_repo_get_cached_commit(repo, id);
622 if (*commit != NULL) {
627 return open_commit(commit, repo, id, 0);
630 const struct got_error *
631 got_object_commit_open(struct got_commit_object **commit,
632 struct got_repository *repo, struct got_object *obj)
634 return open_commit(commit, repo, got_object_get_id(obj), 1);
637 const struct got_error *
638 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
640 const struct got_error *err = NULL;
642 *qid = calloc(1, sizeof(**qid));
644 return got_error_from_errno();
646 (*qid)->id = got_object_id_dup(id);
647 if ((*qid)->id == NULL) {
648 err = got_error_from_errno();
649 got_object_qid_free(*qid);
657 static const struct got_error *
658 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
659 int pack_idx, struct got_object_id *id)
661 const struct got_error *err = NULL;
663 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
668 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
671 static const struct got_error *
672 read_packed_tree_privsep(struct got_tree_object **tree,
673 struct got_pack *pack, struct got_packidx *packidx, int idx,
674 struct got_object_id *id)
676 const struct got_error *err = NULL;
678 if (pack->privsep_child)
679 return request_packed_tree(tree, pack, idx, id);
681 err = start_pack_privsep_child(pack, packidx);
685 return request_packed_tree(tree, pack, idx, id);
688 static const struct got_error *
689 request_tree(struct got_tree_object **tree, struct got_repository *repo,
692 const struct got_error *err = NULL;
693 struct imsgbuf *ibuf;
695 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
697 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
701 return got_privsep_recv_tree(tree, ibuf);
704 const struct got_error *
705 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
706 struct got_repository *repo)
710 struct imsgbuf *ibuf;
712 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
713 return request_tree(tree, repo, obj_fd);
715 ibuf = calloc(1, sizeof(*ibuf));
717 return got_error_from_errno();
719 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
720 return got_error_from_errno();
724 return got_error_from_errno();
726 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
731 if (close(imsg_fds[1]) != 0)
732 return got_error_from_errno();
733 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
735 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
736 imsg_init(ibuf, imsg_fds[0]);
737 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
740 return request_tree(tree, repo, obj_fd);
743 static const struct got_error *
744 open_tree(struct got_tree_object **tree, struct got_repository *repo,
745 struct got_object_id *id, int check_cache)
747 const struct got_error *err = NULL;
748 struct got_packidx *packidx = NULL;
753 *tree = got_repo_get_cached_tree(repo, id);
761 err = got_repo_search_packidx(&packidx, &idx, repo, id);
763 struct got_pack *pack = NULL;
765 err = get_packfile_path(&path_packfile, packidx);
769 pack = got_repo_get_cached_pack(repo, path_packfile);
771 err = got_repo_cache_pack(&pack, repo, path_packfile,
776 err = read_packed_tree_privsep(tree, pack,
778 } else if (err->code == GOT_ERR_NO_OBJ) {
781 err = open_loose_object(&fd, id, repo);
784 err = read_tree_privsep(tree, fd, repo);
789 err = got_repo_cache_tree(repo, id, *tree);
795 const struct got_error *
796 got_object_open_as_tree(struct got_tree_object **tree,
797 struct got_repository *repo, struct got_object_id *id)
799 *tree = got_repo_get_cached_tree(repo, id);
805 return open_tree(tree, repo, id, 0);
808 const struct got_error *
809 got_object_tree_open(struct got_tree_object **tree,
810 struct got_repository *repo, struct got_object *obj)
812 return open_tree(tree, repo, got_object_get_id(obj), 1);
815 const struct got_tree_entries *
816 got_object_tree_get_entries(struct got_tree_object *tree)
818 return &tree->entries;
821 static const struct got_error *
822 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
823 struct got_pack *pack, struct got_packidx *packidx, int idx,
824 struct got_object_id *id)
826 const struct got_error *err = NULL;
828 int basefd, accumfd; /* temporary files for delta application */
830 basefd = got_opentempfd();
832 return got_error_from_errno();
833 accumfd = got_opentempfd();
835 return got_error_from_errno();
837 outfd_child = dup(outfd);
838 if (outfd_child == -1)
839 return got_error_from_errno();
841 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
845 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
853 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
860 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
865 err = got_privsep_recv_blob(outbuf, size, hdrlen,
866 pack->privsep_child->ibuf);
870 if (lseek(outfd, SEEK_SET, 0) == -1)
871 err = got_error_from_errno();
876 static const struct got_error *
877 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
878 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
879 struct got_object_id *id)
881 const struct got_error *err = NULL;
883 if (pack->privsep_child == NULL) {
884 err = start_pack_privsep_child(pack, packidx);
889 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
893 static const struct got_error *
894 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
895 int infd, struct imsgbuf *ibuf)
897 const struct got_error *err = NULL;
900 outfd_child = dup(outfd);
901 if (outfd_child == -1)
902 return got_error_from_errno();
904 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
908 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
912 err = got_privsep_recv_blob(outbuf, 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(uint8_t **outbuf, 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(outbuf, 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,
951 if (close(imsg_fds[1]) != 0)
952 return got_error_from_errno();
953 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
955 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
956 imsg_init(ibuf, imsg_fds[0]);
957 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
959 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
962 static const struct got_error *
963 open_blob(struct got_blob_object **blob, struct got_repository *repo,
964 struct got_object_id *id, size_t blocksize)
966 const struct got_error *err = NULL;
967 struct got_packidx *packidx = NULL;
975 *blob = calloc(1, sizeof(**blob));
977 return got_error_from_errno();
979 outfd = got_opentempfd();
981 return got_error_from_errno();
983 (*blob)->read_buf = malloc(blocksize);
984 if ((*blob)->read_buf == NULL) {
985 err = got_error_from_errno();
989 err = got_repo_search_packidx(&packidx, &idx, repo, id);
991 struct got_pack *pack = NULL;
993 err = get_packfile_path(&path_packfile, packidx);
997 pack = got_repo_get_cached_pack(repo, path_packfile);
999 err = got_repo_cache_pack(&pack, repo, path_packfile,
1004 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1005 pack, packidx, idx, id);
1006 } else if (err->code == GOT_ERR_NO_OBJ) {
1009 err = open_loose_object(&infd, id, repo);
1012 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1018 if (hdrlen > size) {
1019 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1024 if (close(outfd) != 0 && err == NULL)
1025 err = got_error_from_errno();
1027 (*blob)->f = fmemopen(outbuf, size, "rb");
1028 if ((*blob)->f == NULL) {
1029 err = got_error_from_errno();
1033 (*blob)->data = outbuf;
1035 if (fstat(outfd, &sb) == -1) {
1036 err = got_error_from_errno();
1040 if (sb.st_size != size) {
1041 err = got_error(GOT_ERR_PRIVSEP_LEN);
1045 (*blob)->f = fdopen(outfd, "rb");
1046 if ((*blob)->f == NULL) {
1047 err = got_error_from_errno();
1054 (*blob)->hdrlen = hdrlen;
1055 (*blob)->blocksize = blocksize;
1056 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1061 got_object_blob_close(*blob);
1063 } else if (outfd != -1)
1069 const struct got_error *
1070 got_object_open_as_blob(struct got_blob_object **blob,
1071 struct got_repository *repo, struct got_object_id *id,
1074 return open_blob(blob, repo, id, blocksize);
1077 const struct got_error *
1078 got_object_blob_open(struct got_blob_object **blob,
1079 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1081 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1084 const struct got_error *
1085 got_object_blob_close(struct got_blob_object *blob)
1087 const struct got_error *err = NULL;
1088 free(blob->read_buf);
1089 if (blob->f && fclose(blob->f) != 0)
1090 err = got_error_from_errno();
1097 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1099 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1103 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1105 return blob->hdrlen;
1109 got_object_blob_get_read_buf(struct got_blob_object *blob)
1111 return blob->read_buf;
1114 const struct got_error *
1115 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1119 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1120 if (n == 0 && ferror(blob->f))
1121 return got_ferror(blob->f, GOT_ERR_IO);
1126 const struct got_error *
1127 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1128 FILE *outfile, struct got_blob_object *blob)
1130 const struct got_error *err = NULL;
1131 size_t n, len, hdrlen;
1140 hdrlen = got_object_blob_get_hdrlen(blob);
1142 err = got_object_blob_read_block(&len, blob);
1149 buf = got_object_blob_get_read_buf(blob);
1151 for (i = 0; i < len; i++) {
1156 /* Skip blob object header first time around. */
1157 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1158 if (n != len - hdrlen)
1159 return got_ferror(outfile, GOT_ERR_IO);
1163 if (fflush(outfile) != 0)
1164 return got_error_from_errno();
1170 static const struct got_error *
1171 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1172 int pack_idx, struct got_object_id *id)
1174 const struct got_error *err = NULL;
1176 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1181 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1184 static const struct got_error *
1185 read_packed_tag_privsep(struct got_tag_object **tag,
1186 struct got_pack *pack, struct got_packidx *packidx, int idx,
1187 struct got_object_id *id)
1189 const struct got_error *err = NULL;
1191 if (pack->privsep_child)
1192 return request_packed_tag(tag, pack, idx, id);
1194 err = start_pack_privsep_child(pack, packidx);
1198 return request_packed_tag(tag, pack, idx, id);
1201 static const struct got_error *
1202 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1205 const struct got_error *err = NULL;
1206 struct imsgbuf *ibuf;
1208 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1210 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1214 return got_privsep_recv_tag(tag, ibuf);
1217 static const struct got_error *
1218 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1219 struct got_repository *repo)
1223 struct imsgbuf *ibuf;
1225 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1226 return request_tag(tag, repo, obj_fd);
1228 ibuf = calloc(1, sizeof(*ibuf));
1230 return got_error_from_errno();
1232 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1233 return got_error_from_errno();
1237 return got_error_from_errno();
1238 else if (pid == 0) {
1239 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1244 if (close(imsg_fds[1]) != 0)
1245 return got_error_from_errno();
1246 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1248 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1249 imsg_init(ibuf, imsg_fds[0]);
1250 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1252 return request_tag(tag, repo, obj_fd);
1255 static const struct got_error *
1256 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1257 struct got_object_id *id, int check_cache)
1259 const struct got_error *err = NULL;
1260 struct got_packidx *packidx = NULL;
1262 char *path_packfile;
1265 *tag = got_repo_get_cached_tag(repo, id);
1273 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1275 struct got_pack *pack = NULL;
1277 err = get_packfile_path(&path_packfile, packidx);
1281 pack = got_repo_get_cached_pack(repo, path_packfile);
1283 err = got_repo_cache_pack(&pack, repo, path_packfile,
1288 err = read_packed_tag_privsep(tag, pack,
1290 } else if (err->code == GOT_ERR_NO_OBJ) {
1293 err = open_loose_object(&fd, id, repo);
1296 err = read_tag_privsep(tag, fd, repo);
1301 err = got_repo_cache_tag(repo, id, *tag);
1307 const struct got_error *
1308 got_object_open_as_tag(struct got_tag_object **tag,
1309 struct got_repository *repo, struct got_object_id *id)
1311 *tag = got_repo_get_cached_tag(repo, id);
1317 return open_tag(tag, repo, id, 0);
1320 const struct got_error *
1321 got_object_tag_open(struct got_tag_object **tag,
1322 struct got_repository *repo, struct got_object *obj)
1324 return open_tag(tag, repo, got_object_get_id(obj), 1);
1328 got_object_tag_get_object_type(struct got_tag_object *tag)
1330 return tag->obj_type;
1333 struct got_object_id *
1334 got_object_tag_get_object_id(struct got_tag_object *tag)
1339 static struct got_tree_entry *
1340 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1342 struct got_tree_entry *te;
1344 /* Note that tree entries are sorted in strncmp() order. */
1345 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1346 int cmp = strncmp(te->name, name, len);
1351 if (te->name[len] == '\0')
1357 const struct got_tree_entry *
1358 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1360 return find_entry_by_name(tree, name, strlen(name));
1363 const struct got_error *
1364 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1365 struct got_object_id *commit_id, const char *path)
1367 const struct got_error *err = NULL;
1368 struct got_commit_object *commit = NULL;
1369 struct got_tree_object *tree = NULL;
1370 struct got_tree_entry *te = NULL;
1371 const char *seg, *s;
1376 /* We are expecting an absolute in-repository path. */
1378 return got_error(GOT_ERR_NOT_ABSPATH);
1380 err = got_object_open_as_commit(&commit, repo, commit_id);
1384 /* Handle opening of root of commit's tree. */
1385 if (path[1] == '\0') {
1386 *id = got_object_id_dup(commit->tree_id);
1388 err = got_error_from_errno();
1392 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1397 s++; /* skip leading '/' */
1401 struct got_tree_object *next_tree;
1410 te = find_entry_by_name(tree, seg, seglen);
1412 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1423 err = got_object_open_as_tree(&next_tree, repo,
1428 got_object_tree_close(tree);
1434 *id = got_object_id_dup(te->id);
1436 return got_error_from_errno();
1438 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1441 got_object_commit_close(commit);
1443 got_object_tree_close(tree);
1447 const struct got_error *
1448 got_object_tree_path_changed(int *changed,
1449 struct got_tree_object *tree01, struct got_tree_object *tree02,
1450 const char *path, struct got_repository *repo)
1452 const struct got_error *err = NULL;
1453 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1454 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1455 const char *seg, *s;
1460 /* We are expecting an absolute in-repository path. */
1462 return got_error(GOT_ERR_NOT_ABSPATH);
1464 /* We not do support comparing the root path. */
1465 if (path[1] == '\0')
1466 return got_error(GOT_ERR_BAD_PATH);
1471 s++; /* skip leading '/' */
1475 struct got_tree_object *next_tree1, *next_tree2;
1484 te1 = find_entry_by_name(tree1, seg, seglen);
1486 err = got_error(GOT_ERR_NO_OBJ);
1490 te2 = find_entry_by_name(tree2, seg, seglen);
1496 if (te1->mode != te2->mode) {
1501 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1506 if (*s == '\0') { /* final path element */
1515 err = got_object_open_as_tree(&next_tree1, repo,
1520 if (tree1 != tree01)
1521 got_object_tree_close(tree1);
1524 err = got_object_open_as_tree(&next_tree2, repo,
1529 if (tree2 != tree02)
1530 got_object_tree_close(tree2);
1535 if (tree1 && tree1 != tree01)
1536 got_object_tree_close(tree1);
1537 if (tree2 && tree2 != tree02)
1538 got_object_tree_close(tree2);
1542 const struct got_error *
1543 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1544 struct got_tree_entry *te)
1546 const struct got_error *err = NULL;
1548 *new_te = calloc(1, sizeof(**new_te));
1549 if (*new_te == NULL)
1550 return got_error_from_errno();
1552 (*new_te)->mode = te->mode;
1553 (*new_te)->name = strdup(te->name);
1554 if ((*new_te)->name == NULL) {
1555 err = got_error_from_errno();
1556 got_object_tree_entry_close(*new_te);
1560 (*new_te)->id = got_object_id_dup(te->id);
1561 if ((*new_te)->id == NULL) {
1562 err = got_error_from_errno();
1563 got_object_tree_entry_close(*new_te);