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;
584 char *path_packfile = 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);
630 const struct got_error *
631 got_object_open_as_commit(struct got_commit_object **commit,
632 struct got_repository *repo, struct got_object_id *id)
634 *commit = got_repo_get_cached_commit(repo, id);
635 if (*commit != NULL) {
640 return open_commit(commit, repo, id, 0);
643 const struct got_error *
644 got_object_commit_open(struct got_commit_object **commit,
645 struct got_repository *repo, struct got_object *obj)
647 return open_commit(commit, repo, got_object_get_id(obj), 1);
650 const struct got_error *
651 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
653 const struct got_error *err = NULL;
655 *qid = calloc(1, sizeof(**qid));
657 return got_error_from_errno("calloc");
659 (*qid)->id = got_object_id_dup(id);
660 if ((*qid)->id == NULL) {
661 err = got_error_from_errno("got_object_id_dup");
662 got_object_qid_free(*qid);
670 static const struct got_error *
671 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
672 int pack_idx, struct got_object_id *id)
674 const struct got_error *err = NULL;
676 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
681 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
684 static const struct got_error *
685 read_packed_tree_privsep(struct got_tree_object **tree,
686 struct got_pack *pack, struct got_packidx *packidx, int idx,
687 struct got_object_id *id)
689 const struct got_error *err = NULL;
691 if (pack->privsep_child)
692 return request_packed_tree(tree, pack, idx, id);
694 err = start_pack_privsep_child(pack, packidx);
698 return request_packed_tree(tree, pack, idx, id);
701 static const struct got_error *
702 request_tree(struct got_tree_object **tree, struct got_repository *repo,
705 const struct got_error *err = NULL;
706 struct imsgbuf *ibuf;
708 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
710 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
714 return got_privsep_recv_tree(tree, ibuf);
717 const struct got_error *
718 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
719 struct got_repository *repo)
723 struct imsgbuf *ibuf;
725 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
726 return request_tree(tree, repo, obj_fd);
728 ibuf = calloc(1, sizeof(*ibuf));
730 return got_error_from_errno("calloc");
732 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
733 return got_error_from_errno("socketpair");
737 return got_error_from_errno("fork");
739 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
744 if (close(imsg_fds[1]) != 0)
745 return got_error_from_errno("close");
746 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
748 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
749 imsg_init(ibuf, imsg_fds[0]);
750 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
753 return request_tree(tree, repo, obj_fd);
756 static const struct got_error *
757 open_tree(struct got_tree_object **tree, struct got_repository *repo,
758 struct got_object_id *id, int check_cache)
760 const struct got_error *err = NULL;
761 struct got_packidx *packidx = NULL;
763 char *path_packfile = NULL;
766 *tree = got_repo_get_cached_tree(repo, id);
774 err = got_repo_search_packidx(&packidx, &idx, repo, id);
776 struct got_pack *pack = NULL;
778 err = get_packfile_path(&path_packfile, packidx);
782 pack = got_repo_get_cached_pack(repo, path_packfile);
784 err = got_repo_cache_pack(&pack, repo, path_packfile,
789 err = read_packed_tree_privsep(tree, pack,
791 } else if (err->code == GOT_ERR_NO_OBJ) {
794 err = open_loose_object(&fd, id, repo);
797 err = read_tree_privsep(tree, fd, repo);
802 err = got_repo_cache_tree(repo, id, *tree);
809 const struct got_error *
810 got_object_open_as_tree(struct got_tree_object **tree,
811 struct got_repository *repo, struct got_object_id *id)
813 *tree = got_repo_get_cached_tree(repo, id);
819 return open_tree(tree, repo, id, 0);
822 const struct got_error *
823 got_object_tree_open(struct got_tree_object **tree,
824 struct got_repository *repo, struct got_object *obj)
826 return open_tree(tree, repo, got_object_get_id(obj), 1);
829 const struct got_tree_entries *
830 got_object_tree_get_entries(struct got_tree_object *tree)
832 return &tree->entries;
835 static const struct got_error *
836 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
837 struct got_pack *pack, struct got_packidx *packidx, int idx,
838 struct got_object_id *id)
840 const struct got_error *err = NULL;
842 int basefd, accumfd; /* temporary files for delta application */
844 basefd = got_opentempfd();
846 return got_error_from_errno("got_opentempfd");
847 accumfd = got_opentempfd();
849 return got_error_from_errno("got_opentempfd");
851 outfd_child = dup(outfd);
852 if (outfd_child == -1)
853 return got_error_from_errno("dup");
855 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
859 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
867 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
874 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
879 err = got_privsep_recv_blob(outbuf, size, hdrlen,
880 pack->privsep_child->ibuf);
884 if (lseek(outfd, SEEK_SET, 0) == -1)
885 err = got_error_from_errno("lseek");
890 static const struct got_error *
891 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
892 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
893 struct got_object_id *id)
895 const struct got_error *err = NULL;
897 if (pack->privsep_child == NULL) {
898 err = start_pack_privsep_child(pack, packidx);
903 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
907 static const struct got_error *
908 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
909 int infd, struct imsgbuf *ibuf)
911 const struct got_error *err = NULL;
914 outfd_child = dup(outfd);
915 if (outfd_child == -1)
916 return got_error_from_errno("dup");
918 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
922 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
926 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
930 if (lseek(outfd, SEEK_SET, 0) == -1)
931 return got_error_from_errno("lseek");
936 static const struct got_error *
937 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
938 int outfd, int infd, struct got_repository *repo)
942 struct imsgbuf *ibuf;
944 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
945 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
946 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
949 ibuf = calloc(1, sizeof(*ibuf));
951 return got_error_from_errno("calloc");
953 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
954 return got_error_from_errno("socketpair");
958 return got_error_from_errno("fork");
960 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
965 if (close(imsg_fds[1]) != 0)
966 return got_error_from_errno("close");
967 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
969 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
970 imsg_init(ibuf, imsg_fds[0]);
971 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
973 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
976 static const struct got_error *
977 open_blob(struct got_blob_object **blob, struct got_repository *repo,
978 struct got_object_id *id, size_t blocksize)
980 const struct got_error *err = NULL;
981 struct got_packidx *packidx = NULL;
983 char *path_packfile = NULL;
989 *blob = calloc(1, sizeof(**blob));
991 return got_error_from_errno("calloc");
993 outfd = got_opentempfd();
995 return got_error_from_errno("got_opentempfd");
997 (*blob)->read_buf = malloc(blocksize);
998 if ((*blob)->read_buf == NULL) {
999 err = got_error_from_errno("malloc");
1003 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1005 struct got_pack *pack = NULL;
1007 err = get_packfile_path(&path_packfile, packidx);
1011 pack = got_repo_get_cached_pack(repo, path_packfile);
1013 err = got_repo_cache_pack(&pack, repo, path_packfile,
1018 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1019 pack, packidx, idx, id);
1020 } else if (err->code == GOT_ERR_NO_OBJ) {
1023 err = open_loose_object(&infd, id, repo);
1026 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1032 if (hdrlen > size) {
1033 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1038 if (close(outfd) != 0 && err == NULL)
1039 err = got_error_from_errno("close");
1041 (*blob)->f = fmemopen(outbuf, size, "rb");
1042 if ((*blob)->f == NULL) {
1043 err = got_error_from_errno("fmemopen");
1047 (*blob)->data = outbuf;
1049 if (fstat(outfd, &sb) == -1) {
1050 err = got_error_from_errno("fstat");
1054 if (sb.st_size != size) {
1055 err = got_error(GOT_ERR_PRIVSEP_LEN);
1059 (*blob)->f = fdopen(outfd, "rb");
1060 if ((*blob)->f == NULL) {
1061 err = got_error_from_errno("fdopen");
1068 (*blob)->hdrlen = hdrlen;
1069 (*blob)->blocksize = blocksize;
1070 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1073 free(path_packfile);
1076 got_object_blob_close(*blob);
1078 } else if (outfd != -1)
1084 const struct got_error *
1085 got_object_open_as_blob(struct got_blob_object **blob,
1086 struct got_repository *repo, struct got_object_id *id,
1089 return open_blob(blob, repo, id, blocksize);
1092 const struct got_error *
1093 got_object_blob_open(struct got_blob_object **blob,
1094 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1096 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1099 const struct got_error *
1100 got_object_blob_close(struct got_blob_object *blob)
1102 const struct got_error *err = NULL;
1103 free(blob->read_buf);
1104 if (blob->f && fclose(blob->f) != 0)
1105 err = got_error_from_errno("fclose");
1112 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1114 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1118 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1120 return blob->hdrlen;
1124 got_object_blob_get_read_buf(struct got_blob_object *blob)
1126 return blob->read_buf;
1129 const struct got_error *
1130 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1134 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1135 if (n == 0 && ferror(blob->f))
1136 return got_ferror(blob->f, GOT_ERR_IO);
1141 const struct got_error *
1142 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1143 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1145 const struct got_error *err = NULL;
1146 size_t n, len, hdrlen;
1149 size_t noffsets = 0;
1150 off_t off = 0, total_len = 0;
1153 *line_offsets = NULL;
1159 hdrlen = got_object_blob_get_hdrlen(blob);
1161 err = got_object_blob_read_block(&len, blob);
1166 buf = got_object_blob_get_read_buf(blob);
1168 if (line_offsets && nlines) {
1169 if (*line_offsets == NULL) {
1170 /* Have some data but perhaps no '\n'. */
1173 *line_offsets = calloc(1, sizeof(**line_offsets));
1174 if (*line_offsets == NULL)
1175 return got_error_from_errno("malloc");
1177 /* Skip forward over end of first line. */
1184 /* Scan '\n' offsets in remaining chunk of data. */
1186 if (buf[i] != '\n') {
1191 if (noffsets < *nlines) {
1192 off_t *o = recallocarray(*line_offsets,
1194 sizeof(**line_offsets));
1196 free(*line_offsets);
1197 *line_offsets = NULL;
1198 return got_error_from_errno(
1204 off = total_len + i - hdrlen + 1;
1205 (*line_offsets)[*nlines - 1] = off;
1209 /* Skip blob object header first time around. */
1210 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1211 if (n != len - hdrlen)
1212 return got_ferror(outfile, GOT_ERR_IO);
1213 total_len += len - hdrlen;
1217 if (fflush(outfile) != 0)
1218 return got_error_from_errno("fflush");
1222 *filesize = total_len;
1227 static const struct got_error *
1228 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1229 int pack_idx, struct got_object_id *id)
1231 const struct got_error *err = NULL;
1233 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1238 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1241 static const struct got_error *
1242 read_packed_tag_privsep(struct got_tag_object **tag,
1243 struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 struct got_object_id *id)
1246 const struct got_error *err = NULL;
1248 if (pack->privsep_child)
1249 return request_packed_tag(tag, pack, idx, id);
1251 err = start_pack_privsep_child(pack, packidx);
1255 return request_packed_tag(tag, pack, idx, id);
1258 static const struct got_error *
1259 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1262 const struct got_error *err = NULL;
1263 struct imsgbuf *ibuf;
1265 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1267 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1271 return got_privsep_recv_tag(tag, ibuf);
1274 static const struct got_error *
1275 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1276 struct got_repository *repo)
1280 struct imsgbuf *ibuf;
1282 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1283 return request_tag(tag, repo, obj_fd);
1285 ibuf = calloc(1, sizeof(*ibuf));
1287 return got_error_from_errno("calloc");
1289 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1290 return got_error_from_errno("socketpair");
1294 return got_error_from_errno("fork");
1295 else if (pid == 0) {
1296 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1301 if (close(imsg_fds[1]) != 0)
1302 return got_error_from_errno("close");
1303 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1305 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1306 imsg_init(ibuf, imsg_fds[0]);
1307 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1309 return request_tag(tag, repo, obj_fd);
1312 static const struct got_error *
1313 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1314 struct got_object_id *id, int check_cache)
1316 const struct got_error *err = NULL;
1317 struct got_packidx *packidx = NULL;
1319 char *path_packfile = NULL;
1320 struct got_object *obj = NULL;
1321 int obj_type = GOT_OBJ_TYPE_ANY;
1324 *tag = got_repo_get_cached_tag(repo, id);
1332 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1334 struct got_pack *pack = NULL;
1336 err = get_packfile_path(&path_packfile, packidx);
1340 pack = got_repo_get_cached_pack(repo, path_packfile);
1342 err = got_repo_cache_pack(&pack, repo, path_packfile,
1348 /* Beware of "leightweight" tags: Check object type first. */
1349 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1353 obj_type = obj->type;
1354 got_object_close(obj);
1355 if (obj_type != GOT_OBJ_TYPE_TAG) {
1356 err = got_error(GOT_ERR_OBJ_TYPE);
1359 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1360 } else if (err->code == GOT_ERR_NO_OBJ) {
1363 err = open_loose_object(&fd, id, repo);
1366 err = read_object_header_privsep(&obj, repo, fd);
1369 obj_type = obj->type;
1370 got_object_close(obj);
1371 if (obj_type != GOT_OBJ_TYPE_TAG)
1372 return got_error(GOT_ERR_OBJ_TYPE);
1374 err = open_loose_object(&fd, id, repo);
1377 err = read_tag_privsep(tag, fd, repo);
1382 err = got_repo_cache_tag(repo, id, *tag);
1385 free(path_packfile);
1389 const struct got_error *
1390 got_object_open_as_tag(struct got_tag_object **tag,
1391 struct got_repository *repo, struct got_object_id *id)
1393 *tag = got_repo_get_cached_tag(repo, id);
1399 return open_tag(tag, repo, id, 0);
1402 const struct got_error *
1403 got_object_tag_open(struct got_tag_object **tag,
1404 struct got_repository *repo, struct got_object *obj)
1406 return open_tag(tag, repo, got_object_get_id(obj), 1);
1410 got_object_tag_get_name(struct got_tag_object *tag)
1416 got_object_tag_get_object_type(struct got_tag_object *tag)
1418 return tag->obj_type;
1421 struct got_object_id *
1422 got_object_tag_get_object_id(struct got_tag_object *tag)
1428 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1430 return tag->tagger_time;
1434 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1436 return tag->tagger_gmtoff;
1440 got_object_tag_get_tagger(struct got_tag_object *tag)
1446 got_object_tag_get_message(struct got_tag_object *tag)
1451 static struct got_tree_entry *
1452 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1454 struct got_tree_entry *te;
1456 /* Note that tree entries are sorted in strncmp() order. */
1457 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1458 int cmp = strncmp(te->name, name, len);
1463 if (te->name[len] == '\0')
1469 const struct got_tree_entry *
1470 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1472 return find_entry_by_name(tree, name, strlen(name));
1475 const struct got_error *
1476 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1477 struct got_object_id *commit_id, const char *path)
1479 const struct got_error *err = NULL;
1480 struct got_commit_object *commit = NULL;
1481 struct got_tree_object *tree = NULL;
1482 struct got_tree_entry *te = NULL;
1483 const char *seg, *s;
1488 err = got_object_open_as_commit(&commit, repo, commit_id);
1492 /* Handle opening of root of commit's tree. */
1493 if (got_path_is_root_dir(path)) {
1494 *id = got_object_id_dup(commit->tree_id);
1496 err = got_error_from_errno("got_object_id_dup");
1500 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1510 struct got_tree_object *next_tree;
1519 te = find_entry_by_name(tree, seg, seglen);
1521 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1532 err = got_object_open_as_tree(&next_tree, repo,
1537 got_object_tree_close(tree);
1543 *id = got_object_id_dup(te->id);
1545 return got_error_from_errno("got_object_id_dup");
1547 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1550 got_object_commit_close(commit);
1552 got_object_tree_close(tree);
1556 const struct got_error *
1557 got_object_tree_path_changed(int *changed,
1558 struct got_tree_object *tree01, struct got_tree_object *tree02,
1559 const char *path, struct got_repository *repo)
1561 const struct got_error *err = NULL;
1562 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1563 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1564 const char *seg, *s;
1569 /* We are expecting an absolute in-repository path. */
1571 return got_error(GOT_ERR_NOT_ABSPATH);
1573 /* We not do support comparing the root path. */
1574 if (path[1] == '\0')
1575 return got_error(GOT_ERR_BAD_PATH);
1580 s++; /* skip leading '/' */
1584 struct got_tree_object *next_tree1, *next_tree2;
1593 te1 = find_entry_by_name(tree1, seg, seglen);
1595 err = got_error(GOT_ERR_NO_OBJ);
1599 te2 = find_entry_by_name(tree2, seg, seglen);
1605 if (te1->mode != te2->mode) {
1610 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1615 if (*s == '\0') { /* final path element */
1624 err = got_object_open_as_tree(&next_tree1, repo,
1629 if (tree1 != tree01)
1630 got_object_tree_close(tree1);
1633 err = got_object_open_as_tree(&next_tree2, repo,
1638 if (tree2 != tree02)
1639 got_object_tree_close(tree2);
1644 if (tree1 && tree1 != tree01)
1645 got_object_tree_close(tree1);
1646 if (tree2 && tree2 != tree02)
1647 got_object_tree_close(tree2);
1651 const struct got_error *
1652 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1653 struct got_tree_entry *te)
1655 const struct got_error *err = NULL;
1657 *new_te = calloc(1, sizeof(**new_te));
1658 if (*new_te == NULL)
1659 return got_error_from_errno("calloc");
1661 (*new_te)->mode = te->mode;
1662 (*new_te)->name = strdup(te->name);
1663 if ((*new_te)->name == NULL) {
1664 err = got_error_from_errno("strdup");
1668 (*new_te)->id = got_object_id_dup(te->id);
1669 if ((*new_te)->id == NULL) {
1670 err = got_error_from_errno("got_object_id_dup");
1675 got_object_tree_entry_close(*new_te);
1682 got_object_tree_entry_is_submodule(const struct got_tree_entry *te)
1684 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);