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>
24 #include <sys/resource.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 struct got_object_id *
61 got_object_id_dup(struct got_object_id *id1)
63 struct got_object_id *id2;
65 id2 = malloc(sizeof(*id2));
68 memcpy(id2, id1, sizeof(*id2));
72 struct got_object_id *
73 got_object_get_id(struct got_object *obj)
78 const struct got_error *
79 got_object_get_id_str(char **outbuf, struct got_object *obj)
81 return got_object_id_str(outbuf, &obj->id);
84 const struct got_error *
85 got_object_get_type(int *type, struct got_repository *repo,
86 struct got_object_id *id)
88 const struct got_error *err = NULL;
89 struct got_object *obj;
91 err = got_object_open(&obj, repo, id);
96 case GOT_OBJ_TYPE_COMMIT:
97 case GOT_OBJ_TYPE_TREE:
98 case GOT_OBJ_TYPE_BLOB:
99 case GOT_OBJ_TYPE_TAG:
103 err = got_error(GOT_ERR_OBJ_TYPE);
107 got_object_close(obj);
111 const struct got_error *
112 got_object_get_path(char **path, struct got_object_id *id,
113 struct got_repository *repo)
115 const struct got_error *err = NULL;
121 path_objects = got_repo_get_path_objects(repo);
122 if (path_objects == NULL)
123 return got_error_from_errno("got_repo_get_path_objects");
125 err = got_object_id_str(&hex, id);
129 if (asprintf(path, "%s/%.2x/%s", path_objects,
130 id->sha1[0], hex + 2) == -1)
131 err = got_error_from_errno("asprintf");
139 static const struct got_error *
140 open_loose_object(int *fd, struct got_object_id *id,
141 struct got_repository *repo)
143 const struct got_error *err = NULL;
146 err = got_object_get_path(&path, id, repo);
149 *fd = open(path, O_RDONLY | O_NOFOLLOW);
151 err = got_error_from_errno2("open", path);
159 static const struct got_error *
160 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
164 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
165 size = strlen(packidx->path_packidx) + 2;
166 if (size < GOT_PACKFILE_NAMELEN + 1)
167 return got_error(GOT_ERR_BAD_PATH);
169 *path_packfile = malloc(size);
170 if (*path_packfile == NULL)
171 return got_error_from_errno("malloc");
173 /* Copy up to and excluding ".idx". */
174 if (strlcpy(*path_packfile, packidx->path_packidx,
175 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
178 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
179 return got_error(GOT_ERR_NO_SPACE);
185 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
187 if (close(imsg_fds[0]) != 0) {
188 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
192 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
193 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
196 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
197 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
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("strdup");
228 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
234 set_max_datasize(void)
238 if (getrlimit(RLIMIT_DATA, &rl) != 0)
241 rl.rlim_cur = rl.rlim_max;
242 setrlimit(RLIMIT_DATA, &rl);
245 static const struct got_error *
246 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
248 const struct got_error *err = NULL;
251 struct imsgbuf *ibuf;
253 ibuf = calloc(1, sizeof(*ibuf));
255 return got_error_from_errno("calloc");
257 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
258 if (pack->privsep_child == NULL) {
259 err = got_error_from_errno("calloc");
264 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
265 err = got_error_from_errno("socketpair");
271 err = got_error_from_errno("fork");
273 } else if (pid == 0) {
275 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
276 pack->path_packfile);
280 if (close(imsg_fds[1]) != 0)
281 return got_error_from_errno("close");
282 pack->privsep_child->imsg_fd = imsg_fds[0];
283 pack->privsep_child->pid = pid;
284 imsg_init(ibuf, imsg_fds[0]);
285 pack->privsep_child->ibuf = ibuf;
287 err = got_privsep_init_pack_child(ibuf, pack, packidx);
289 const struct got_error *child_err;
290 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
291 child_err = got_privsep_wait_for_child(
292 pack->privsep_child->pid);
293 if (child_err && err == NULL)
299 free(pack->privsep_child);
300 pack->privsep_child = NULL;
305 static const struct got_error *
306 read_packed_object_privsep(struct got_object **obj,
307 struct got_repository *repo, struct got_pack *pack,
308 struct got_packidx *packidx, int idx, struct got_object_id *id)
310 const struct got_error *err = NULL;
312 if (pack->privsep_child)
313 return request_packed_object(obj, pack, idx, id);
315 err = start_pack_privsep_child(pack, packidx);
319 return request_packed_object(obj, pack, idx, id);
323 static const struct got_error *
324 open_packed_object(struct got_object **obj, struct got_object_id *id,
325 struct got_repository *repo)
327 const struct got_error *err = NULL;
328 struct got_pack *pack = NULL;
329 struct got_packidx *packidx = NULL;
333 err = got_repo_search_packidx(&packidx, &idx, repo, id);
337 err = get_packfile_path(&path_packfile, packidx);
341 pack = got_repo_get_cached_pack(repo, path_packfile);
343 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
348 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
352 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
358 static const struct got_error *
359 request_object(struct got_object **obj, struct got_repository *repo, int fd)
361 const struct got_error *err = NULL;
362 struct imsgbuf *ibuf;
364 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
366 err = got_privsep_send_obj_req(ibuf, fd);
370 return got_privsep_recv_obj(obj, ibuf);
373 static const struct got_error *
374 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
379 struct imsgbuf *ibuf;
381 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
382 return request_object(obj, repo, obj_fd);
384 ibuf = calloc(1, sizeof(*ibuf));
386 return got_error_from_errno("calloc");
388 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
389 return got_error_from_errno("socketpair");
393 return got_error_from_errno("fork");
395 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
400 if (close(imsg_fds[1]) != 0)
401 return got_error_from_errno("close");
402 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
404 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
405 imsg_init(ibuf, imsg_fds[0]);
406 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
408 return request_object(obj, repo, obj_fd);
412 const struct got_error *
413 got_object_open(struct got_object **obj, struct got_repository *repo,
414 struct got_object_id *id)
416 const struct got_error *err = NULL;
420 *obj = got_repo_get_cached_object(repo, id);
426 err = open_packed_object(obj, id, repo);
427 if (err && err->code != GOT_ERR_NO_OBJ)
431 return got_repo_cache_object(repo, id, *obj);
434 err = got_object_get_path(&path, id, repo);
438 fd = open(path, O_RDONLY | O_NOFOLLOW);
441 err = got_error_no_obj(id);
443 err = got_error_from_errno2("open", path);
446 err = read_object_header_privsep(obj, repo, fd);
449 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
453 err = got_repo_cache_object(repo, id, *obj);
460 const struct got_error *
461 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
464 struct got_object_id id;
466 if (!got_parse_sha1_digest(id.sha1, id_str))
467 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
469 return got_object_open(obj, repo, &id);
472 const struct got_error *
473 got_object_resolve_id_str(struct got_object_id **id,
474 struct got_repository *repo, const char *id_str)
476 const struct got_error *err = NULL;
477 struct got_object *obj;
479 err = got_object_open_by_id_str(&obj, repo, id_str);
483 *id = got_object_id_dup(got_object_get_id(obj));
484 got_object_close(obj);
486 return got_error_from_errno("got_object_id_dup");
491 static const struct got_error *
492 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
493 int pack_idx, struct got_object_id *id)
495 const struct got_error *err = NULL;
497 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
502 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
505 static const struct got_error *
506 read_packed_commit_privsep(struct got_commit_object **commit,
507 struct got_pack *pack, struct got_packidx *packidx, int idx,
508 struct got_object_id *id)
510 const struct got_error *err = NULL;
512 if (pack->privsep_child)
513 return request_packed_commit(commit, pack, idx, id);
515 err = start_pack_privsep_child(pack, packidx);
519 return request_packed_commit(commit, pack, idx, id);
522 static const struct got_error *
523 request_commit(struct got_commit_object **commit, struct got_repository *repo,
526 const struct got_error *err = NULL;
527 struct imsgbuf *ibuf;
529 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
531 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
535 return got_privsep_recv_commit(commit, ibuf);
538 static const struct got_error *
539 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
540 struct got_repository *repo)
544 struct imsgbuf *ibuf;
546 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
547 return request_commit(commit, repo, obj_fd);
549 ibuf = calloc(1, sizeof(*ibuf));
551 return got_error_from_errno("calloc");
553 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
554 return got_error_from_errno("socketpair");
558 return got_error_from_errno("fork");
560 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
565 if (close(imsg_fds[1]) != 0)
566 return got_error_from_errno("close");
567 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
569 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
570 imsg_init(ibuf, imsg_fds[0]);
571 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
573 return request_commit(commit, repo, obj_fd);
577 static const struct got_error *
578 open_commit(struct got_commit_object **commit,
579 struct got_repository *repo, struct got_object_id *id, int check_cache)
581 const struct got_error *err = NULL;
582 struct got_packidx *packidx = NULL;
587 *commit = got_repo_get_cached_commit(repo, id);
588 if (*commit != NULL) {
595 err = got_repo_search_packidx(&packidx, &idx, repo, id);
597 struct got_pack *pack = NULL;
599 err = get_packfile_path(&path_packfile, packidx);
603 pack = got_repo_get_cached_pack(repo, path_packfile);
605 err = got_repo_cache_pack(&pack, repo, path_packfile,
610 err = read_packed_commit_privsep(commit, pack,
612 } else if (err->code == GOT_ERR_NO_OBJ) {
615 err = open_loose_object(&fd, id, repo);
618 err = read_commit_privsep(commit, fd, repo);
623 err = got_repo_cache_commit(repo, id, *commit);
629 const struct got_error *
630 got_object_open_as_commit(struct got_commit_object **commit,
631 struct got_repository *repo, struct got_object_id *id)
633 *commit = got_repo_get_cached_commit(repo, id);
634 if (*commit != NULL) {
639 return open_commit(commit, repo, id, 0);
642 const struct got_error *
643 got_object_commit_open(struct got_commit_object **commit,
644 struct got_repository *repo, struct got_object *obj)
646 return open_commit(commit, repo, got_object_get_id(obj), 1);
649 const struct got_error *
650 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
652 const struct got_error *err = NULL;
654 *qid = calloc(1, sizeof(**qid));
656 return got_error_from_errno("calloc");
658 (*qid)->id = got_object_id_dup(id);
659 if ((*qid)->id == NULL) {
660 err = got_error_from_errno("got_object_id_dup");
661 got_object_qid_free(*qid);
669 static const struct got_error *
670 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
671 int pack_idx, struct got_object_id *id)
673 const struct got_error *err = NULL;
675 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
680 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
683 static const struct got_error *
684 read_packed_tree_privsep(struct got_tree_object **tree,
685 struct got_pack *pack, struct got_packidx *packidx, int idx,
686 struct got_object_id *id)
688 const struct got_error *err = NULL;
690 if (pack->privsep_child)
691 return request_packed_tree(tree, pack, idx, id);
693 err = start_pack_privsep_child(pack, packidx);
697 return request_packed_tree(tree, pack, idx, id);
700 static const struct got_error *
701 request_tree(struct got_tree_object **tree, struct got_repository *repo,
704 const struct got_error *err = NULL;
705 struct imsgbuf *ibuf;
707 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
709 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
713 return got_privsep_recv_tree(tree, ibuf);
716 const struct got_error *
717 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
718 struct got_repository *repo)
722 struct imsgbuf *ibuf;
724 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
725 return request_tree(tree, repo, obj_fd);
727 ibuf = calloc(1, sizeof(*ibuf));
729 return got_error_from_errno("calloc");
731 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
732 return got_error_from_errno("socketpair");
736 return got_error_from_errno("fork");
738 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
743 if (close(imsg_fds[1]) != 0)
744 return got_error_from_errno("close");
745 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
747 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
748 imsg_init(ibuf, imsg_fds[0]);
749 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
752 return request_tree(tree, repo, obj_fd);
755 static const struct got_error *
756 open_tree(struct got_tree_object **tree, struct got_repository *repo,
757 struct got_object_id *id, int check_cache)
759 const struct got_error *err = NULL;
760 struct got_packidx *packidx = NULL;
765 *tree = got_repo_get_cached_tree(repo, id);
773 err = got_repo_search_packidx(&packidx, &idx, repo, id);
775 struct got_pack *pack = NULL;
777 err = get_packfile_path(&path_packfile, packidx);
781 pack = got_repo_get_cached_pack(repo, path_packfile);
783 err = got_repo_cache_pack(&pack, repo, path_packfile,
788 err = read_packed_tree_privsep(tree, pack,
790 } else if (err->code == GOT_ERR_NO_OBJ) {
793 err = open_loose_object(&fd, id, repo);
796 err = read_tree_privsep(tree, fd, repo);
801 err = got_repo_cache_tree(repo, id, *tree);
807 const struct got_error *
808 got_object_open_as_tree(struct got_tree_object **tree,
809 struct got_repository *repo, struct got_object_id *id)
811 *tree = got_repo_get_cached_tree(repo, id);
817 return open_tree(tree, repo, id, 0);
820 const struct got_error *
821 got_object_tree_open(struct got_tree_object **tree,
822 struct got_repository *repo, struct got_object *obj)
824 return open_tree(tree, repo, got_object_get_id(obj), 1);
827 const struct got_tree_entries *
828 got_object_tree_get_entries(struct got_tree_object *tree)
830 return &tree->entries;
833 static const struct got_error *
834 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
835 struct got_pack *pack, struct got_packidx *packidx, int idx,
836 struct got_object_id *id)
838 const struct got_error *err = NULL;
840 int basefd, accumfd; /* temporary files for delta application */
842 basefd = got_opentempfd();
844 return got_error_from_errno("got_opentempfd");
845 accumfd = got_opentempfd();
847 return got_error_from_errno("got_opentempfd");
849 outfd_child = dup(outfd);
850 if (outfd_child == -1)
851 return got_error_from_errno("dup");
853 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
857 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
865 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
872 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
877 err = got_privsep_recv_blob(outbuf, size, hdrlen,
878 pack->privsep_child->ibuf);
882 if (lseek(outfd, SEEK_SET, 0) == -1)
883 err = got_error_from_errno("lseek");
888 static const struct got_error *
889 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
890 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
891 struct got_object_id *id)
893 const struct got_error *err = NULL;
895 if (pack->privsep_child == NULL) {
896 err = start_pack_privsep_child(pack, packidx);
901 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
905 static const struct got_error *
906 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
907 int infd, struct imsgbuf *ibuf)
909 const struct got_error *err = NULL;
912 outfd_child = dup(outfd);
913 if (outfd_child == -1)
914 return got_error_from_errno("dup");
916 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
920 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
924 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
928 if (lseek(outfd, SEEK_SET, 0) == -1)
929 return got_error_from_errno("lseek");
934 static const struct got_error *
935 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
936 int outfd, int infd, struct got_repository *repo)
940 struct imsgbuf *ibuf;
942 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
943 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
944 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
947 ibuf = calloc(1, sizeof(*ibuf));
949 return got_error_from_errno("calloc");
951 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
952 return got_error_from_errno("socketpair");
956 return got_error_from_errno("fork");
958 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
963 if (close(imsg_fds[1]) != 0)
964 return got_error_from_errno("close");
965 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
967 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
968 imsg_init(ibuf, imsg_fds[0]);
969 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
971 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
974 static const struct got_error *
975 open_blob(struct got_blob_object **blob, struct got_repository *repo,
976 struct got_object_id *id, size_t blocksize)
978 const struct got_error *err = NULL;
979 struct got_packidx *packidx = NULL;
987 *blob = calloc(1, sizeof(**blob));
989 return got_error_from_errno("calloc");
991 outfd = got_opentempfd();
993 return got_error_from_errno("got_opentempfd");
995 (*blob)->read_buf = malloc(blocksize);
996 if ((*blob)->read_buf == NULL) {
997 err = got_error_from_errno("malloc");
1001 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1003 struct got_pack *pack = NULL;
1005 err = get_packfile_path(&path_packfile, packidx);
1009 pack = got_repo_get_cached_pack(repo, path_packfile);
1011 err = got_repo_cache_pack(&pack, repo, path_packfile,
1016 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1017 pack, packidx, idx, id);
1018 } else if (err->code == GOT_ERR_NO_OBJ) {
1021 err = open_loose_object(&infd, id, repo);
1024 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
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("close");
1039 (*blob)->f = fmemopen(outbuf, size, "rb");
1040 if ((*blob)->f == NULL) {
1041 err = got_error_from_errno("fmemopen");
1045 (*blob)->data = outbuf;
1047 if (fstat(outfd, &sb) == -1) {
1048 err = got_error_from_errno("fstat");
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("fdopen");
1066 (*blob)->hdrlen = hdrlen;
1067 (*blob)->blocksize = blocksize;
1068 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1073 got_object_blob_close(*blob);
1075 } else if (outfd != -1)
1081 const struct got_error *
1082 got_object_open_as_blob(struct got_blob_object **blob,
1083 struct got_repository *repo, struct got_object_id *id,
1086 return open_blob(blob, repo, id, blocksize);
1089 const struct got_error *
1090 got_object_blob_open(struct got_blob_object **blob,
1091 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1093 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1096 const struct got_error *
1097 got_object_blob_close(struct got_blob_object *blob)
1099 const struct got_error *err = NULL;
1100 free(blob->read_buf);
1101 if (blob->f && fclose(blob->f) != 0)
1102 err = got_error_from_errno("fclose");
1109 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1111 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1115 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1117 return blob->hdrlen;
1121 got_object_blob_get_read_buf(struct got_blob_object *blob)
1123 return blob->read_buf;
1126 const struct got_error *
1127 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1131 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1132 if (n == 0 && ferror(blob->f))
1133 return got_ferror(blob->f, GOT_ERR_IO);
1138 const struct got_error *
1139 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1140 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1142 const struct got_error *err = NULL;
1143 size_t n, len, hdrlen;
1146 size_t noffsets = 0;
1150 *line_offsets = NULL;
1156 hdrlen = got_object_blob_get_hdrlen(blob);
1158 err = got_object_blob_read_block(&len, blob);
1163 buf = got_object_blob_get_read_buf(blob);
1164 for (i = hdrlen; i < len; i++) {
1169 if (line_offsets && nlines && noffsets < *nlines) {
1170 off_t *o = recallocarray(*line_offsets,
1171 noffsets, *nlines, sizeof(**line_offsets));
1173 free(*line_offsets);
1174 *line_offsets = NULL;
1175 return got_error_from_errno(
1181 if (line_offsets && nlines && total_len) {
1182 (*line_offsets)[*nlines - 1] = off;
1183 off = *total_len + i + 1 - got_object_blob_get_hdrlen(blob);
1188 /* Skip blob object header first time around. */
1189 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1190 if (n != len - hdrlen)
1191 return got_ferror(outfile, GOT_ERR_IO);
1195 if (fflush(outfile) != 0)
1196 return got_error_from_errno("fflush");
1202 static const struct got_error *
1203 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1204 int pack_idx, struct got_object_id *id)
1206 const struct got_error *err = NULL;
1208 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1213 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1216 static const struct got_error *
1217 read_packed_tag_privsep(struct got_tag_object **tag,
1218 struct got_pack *pack, struct got_packidx *packidx, int idx,
1219 struct got_object_id *id)
1221 const struct got_error *err = NULL;
1223 if (pack->privsep_child)
1224 return request_packed_tag(tag, pack, idx, id);
1226 err = start_pack_privsep_child(pack, packidx);
1230 return request_packed_tag(tag, pack, idx, id);
1233 static const struct got_error *
1234 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1237 const struct got_error *err = NULL;
1238 struct imsgbuf *ibuf;
1240 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1242 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1246 return got_privsep_recv_tag(tag, ibuf);
1249 static const struct got_error *
1250 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1251 struct got_repository *repo)
1255 struct imsgbuf *ibuf;
1257 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1258 return request_tag(tag, repo, obj_fd);
1260 ibuf = calloc(1, sizeof(*ibuf));
1262 return got_error_from_errno("calloc");
1264 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1265 return got_error_from_errno("socketpair");
1269 return got_error_from_errno("fork");
1270 else if (pid == 0) {
1271 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1276 if (close(imsg_fds[1]) != 0)
1277 return got_error_from_errno("close");
1278 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1280 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1281 imsg_init(ibuf, imsg_fds[0]);
1282 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1284 return request_tag(tag, repo, obj_fd);
1287 static const struct got_error *
1288 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1289 struct got_object_id *id, int check_cache)
1291 const struct got_error *err = NULL;
1292 struct got_packidx *packidx = NULL;
1294 char *path_packfile;
1297 *tag = got_repo_get_cached_tag(repo, id);
1305 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1307 struct got_pack *pack = NULL;
1309 err = get_packfile_path(&path_packfile, packidx);
1313 pack = got_repo_get_cached_pack(repo, path_packfile);
1315 err = got_repo_cache_pack(&pack, repo, path_packfile,
1320 err = read_packed_tag_privsep(tag, pack,
1322 } else if (err->code == GOT_ERR_NO_OBJ) {
1325 err = open_loose_object(&fd, id, repo);
1328 err = read_tag_privsep(tag, fd, repo);
1333 err = got_repo_cache_tag(repo, id, *tag);
1339 const struct got_error *
1340 got_object_open_as_tag(struct got_tag_object **tag,
1341 struct got_repository *repo, struct got_object_id *id)
1343 *tag = got_repo_get_cached_tag(repo, id);
1349 return open_tag(tag, repo, id, 0);
1352 const struct got_error *
1353 got_object_tag_open(struct got_tag_object **tag,
1354 struct got_repository *repo, struct got_object *obj)
1356 return open_tag(tag, repo, got_object_get_id(obj), 1);
1360 got_object_tag_get_name(struct got_tag_object *tag)
1366 got_object_tag_get_object_type(struct got_tag_object *tag)
1368 return tag->obj_type;
1371 struct got_object_id *
1372 got_object_tag_get_object_id(struct got_tag_object *tag)
1377 static struct got_tree_entry *
1378 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1380 struct got_tree_entry *te;
1382 /* Note that tree entries are sorted in strncmp() order. */
1383 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1384 int cmp = strncmp(te->name, name, len);
1389 if (te->name[len] == '\0')
1395 const struct got_tree_entry *
1396 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1398 return find_entry_by_name(tree, name, strlen(name));
1401 const struct got_error *
1402 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1403 struct got_object_id *commit_id, const char *path)
1405 const struct got_error *err = NULL;
1406 struct got_commit_object *commit = NULL;
1407 struct got_tree_object *tree = NULL;
1408 struct got_tree_entry *te = NULL;
1409 const char *seg, *s;
1414 err = got_object_open_as_commit(&commit, repo, commit_id);
1418 /* Handle opening of root of commit's tree. */
1419 if (got_path_is_root_dir(path)) {
1420 *id = got_object_id_dup(commit->tree_id);
1422 err = got_error_from_errno("got_object_id_dup");
1426 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1436 struct got_tree_object *next_tree;
1445 te = find_entry_by_name(tree, seg, seglen);
1447 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1458 err = got_object_open_as_tree(&next_tree, repo,
1463 got_object_tree_close(tree);
1469 *id = got_object_id_dup(te->id);
1471 return got_error_from_errno("got_object_id_dup");
1473 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1476 got_object_commit_close(commit);
1478 got_object_tree_close(tree);
1482 const struct got_error *
1483 got_object_tree_path_changed(int *changed,
1484 struct got_tree_object *tree01, struct got_tree_object *tree02,
1485 const char *path, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1489 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1490 const char *seg, *s;
1495 /* We are expecting an absolute in-repository path. */
1497 return got_error(GOT_ERR_NOT_ABSPATH);
1499 /* We not do support comparing the root path. */
1500 if (path[1] == '\0')
1501 return got_error(GOT_ERR_BAD_PATH);
1506 s++; /* skip leading '/' */
1510 struct got_tree_object *next_tree1, *next_tree2;
1519 te1 = find_entry_by_name(tree1, seg, seglen);
1521 err = got_error(GOT_ERR_NO_OBJ);
1525 te2 = find_entry_by_name(tree2, seg, seglen);
1531 if (te1->mode != te2->mode) {
1536 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1541 if (*s == '\0') { /* final path element */
1550 err = got_object_open_as_tree(&next_tree1, repo,
1555 if (tree1 != tree01)
1556 got_object_tree_close(tree1);
1559 err = got_object_open_as_tree(&next_tree2, repo,
1564 if (tree2 != tree02)
1565 got_object_tree_close(tree2);
1570 if (tree1 && tree1 != tree01)
1571 got_object_tree_close(tree1);
1572 if (tree2 && tree2 != tree02)
1573 got_object_tree_close(tree2);
1577 const struct got_error *
1578 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1579 struct got_tree_entry *te)
1581 const struct got_error *err = NULL;
1583 *new_te = calloc(1, sizeof(**new_te));
1584 if (*new_te == NULL)
1585 return got_error_from_errno("calloc");
1587 (*new_te)->mode = te->mode;
1588 (*new_te)->name = strdup(te->name);
1589 if ((*new_te)->name == NULL) {
1590 err = got_error_from_errno("strdup");
1594 (*new_te)->id = got_object_id_dup(te->id);
1595 if ((*new_te)->id == NULL) {
1596 err = got_error_from_errno("got_object_id_dup");
1601 got_object_tree_entry_close(*new_te);