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_get_id(struct got_object *obj)
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 return got_object_id_str(outbuf, &obj->id);
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
76 const struct got_error *err = NULL;
77 struct got_object *obj;
79 err = got_object_open(&obj, repo, id);
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
91 err = got_error(GOT_ERR_OBJ_TYPE);
95 got_object_close(obj);
99 const struct got_error *
100 got_object_get_path(char **path, struct got_object_id *id,
101 struct got_repository *repo)
103 const struct got_error *err = NULL;
109 path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno("got_repo_get_path_objects");
113 err = got_object_id_str(&hex, id);
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
127 static const struct got_error *
128 open_loose_object(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
134 err = got_object_get_path(&path, id, repo);
137 *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_from_errno2("open", path);
147 static const struct got_error *
148 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
152 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
153 size = strlen(packidx->path_packidx) + 2;
154 if (size < GOT_PACKFILE_NAMELEN + 1)
155 return got_error(GOT_ERR_BAD_PATH);
157 *path_packfile = malloc(size);
158 if (*path_packfile == NULL)
159 return got_error_from_errno("malloc");
161 /* Copy up to and excluding ".idx". */
162 if (strlcpy(*path_packfile, packidx->path_packidx,
163 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
164 return got_error(GOT_ERR_NO_SPACE);
166 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
167 return got_error(GOT_ERR_NO_SPACE);
172 static const struct got_error *
173 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
174 struct got_object_id *id)
176 const struct got_error *err = NULL;
177 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
179 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
183 err = got_privsep_recv_obj(obj, ibuf);
187 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
193 set_max_datasize(void)
197 if (getrlimit(RLIMIT_DATA, &rl) != 0)
200 rl.rlim_cur = rl.rlim_max;
201 setrlimit(RLIMIT_DATA, &rl);
204 static const struct got_error *
205 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
207 const struct got_error *err = NULL;
210 struct imsgbuf *ibuf;
212 ibuf = calloc(1, sizeof(*ibuf));
214 return got_error_from_errno("calloc");
216 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
217 if (pack->privsep_child == NULL) {
218 err = got_error_from_errno("calloc");
223 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
224 err = got_error_from_errno("socketpair");
230 err = got_error_from_errno("fork");
232 } else if (pid == 0) {
234 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
235 pack->path_packfile);
239 if (close(imsg_fds[1]) != 0)
240 return got_error_from_errno("close");
241 pack->privsep_child->imsg_fd = imsg_fds[0];
242 pack->privsep_child->pid = pid;
243 imsg_init(ibuf, imsg_fds[0]);
244 pack->privsep_child->ibuf = ibuf;
246 err = got_privsep_init_pack_child(ibuf, pack, packidx);
248 const struct got_error *child_err;
249 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
250 child_err = got_privsep_wait_for_child(
251 pack->privsep_child->pid);
252 if (child_err && err == NULL)
258 free(pack->privsep_child);
259 pack->privsep_child = NULL;
264 static const struct got_error *
265 read_packed_object_privsep(struct got_object **obj,
266 struct got_repository *repo, struct got_pack *pack,
267 struct got_packidx *packidx, int idx, struct got_object_id *id)
269 const struct got_error *err = NULL;
271 if (pack->privsep_child)
272 return request_packed_object(obj, pack, idx, id);
274 err = start_pack_privsep_child(pack, packidx);
278 return request_packed_object(obj, pack, idx, id);
282 static const struct got_error *
283 open_packed_object(struct got_object **obj, struct got_object_id *id,
284 struct got_repository *repo)
286 const struct got_error *err = NULL;
287 struct got_pack *pack = NULL;
288 struct got_packidx *packidx = NULL;
292 err = got_repo_search_packidx(&packidx, &idx, repo, id);
296 err = get_packfile_path(&path_packfile, packidx);
300 pack = got_repo_get_cached_pack(repo, path_packfile);
302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
307 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
315 static const struct got_error *
316 request_object(struct got_object **obj, struct got_repository *repo, int fd)
318 const struct got_error *err = NULL;
319 struct imsgbuf *ibuf;
321 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
323 err = got_privsep_send_obj_req(ibuf, fd);
327 return got_privsep_recv_obj(obj, ibuf);
330 static const struct got_error *
331 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
334 const struct got_error *err;
337 struct imsgbuf *ibuf;
339 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
340 return request_object(obj, repo, obj_fd);
342 ibuf = calloc(1, sizeof(*ibuf));
344 return got_error_from_errno("calloc");
346 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
347 err = got_error_from_errno("socketpair");
354 err = got_error_from_errno("fork");
359 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
364 if (close(imsg_fds[1]) != 0) {
365 err = got_error_from_errno("close");
369 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
371 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
372 imsg_init(ibuf, imsg_fds[0]);
373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
375 return request_object(obj, repo, obj_fd);
379 const struct got_error *
380 got_object_open(struct got_object **obj, struct got_repository *repo,
381 struct got_object_id *id)
383 const struct got_error *err = NULL;
387 *obj = got_repo_get_cached_object(repo, id);
393 err = open_packed_object(obj, id, repo);
394 if (err && err->code != GOT_ERR_NO_OBJ)
398 return got_repo_cache_object(repo, id, *obj);
401 err = got_object_get_path(&path, id, repo);
405 fd = open(path, O_RDONLY | O_NOFOLLOW);
408 err = got_error_no_obj(id);
410 err = got_error_from_errno2("open", path);
413 err = read_object_header_privsep(obj, repo, fd);
416 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
420 err = got_repo_cache_object(repo, id, *obj);
427 const struct got_error *
428 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
431 struct got_object_id id;
433 if (!got_parse_sha1_digest(id.sha1, id_str))
434 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
436 return got_object_open(obj, repo, &id);
439 const struct got_error *
440 got_object_resolve_id_str(struct got_object_id **id,
441 struct got_repository *repo, const char *id_str)
443 const struct got_error *err = NULL;
444 struct got_object *obj;
446 err = got_object_open_by_id_str(&obj, repo, id_str);
450 *id = got_object_id_dup(got_object_get_id(obj));
451 got_object_close(obj);
453 return got_error_from_errno("got_object_id_dup");
458 static const struct got_error *
459 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
460 int pack_idx, struct got_object_id *id)
462 const struct got_error *err = NULL;
464 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
469 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
473 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
477 static const struct got_error *
478 read_packed_commit_privsep(struct got_commit_object **commit,
479 struct got_pack *pack, struct got_packidx *packidx, int idx,
480 struct got_object_id *id)
482 const struct got_error *err = NULL;
484 if (pack->privsep_child)
485 return request_packed_commit(commit, pack, idx, id);
487 err = start_pack_privsep_child(pack, packidx);
491 return request_packed_commit(commit, pack, idx, id);
494 static const struct got_error *
495 request_commit(struct got_commit_object **commit, struct got_repository *repo,
498 const struct got_error *err = NULL;
499 struct imsgbuf *ibuf;
501 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
503 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
507 return got_privsep_recv_commit(commit, ibuf);
510 static const struct got_error *
511 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
512 struct got_repository *repo)
514 const struct got_error *err;
517 struct imsgbuf *ibuf;
519 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
520 return request_commit(commit, repo, obj_fd);
522 ibuf = calloc(1, sizeof(*ibuf));
524 return got_error_from_errno("calloc");
526 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
527 err = got_error_from_errno("socketpair");
534 err = got_error_from_errno("fork");
539 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
544 if (close(imsg_fds[1]) != 0) {
545 err = got_error_from_errno("close");
549 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
551 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
552 imsg_init(ibuf, imsg_fds[0]);
553 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
555 return request_commit(commit, repo, obj_fd);
559 static const struct got_error *
560 open_commit(struct got_commit_object **commit,
561 struct got_repository *repo, struct got_object_id *id, int check_cache)
563 const struct got_error *err = NULL;
564 struct got_packidx *packidx = NULL;
566 char *path_packfile = NULL;
569 *commit = got_repo_get_cached_commit(repo, id);
570 if (*commit != NULL) {
577 err = got_repo_search_packidx(&packidx, &idx, repo, id);
579 struct got_pack *pack = NULL;
581 err = get_packfile_path(&path_packfile, packidx);
585 pack = got_repo_get_cached_pack(repo, path_packfile);
587 err = got_repo_cache_pack(&pack, repo, path_packfile,
592 err = read_packed_commit_privsep(commit, pack,
594 } else if (err->code == GOT_ERR_NO_OBJ) {
597 err = open_loose_object(&fd, id, repo);
600 err = read_commit_privsep(commit, fd, repo);
605 err = got_repo_cache_commit(repo, id, *commit);
612 const struct got_error *
613 got_object_open_as_commit(struct got_commit_object **commit,
614 struct got_repository *repo, struct got_object_id *id)
616 *commit = got_repo_get_cached_commit(repo, id);
617 if (*commit != NULL) {
622 return open_commit(commit, repo, id, 0);
625 const struct got_error *
626 got_object_commit_open(struct got_commit_object **commit,
627 struct got_repository *repo, struct got_object *obj)
629 return open_commit(commit, repo, got_object_get_id(obj), 1);
632 const struct got_error *
633 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
635 const struct got_error *err = NULL;
637 *qid = calloc(1, sizeof(**qid));
639 return got_error_from_errno("calloc");
641 (*qid)->id = got_object_id_dup(id);
642 if ((*qid)->id == NULL) {
643 err = got_error_from_errno("got_object_id_dup");
644 got_object_qid_free(*qid);
652 static const struct got_error *
653 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
654 int pack_idx, struct got_object_id *id)
656 const struct got_error *err = NULL;
658 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
663 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
666 static const struct got_error *
667 read_packed_tree_privsep(struct got_tree_object **tree,
668 struct got_pack *pack, struct got_packidx *packidx, int idx,
669 struct got_object_id *id)
671 const struct got_error *err = NULL;
673 if (pack->privsep_child)
674 return request_packed_tree(tree, pack, idx, id);
676 err = start_pack_privsep_child(pack, packidx);
680 return request_packed_tree(tree, pack, idx, id);
683 static const struct got_error *
684 request_tree(struct got_tree_object **tree, struct got_repository *repo,
687 const struct got_error *err = NULL;
688 struct imsgbuf *ibuf;
690 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
692 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
696 return got_privsep_recv_tree(tree, ibuf);
699 const struct got_error *
700 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
701 struct got_repository *repo)
703 const struct got_error *err;
706 struct imsgbuf *ibuf;
708 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
709 return request_tree(tree, repo, obj_fd);
711 ibuf = calloc(1, sizeof(*ibuf));
713 return got_error_from_errno("calloc");
715 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
716 err = got_error_from_errno("socketpair");
723 err = got_error_from_errno("fork");
728 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
733 if (close(imsg_fds[1]) != 0) {
734 err = got_error_from_errno("close");
738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
741 imsg_init(ibuf, imsg_fds[0]);
742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
745 return request_tree(tree, repo, obj_fd);
748 static const struct got_error *
749 open_tree(struct got_tree_object **tree, struct got_repository *repo,
750 struct got_object_id *id, int check_cache)
752 const struct got_error *err = NULL;
753 struct got_packidx *packidx = NULL;
755 char *path_packfile = NULL;
758 *tree = got_repo_get_cached_tree(repo, id);
766 err = got_repo_search_packidx(&packidx, &idx, repo, id);
768 struct got_pack *pack = NULL;
770 err = get_packfile_path(&path_packfile, packidx);
774 pack = got_repo_get_cached_pack(repo, path_packfile);
776 err = got_repo_cache_pack(&pack, repo, path_packfile,
781 err = read_packed_tree_privsep(tree, pack,
783 } else if (err->code == GOT_ERR_NO_OBJ) {
786 err = open_loose_object(&fd, id, repo);
789 err = read_tree_privsep(tree, fd, repo);
794 err = got_repo_cache_tree(repo, id, *tree);
801 const struct got_error *
802 got_object_open_as_tree(struct got_tree_object **tree,
803 struct got_repository *repo, struct got_object_id *id)
805 *tree = got_repo_get_cached_tree(repo, id);
811 return open_tree(tree, repo, id, 0);
814 const struct got_error *
815 got_object_tree_open(struct got_tree_object **tree,
816 struct got_repository *repo, struct got_object *obj)
818 return open_tree(tree, repo, got_object_get_id(obj), 1);
822 got_object_tree_get_nentries(struct got_tree_object *tree)
824 return tree->nentries;
827 struct got_tree_entry *
828 got_object_tree_get_first_entry(struct got_tree_object *tree)
830 return got_object_tree_get_entry(tree, 0);
833 struct got_tree_entry *
834 got_object_tree_get_last_entry(struct got_tree_object *tree)
836 return got_object_tree_get_entry(tree, tree->nentries - 1);
839 struct got_tree_entry *
840 got_object_tree_get_entry(struct got_tree_object *tree, int i)
842 if (i < 0 || i >= tree->nentries)
844 return &tree->entries[i];
848 got_tree_entry_get_mode(struct got_tree_entry *te)
854 got_tree_entry_get_name(struct got_tree_entry *te)
859 struct got_object_id *
860 got_tree_entry_get_id(struct got_tree_entry *te)
866 got_tree_entry_get_index(struct got_tree_entry *te)
871 struct got_tree_entry *
872 got_tree_entry_get_next(struct got_tree_object *tree,
873 struct got_tree_entry *te)
875 return got_object_tree_get_entry(tree, te->idx + 1);
878 struct got_tree_entry *
879 got_tree_entry_get_prev(struct got_tree_object *tree,
880 struct got_tree_entry *te)
882 return got_object_tree_get_entry(tree, te->idx - 1);
885 static const struct got_error *
886 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
887 struct got_pack *pack, struct got_packidx *packidx, int idx,
888 struct got_object_id *id)
890 const struct got_error *err = NULL;
892 int basefd, accumfd; /* temporary files for delta application */
894 basefd = got_opentempfd();
896 return got_error_from_errno("got_opentempfd");
897 accumfd = got_opentempfd();
899 return got_error_from_errno("got_opentempfd");
901 outfd_child = dup(outfd);
902 if (outfd_child == -1)
903 return got_error_from_errno("dup");
905 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
909 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
917 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
924 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
929 err = got_privsep_recv_blob(outbuf, size, hdrlen,
930 pack->privsep_child->ibuf);
934 if (lseek(outfd, SEEK_SET, 0) == -1)
935 err = got_error_from_errno("lseek");
940 static const struct got_error *
941 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
942 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
943 struct got_object_id *id)
945 const struct got_error *err = NULL;
947 if (pack->privsep_child == NULL) {
948 err = start_pack_privsep_child(pack, packidx);
953 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
957 static const struct got_error *
958 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
959 int infd, struct imsgbuf *ibuf)
961 const struct got_error *err = NULL;
964 outfd_child = dup(outfd);
965 if (outfd_child == -1)
966 return got_error_from_errno("dup");
968 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
972 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
976 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
980 if (lseek(outfd, SEEK_SET, 0) == -1)
981 return got_error_from_errno("lseek");
986 static const struct got_error *
987 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
988 int outfd, int infd, struct got_repository *repo)
990 const struct got_error *err;
993 struct imsgbuf *ibuf;
995 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
996 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
997 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1000 ibuf = calloc(1, sizeof(*ibuf));
1002 return got_error_from_errno("calloc");
1004 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1005 err = got_error_from_errno("socketpair");
1012 err = got_error_from_errno("fork");
1016 else if (pid == 0) {
1017 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1022 if (close(imsg_fds[1]) != 0) {
1023 err = got_error_from_errno("close");
1027 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1029 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1030 imsg_init(ibuf, imsg_fds[0]);
1031 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1033 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1036 static const struct got_error *
1037 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1038 struct got_object_id *id, size_t blocksize)
1040 const struct got_error *err = NULL;
1041 struct got_packidx *packidx = NULL;
1043 char *path_packfile = NULL;
1046 size_t size, hdrlen;
1049 *blob = calloc(1, sizeof(**blob));
1051 return got_error_from_errno("calloc");
1053 outfd = got_opentempfd();
1055 return got_error_from_errno("got_opentempfd");
1057 (*blob)->read_buf = malloc(blocksize);
1058 if ((*blob)->read_buf == NULL) {
1059 err = got_error_from_errno("malloc");
1063 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1065 struct got_pack *pack = NULL;
1067 err = get_packfile_path(&path_packfile, packidx);
1071 pack = got_repo_get_cached_pack(repo, path_packfile);
1073 err = got_repo_cache_pack(&pack, repo, path_packfile,
1078 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1079 pack, packidx, idx, id);
1080 } else if (err->code == GOT_ERR_NO_OBJ) {
1083 err = open_loose_object(&infd, id, repo);
1086 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1092 if (hdrlen > size) {
1093 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1098 if (close(outfd) != 0 && err == NULL)
1099 err = got_error_from_errno("close");
1101 (*blob)->f = fmemopen(outbuf, size, "rb");
1102 if ((*blob)->f == NULL) {
1103 err = got_error_from_errno("fmemopen");
1107 (*blob)->data = outbuf;
1109 if (fstat(outfd, &sb) == -1) {
1110 err = got_error_from_errno("fstat");
1114 if (sb.st_size != size) {
1115 err = got_error(GOT_ERR_PRIVSEP_LEN);
1119 (*blob)->f = fdopen(outfd, "rb");
1120 if ((*blob)->f == NULL) {
1121 err = got_error_from_errno("fdopen");
1128 (*blob)->hdrlen = hdrlen;
1129 (*blob)->blocksize = blocksize;
1130 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1133 free(path_packfile);
1136 got_object_blob_close(*blob);
1138 } else if (outfd != -1)
1144 const struct got_error *
1145 got_object_open_as_blob(struct got_blob_object **blob,
1146 struct got_repository *repo, struct got_object_id *id,
1149 return open_blob(blob, repo, id, blocksize);
1152 const struct got_error *
1153 got_object_blob_open(struct got_blob_object **blob,
1154 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1156 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1159 const struct got_error *
1160 got_object_blob_close(struct got_blob_object *blob)
1162 const struct got_error *err = NULL;
1163 free(blob->read_buf);
1164 if (blob->f && fclose(blob->f) != 0)
1165 err = got_error_from_errno("fclose");
1172 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1174 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1178 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1180 return blob->hdrlen;
1184 got_object_blob_get_read_buf(struct got_blob_object *blob)
1186 return blob->read_buf;
1189 const struct got_error *
1190 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1194 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1195 if (n == 0 && ferror(blob->f))
1196 return got_ferror(blob->f, GOT_ERR_IO);
1201 const struct got_error *
1202 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1203 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1205 const struct got_error *err = NULL;
1206 size_t n, len, hdrlen;
1209 size_t noffsets = 0;
1210 off_t off = 0, total_len = 0;
1213 *line_offsets = NULL;
1219 hdrlen = got_object_blob_get_hdrlen(blob);
1221 err = got_object_blob_read_block(&len, blob);
1226 buf = got_object_blob_get_read_buf(blob);
1228 if (line_offsets && nlines) {
1229 if (*line_offsets == NULL) {
1230 /* Have some data but perhaps no '\n'. */
1233 *line_offsets = calloc(1, sizeof(**line_offsets));
1234 if (*line_offsets == NULL)
1235 return got_error_from_errno("calloc");
1237 /* Skip forward over end of first line. */
1244 /* Scan '\n' offsets in remaining chunk of data. */
1246 if (buf[i] != '\n') {
1251 if (noffsets < *nlines) {
1252 off_t *o = recallocarray(*line_offsets,
1254 sizeof(**line_offsets));
1256 free(*line_offsets);
1257 *line_offsets = NULL;
1258 return got_error_from_errno(
1264 off = total_len + i - hdrlen + 1;
1265 (*line_offsets)[*nlines - 1] = off;
1269 /* Skip blob object header first time around. */
1270 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1271 if (n != len - hdrlen)
1272 return got_ferror(outfile, GOT_ERR_IO);
1273 total_len += len - hdrlen;
1277 if (fflush(outfile) != 0)
1278 return got_error_from_errno("fflush");
1282 *filesize = total_len;
1287 static const struct got_error *
1288 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1289 int pack_idx, struct got_object_id *id)
1291 const struct got_error *err = NULL;
1293 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1298 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1301 static const struct got_error *
1302 read_packed_tag_privsep(struct got_tag_object **tag,
1303 struct got_pack *pack, struct got_packidx *packidx, int idx,
1304 struct got_object_id *id)
1306 const struct got_error *err = NULL;
1308 if (pack->privsep_child)
1309 return request_packed_tag(tag, pack, idx, id);
1311 err = start_pack_privsep_child(pack, packidx);
1315 return request_packed_tag(tag, pack, idx, id);
1318 static const struct got_error *
1319 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1322 const struct got_error *err = NULL;
1323 struct imsgbuf *ibuf;
1325 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1327 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1331 return got_privsep_recv_tag(tag, ibuf);
1334 static const struct got_error *
1335 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1336 struct got_repository *repo)
1338 const struct got_error *err;
1341 struct imsgbuf *ibuf;
1343 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1344 return request_tag(tag, repo, obj_fd);
1346 ibuf = calloc(1, sizeof(*ibuf));
1348 return got_error_from_errno("calloc");
1350 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1351 err = got_error_from_errno("socketpair");
1358 err = got_error_from_errno("fork");
1362 else if (pid == 0) {
1363 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1368 if (close(imsg_fds[1]) != 0) {
1369 err = got_error_from_errno("close");
1373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1375 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1376 imsg_init(ibuf, imsg_fds[0]);
1377 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1379 return request_tag(tag, repo, obj_fd);
1382 static const struct got_error *
1383 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1384 struct got_object_id *id, int check_cache)
1386 const struct got_error *err = NULL;
1387 struct got_packidx *packidx = NULL;
1389 char *path_packfile = NULL;
1390 struct got_object *obj = NULL;
1391 int obj_type = GOT_OBJ_TYPE_ANY;
1394 *tag = got_repo_get_cached_tag(repo, id);
1402 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1404 struct got_pack *pack = NULL;
1406 err = get_packfile_path(&path_packfile, packidx);
1410 pack = got_repo_get_cached_pack(repo, path_packfile);
1412 err = got_repo_cache_pack(&pack, repo, path_packfile,
1418 /* Beware of "lightweight" tags: Check object type first. */
1419 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1423 obj_type = obj->type;
1424 got_object_close(obj);
1425 if (obj_type != GOT_OBJ_TYPE_TAG) {
1426 err = got_error(GOT_ERR_OBJ_TYPE);
1429 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1430 } else if (err->code == GOT_ERR_NO_OBJ) {
1433 err = open_loose_object(&fd, id, repo);
1436 err = read_object_header_privsep(&obj, repo, fd);
1439 obj_type = obj->type;
1440 got_object_close(obj);
1441 if (obj_type != GOT_OBJ_TYPE_TAG)
1442 return got_error(GOT_ERR_OBJ_TYPE);
1444 err = open_loose_object(&fd, id, repo);
1447 err = read_tag_privsep(tag, fd, repo);
1452 err = got_repo_cache_tag(repo, id, *tag);
1455 free(path_packfile);
1459 const struct got_error *
1460 got_object_open_as_tag(struct got_tag_object **tag,
1461 struct got_repository *repo, struct got_object_id *id)
1463 *tag = got_repo_get_cached_tag(repo, id);
1469 return open_tag(tag, repo, id, 0);
1472 const struct got_error *
1473 got_object_tag_open(struct got_tag_object **tag,
1474 struct got_repository *repo, struct got_object *obj)
1476 return open_tag(tag, repo, got_object_get_id(obj), 1);
1480 got_object_tag_get_name(struct got_tag_object *tag)
1486 got_object_tag_get_object_type(struct got_tag_object *tag)
1488 return tag->obj_type;
1491 struct got_object_id *
1492 got_object_tag_get_object_id(struct got_tag_object *tag)
1498 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1500 return tag->tagger_time;
1504 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1506 return tag->tagger_gmtoff;
1510 got_object_tag_get_tagger(struct got_tag_object *tag)
1516 got_object_tag_get_message(struct got_tag_object *tag)
1521 static struct got_tree_entry *
1522 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1526 /* Note that tree entries are sorted in strncmp() order. */
1527 for (i = 0; i < tree->nentries; i++) {
1528 struct got_tree_entry *te = &tree->entries[i];
1529 int cmp = strncmp(te->name, name, len);
1534 if (te->name[len] == '\0')
1540 struct got_tree_entry *
1541 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1543 return find_entry_by_name(tree, name, strlen(name));
1546 const struct got_error *
1547 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1548 struct got_object_id *commit_id, const char *path)
1550 const struct got_error *err = NULL;
1551 struct got_commit_object *commit = NULL;
1552 struct got_tree_object *tree = NULL;
1553 struct got_tree_entry *te = NULL;
1554 const char *seg, *s;
1559 err = got_object_open_as_commit(&commit, repo, commit_id);
1563 /* Handle opening of root of commit's tree. */
1564 if (got_path_is_root_dir(path)) {
1565 *id = got_object_id_dup(commit->tree_id);
1567 err = got_error_from_errno("got_object_id_dup");
1571 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1581 struct got_tree_object *next_tree;
1590 te = find_entry_by_name(tree, seg, seglen);
1592 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1603 err = got_object_open_as_tree(&next_tree, repo,
1608 got_object_tree_close(tree);
1614 *id = got_object_id_dup(&te->id);
1616 return got_error_from_errno("got_object_id_dup");
1618 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1621 got_object_commit_close(commit);
1623 got_object_tree_close(tree);
1627 const struct got_error *
1628 got_object_tree_path_changed(int *changed,
1629 struct got_tree_object *tree01, struct got_tree_object *tree02,
1630 const char *path, struct got_repository *repo)
1632 const struct got_error *err = NULL;
1633 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1634 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1635 const char *seg, *s;
1640 /* We are expecting an absolute in-repository path. */
1642 return got_error(GOT_ERR_NOT_ABSPATH);
1644 /* We not do support comparing the root path. */
1645 if (path[1] == '\0')
1646 return got_error(GOT_ERR_BAD_PATH);
1651 s++; /* skip leading '/' */
1655 struct got_tree_object *next_tree1, *next_tree2;
1664 te1 = find_entry_by_name(tree1, seg, seglen);
1666 err = got_error(GOT_ERR_NO_OBJ);
1670 te2 = find_entry_by_name(tree2, seg, seglen);
1676 if (te1->mode != te2->mode) {
1681 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1686 if (*s == '\0') { /* final path element */
1695 err = got_object_open_as_tree(&next_tree1, repo,
1700 if (tree1 != tree01)
1701 got_object_tree_close(tree1);
1704 err = got_object_open_as_tree(&next_tree2, repo,
1709 if (tree2 != tree02)
1710 got_object_tree_close(tree2);
1715 if (tree1 && tree1 != tree01)
1716 got_object_tree_close(tree1);
1717 if (tree2 && tree2 != tree02)
1718 got_object_tree_close(tree2);
1722 const struct got_error *
1723 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1724 struct got_tree_entry *te)
1726 const struct got_error *err = NULL;
1728 *new_te = calloc(1, sizeof(**new_te));
1729 if (*new_te == NULL)
1730 return got_error_from_errno("calloc");
1732 (*new_te)->mode = te->mode;
1733 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1734 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1739 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1741 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1744 const struct got_error *
1745 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1746 struct got_object_id *commit_id, const char *path,
1747 struct got_repository *repo)
1749 const struct got_error *err = NULL;
1750 struct got_pack *pack = NULL;
1751 struct got_packidx *packidx = NULL;
1752 char *path_packfile = NULL;
1753 struct got_commit_object *changed_commit = NULL;
1754 struct got_object_id *changed_commit_id = NULL;
1757 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1759 if (err->code != GOT_ERR_NO_OBJ)
1764 err = get_packfile_path(&path_packfile, packidx);
1768 pack = got_repo_get_cached_pack(repo, path_packfile);
1770 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1775 if (pack->privsep_child == NULL) {
1776 err = start_pack_privsep_child(pack, packidx);
1781 err = got_privsep_send_commit_traversal_request(
1782 pack->privsep_child->ibuf, commit_id, idx, path);
1786 err = got_privsep_recv_traversed_commits(&changed_commit,
1787 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1791 if (changed_commit) {
1793 * Cache the commit in which the path was changed.
1794 * This commit might be opened again soon.
1796 changed_commit->refcnt++;
1797 err = got_repo_cache_commit(repo, changed_commit_id,
1799 got_object_commit_close(changed_commit);
1802 free(path_packfile);
1803 free(changed_commit_id);