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);
184 static const struct got_error *
185 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
186 struct got_object_id *id)
188 const struct got_error *err = NULL;
189 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
191 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
195 err = got_privsep_recv_obj(obj, ibuf);
199 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
205 set_max_datasize(void)
209 if (getrlimit(RLIMIT_DATA, &rl) != 0)
212 rl.rlim_cur = rl.rlim_max;
213 setrlimit(RLIMIT_DATA, &rl);
216 static const struct got_error *
217 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
219 const struct got_error *err = NULL;
222 struct imsgbuf *ibuf;
224 ibuf = calloc(1, sizeof(*ibuf));
226 return got_error_from_errno("calloc");
228 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
229 if (pack->privsep_child == NULL) {
230 err = got_error_from_errno("calloc");
235 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
236 err = got_error_from_errno("socketpair");
242 err = got_error_from_errno("fork");
244 } else if (pid == 0) {
246 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
247 pack->path_packfile);
251 if (close(imsg_fds[1]) != 0)
252 return got_error_from_errno("close");
253 pack->privsep_child->imsg_fd = imsg_fds[0];
254 pack->privsep_child->pid = pid;
255 imsg_init(ibuf, imsg_fds[0]);
256 pack->privsep_child->ibuf = ibuf;
258 err = got_privsep_init_pack_child(ibuf, pack, packidx);
260 const struct got_error *child_err;
261 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
262 child_err = got_privsep_wait_for_child(
263 pack->privsep_child->pid);
264 if (child_err && err == NULL)
270 free(pack->privsep_child);
271 pack->privsep_child = NULL;
276 static const struct got_error *
277 read_packed_object_privsep(struct got_object **obj,
278 struct got_repository *repo, struct got_pack *pack,
279 struct got_packidx *packidx, int idx, struct got_object_id *id)
281 const struct got_error *err = NULL;
283 if (pack->privsep_child)
284 return request_packed_object(obj, pack, idx, id);
286 err = start_pack_privsep_child(pack, packidx);
290 return request_packed_object(obj, pack, idx, id);
294 static const struct got_error *
295 open_packed_object(struct got_object **obj, struct got_object_id *id,
296 struct got_repository *repo)
298 const struct got_error *err = NULL;
299 struct got_pack *pack = NULL;
300 struct got_packidx *packidx = NULL;
304 err = got_repo_search_packidx(&packidx, &idx, repo, id);
308 err = get_packfile_path(&path_packfile, packidx);
312 pack = got_repo_get_cached_pack(repo, path_packfile);
314 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
319 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
323 err = got_repo_cache_pack(NULL, repo, path_packfile, packidx);
329 static const struct got_error *
330 request_object(struct got_object **obj, struct got_repository *repo, int fd)
332 const struct got_error *err = NULL;
333 struct imsgbuf *ibuf;
335 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
337 err = got_privsep_send_obj_req(ibuf, fd);
341 return got_privsep_recv_obj(obj, ibuf);
344 static const struct got_error *
345 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
348 const struct got_error *err;
351 struct imsgbuf *ibuf;
353 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
354 return request_object(obj, repo, obj_fd);
356 ibuf = calloc(1, sizeof(*ibuf));
358 return got_error_from_errno("calloc");
360 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
361 err = got_error_from_errno("socketpair");
368 err = got_error_from_errno("fork");
373 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
378 if (close(imsg_fds[1]) != 0) {
379 err = got_error_from_errno("close");
383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
386 imsg_init(ibuf, imsg_fds[0]);
387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
389 return request_object(obj, repo, obj_fd);
393 const struct got_error *
394 got_object_open(struct got_object **obj, struct got_repository *repo,
395 struct got_object_id *id)
397 const struct got_error *err = NULL;
401 *obj = got_repo_get_cached_object(repo, id);
407 err = open_packed_object(obj, id, repo);
408 if (err && err->code != GOT_ERR_NO_OBJ)
412 return got_repo_cache_object(repo, id, *obj);
415 err = got_object_get_path(&path, id, repo);
419 fd = open(path, O_RDONLY | O_NOFOLLOW);
422 err = got_error_no_obj(id);
424 err = got_error_from_errno2("open", path);
427 err = read_object_header_privsep(obj, repo, fd);
430 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
434 err = got_repo_cache_object(repo, id, *obj);
441 const struct got_error *
442 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
445 struct got_object_id id;
447 if (!got_parse_sha1_digest(id.sha1, id_str))
448 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
450 return got_object_open(obj, repo, &id);
453 const struct got_error *
454 got_object_resolve_id_str(struct got_object_id **id,
455 struct got_repository *repo, const char *id_str)
457 const struct got_error *err = NULL;
458 struct got_object *obj;
460 err = got_object_open_by_id_str(&obj, repo, id_str);
464 *id = got_object_id_dup(got_object_get_id(obj));
465 got_object_close(obj);
467 return got_error_from_errno("got_object_id_dup");
472 static const struct got_error *
473 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
474 int pack_idx, struct got_object_id *id)
476 const struct got_error *err = NULL;
478 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
483 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
486 static const struct got_error *
487 read_packed_commit_privsep(struct got_commit_object **commit,
488 struct got_pack *pack, struct got_packidx *packidx, int idx,
489 struct got_object_id *id)
491 const struct got_error *err = NULL;
493 if (pack->privsep_child)
494 return request_packed_commit(commit, pack, idx, id);
496 err = start_pack_privsep_child(pack, packidx);
500 return request_packed_commit(commit, pack, idx, id);
503 static const struct got_error *
504 request_commit(struct got_commit_object **commit, struct got_repository *repo,
507 const struct got_error *err = NULL;
508 struct imsgbuf *ibuf;
510 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
512 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
516 return got_privsep_recv_commit(commit, ibuf);
519 static const struct got_error *
520 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
521 struct got_repository *repo)
523 const struct got_error *err;
526 struct imsgbuf *ibuf;
528 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
529 return request_commit(commit, repo, obj_fd);
531 ibuf = calloc(1, sizeof(*ibuf));
533 return got_error_from_errno("calloc");
535 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
536 err = got_error_from_errno("socketpair");
543 err = got_error_from_errno("fork");
548 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
553 if (close(imsg_fds[1]) != 0) {
554 err = got_error_from_errno("close");
558 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
560 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
561 imsg_init(ibuf, imsg_fds[0]);
562 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
564 return request_commit(commit, repo, obj_fd);
568 static const struct got_error *
569 open_commit(struct got_commit_object **commit,
570 struct got_repository *repo, struct got_object_id *id, int check_cache)
572 const struct got_error *err = NULL;
573 struct got_packidx *packidx = NULL;
575 char *path_packfile = NULL;
578 *commit = got_repo_get_cached_commit(repo, id);
579 if (*commit != NULL) {
586 err = got_repo_search_packidx(&packidx, &idx, repo, id);
588 struct got_pack *pack = NULL;
590 err = get_packfile_path(&path_packfile, packidx);
594 pack = got_repo_get_cached_pack(repo, path_packfile);
596 err = got_repo_cache_pack(&pack, repo, path_packfile,
601 err = read_packed_commit_privsep(commit, pack,
603 } else if (err->code == GOT_ERR_NO_OBJ) {
606 err = open_loose_object(&fd, id, repo);
609 err = read_commit_privsep(commit, fd, repo);
614 err = got_repo_cache_commit(repo, id, *commit);
621 const struct got_error *
622 got_object_open_as_commit(struct got_commit_object **commit,
623 struct got_repository *repo, struct got_object_id *id)
625 *commit = got_repo_get_cached_commit(repo, id);
626 if (*commit != NULL) {
631 return open_commit(commit, repo, id, 0);
634 const struct got_error *
635 got_object_commit_open(struct got_commit_object **commit,
636 struct got_repository *repo, struct got_object *obj)
638 return open_commit(commit, repo, got_object_get_id(obj), 1);
641 const struct got_error *
642 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
644 const struct got_error *err = NULL;
646 *qid = calloc(1, sizeof(**qid));
648 return got_error_from_errno("calloc");
650 (*qid)->id = got_object_id_dup(id);
651 if ((*qid)->id == NULL) {
652 err = got_error_from_errno("got_object_id_dup");
653 got_object_qid_free(*qid);
661 static const struct got_error *
662 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
663 int pack_idx, struct got_object_id *id)
665 const struct got_error *err = NULL;
667 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
672 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
675 static const struct got_error *
676 read_packed_tree_privsep(struct got_tree_object **tree,
677 struct got_pack *pack, struct got_packidx *packidx, int idx,
678 struct got_object_id *id)
680 const struct got_error *err = NULL;
682 if (pack->privsep_child)
683 return request_packed_tree(tree, pack, idx, id);
685 err = start_pack_privsep_child(pack, packidx);
689 return request_packed_tree(tree, pack, idx, id);
692 static const struct got_error *
693 request_tree(struct got_tree_object **tree, struct got_repository *repo,
696 const struct got_error *err = NULL;
697 struct imsgbuf *ibuf;
699 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
701 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
705 return got_privsep_recv_tree(tree, ibuf);
708 const struct got_error *
709 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
710 struct got_repository *repo)
712 const struct got_error *err;
715 struct imsgbuf *ibuf;
717 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
718 return request_tree(tree, repo, obj_fd);
720 ibuf = calloc(1, sizeof(*ibuf));
722 return got_error_from_errno("calloc");
724 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
725 err = got_error_from_errno("socketpair");
732 err = got_error_from_errno("fork");
737 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
742 if (close(imsg_fds[1]) != 0) {
743 err = got_error_from_errno("close");
747 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
749 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
750 imsg_init(ibuf, imsg_fds[0]);
751 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
754 return request_tree(tree, repo, obj_fd);
757 static const struct got_error *
758 open_tree(struct got_tree_object **tree, struct got_repository *repo,
759 struct got_object_id *id, int check_cache)
761 const struct got_error *err = NULL;
762 struct got_packidx *packidx = NULL;
764 char *path_packfile = NULL;
767 *tree = got_repo_get_cached_tree(repo, id);
775 err = got_repo_search_packidx(&packidx, &idx, repo, id);
777 struct got_pack *pack = NULL;
779 err = get_packfile_path(&path_packfile, packidx);
783 pack = got_repo_get_cached_pack(repo, path_packfile);
785 err = got_repo_cache_pack(&pack, repo, path_packfile,
790 err = read_packed_tree_privsep(tree, pack,
792 } else if (err->code == GOT_ERR_NO_OBJ) {
795 err = open_loose_object(&fd, id, repo);
798 err = read_tree_privsep(tree, fd, repo);
803 err = got_repo_cache_tree(repo, id, *tree);
810 const struct got_error *
811 got_object_open_as_tree(struct got_tree_object **tree,
812 struct got_repository *repo, struct got_object_id *id)
814 *tree = got_repo_get_cached_tree(repo, id);
820 return open_tree(tree, repo, id, 0);
823 const struct got_error *
824 got_object_tree_open(struct got_tree_object **tree,
825 struct got_repository *repo, struct got_object *obj)
827 return open_tree(tree, repo, got_object_get_id(obj), 1);
831 got_object_tree_get_nentries(struct got_tree_object *tree)
833 return tree->nentries;
836 struct got_tree_entry *
837 got_object_tree_get_first_entry(struct got_tree_object *tree)
839 return got_object_tree_get_entry(tree, 0);
842 struct got_tree_entry *
843 got_object_tree_get_last_entry(struct got_tree_object *tree)
845 return got_object_tree_get_entry(tree, tree->nentries - 1);
848 struct got_tree_entry *
849 got_object_tree_get_entry(struct got_tree_object *tree, int i)
851 if (i < 0 || i >= tree->nentries)
853 return &tree->entries[i];
857 got_tree_entry_get_mode(struct got_tree_entry *te)
863 got_tree_entry_get_name(struct got_tree_entry *te)
868 struct got_object_id *
869 got_tree_entry_get_id(struct got_tree_entry *te)
875 got_tree_entry_get_index(struct got_tree_entry *te)
880 struct got_tree_entry *
881 got_tree_entry_get_next(struct got_tree_object *tree,
882 struct got_tree_entry *te)
884 return got_object_tree_get_entry(tree, te->idx + 1);
887 struct got_tree_entry *
888 got_tree_entry_get_prev(struct got_tree_object *tree,
889 struct got_tree_entry *te)
891 return got_object_tree_get_entry(tree, te->idx - 1);
894 static const struct got_error *
895 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
896 struct got_pack *pack, struct got_packidx *packidx, int idx,
897 struct got_object_id *id)
899 const struct got_error *err = NULL;
901 int basefd, accumfd; /* temporary files for delta application */
903 basefd = got_opentempfd();
905 return got_error_from_errno("got_opentempfd");
906 accumfd = got_opentempfd();
908 return got_error_from_errno("got_opentempfd");
910 outfd_child = dup(outfd);
911 if (outfd_child == -1)
912 return got_error_from_errno("dup");
914 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
918 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
926 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
933 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
938 err = got_privsep_recv_blob(outbuf, size, hdrlen,
939 pack->privsep_child->ibuf);
943 if (lseek(outfd, SEEK_SET, 0) == -1)
944 err = got_error_from_errno("lseek");
949 static const struct got_error *
950 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
951 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
952 struct got_object_id *id)
954 const struct got_error *err = NULL;
956 if (pack->privsep_child == NULL) {
957 err = start_pack_privsep_child(pack, packidx);
962 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
966 static const struct got_error *
967 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
968 int infd, struct imsgbuf *ibuf)
970 const struct got_error *err = NULL;
973 outfd_child = dup(outfd);
974 if (outfd_child == -1)
975 return got_error_from_errno("dup");
977 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
981 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
985 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
989 if (lseek(outfd, SEEK_SET, 0) == -1)
990 return got_error_from_errno("lseek");
995 static const struct got_error *
996 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
997 int outfd, int infd, struct got_repository *repo)
999 const struct got_error *err;
1002 struct imsgbuf *ibuf;
1004 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1005 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1006 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1009 ibuf = calloc(1, sizeof(*ibuf));
1011 return got_error_from_errno("calloc");
1013 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1014 err = got_error_from_errno("socketpair");
1021 err = got_error_from_errno("fork");
1025 else if (pid == 0) {
1026 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1031 if (close(imsg_fds[1]) != 0) {
1032 err = got_error_from_errno("close");
1036 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1038 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1039 imsg_init(ibuf, imsg_fds[0]);
1040 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1042 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1045 static const struct got_error *
1046 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1047 struct got_object_id *id, size_t blocksize)
1049 const struct got_error *err = NULL;
1050 struct got_packidx *packidx = NULL;
1052 char *path_packfile = NULL;
1055 size_t size, hdrlen;
1058 *blob = calloc(1, sizeof(**blob));
1060 return got_error_from_errno("calloc");
1062 outfd = got_opentempfd();
1064 return got_error_from_errno("got_opentempfd");
1066 (*blob)->read_buf = malloc(blocksize);
1067 if ((*blob)->read_buf == NULL) {
1068 err = got_error_from_errno("malloc");
1072 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1074 struct got_pack *pack = NULL;
1076 err = get_packfile_path(&path_packfile, packidx);
1080 pack = got_repo_get_cached_pack(repo, path_packfile);
1082 err = got_repo_cache_pack(&pack, repo, path_packfile,
1087 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1088 pack, packidx, idx, id);
1089 } else if (err->code == GOT_ERR_NO_OBJ) {
1092 err = open_loose_object(&infd, id, repo);
1095 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1101 if (hdrlen > size) {
1102 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1107 if (close(outfd) != 0 && err == NULL)
1108 err = got_error_from_errno("close");
1110 (*blob)->f = fmemopen(outbuf, size, "rb");
1111 if ((*blob)->f == NULL) {
1112 err = got_error_from_errno("fmemopen");
1116 (*blob)->data = outbuf;
1118 if (fstat(outfd, &sb) == -1) {
1119 err = got_error_from_errno("fstat");
1123 if (sb.st_size != size) {
1124 err = got_error(GOT_ERR_PRIVSEP_LEN);
1128 (*blob)->f = fdopen(outfd, "rb");
1129 if ((*blob)->f == NULL) {
1130 err = got_error_from_errno("fdopen");
1137 (*blob)->hdrlen = hdrlen;
1138 (*blob)->blocksize = blocksize;
1139 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1142 free(path_packfile);
1145 got_object_blob_close(*blob);
1147 } else if (outfd != -1)
1153 const struct got_error *
1154 got_object_open_as_blob(struct got_blob_object **blob,
1155 struct got_repository *repo, struct got_object_id *id,
1158 return open_blob(blob, repo, id, blocksize);
1161 const struct got_error *
1162 got_object_blob_open(struct got_blob_object **blob,
1163 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1165 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1168 const struct got_error *
1169 got_object_blob_close(struct got_blob_object *blob)
1171 const struct got_error *err = NULL;
1172 free(blob->read_buf);
1173 if (blob->f && fclose(blob->f) != 0)
1174 err = got_error_from_errno("fclose");
1181 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1183 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1187 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1189 return blob->hdrlen;
1193 got_object_blob_get_read_buf(struct got_blob_object *blob)
1195 return blob->read_buf;
1198 const struct got_error *
1199 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1203 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1204 if (n == 0 && ferror(blob->f))
1205 return got_ferror(blob->f, GOT_ERR_IO);
1210 const struct got_error *
1211 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1212 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1214 const struct got_error *err = NULL;
1215 size_t n, len, hdrlen;
1218 size_t noffsets = 0;
1219 off_t off = 0, total_len = 0;
1222 *line_offsets = NULL;
1228 hdrlen = got_object_blob_get_hdrlen(blob);
1230 err = got_object_blob_read_block(&len, blob);
1235 buf = got_object_blob_get_read_buf(blob);
1237 if (line_offsets && nlines) {
1238 if (*line_offsets == NULL) {
1239 /* Have some data but perhaps no '\n'. */
1242 *line_offsets = calloc(1, sizeof(**line_offsets));
1243 if (*line_offsets == NULL)
1244 return got_error_from_errno("malloc");
1246 /* Skip forward over end of first line. */
1253 /* Scan '\n' offsets in remaining chunk of data. */
1255 if (buf[i] != '\n') {
1260 if (noffsets < *nlines) {
1261 off_t *o = recallocarray(*line_offsets,
1263 sizeof(**line_offsets));
1265 free(*line_offsets);
1266 *line_offsets = NULL;
1267 return got_error_from_errno(
1273 off = total_len + i - hdrlen + 1;
1274 (*line_offsets)[*nlines - 1] = off;
1278 /* Skip blob object header first time around. */
1279 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1280 if (n != len - hdrlen)
1281 return got_ferror(outfile, GOT_ERR_IO);
1282 total_len += len - hdrlen;
1286 if (fflush(outfile) != 0)
1287 return got_error_from_errno("fflush");
1291 *filesize = total_len;
1296 static const struct got_error *
1297 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1298 int pack_idx, struct got_object_id *id)
1300 const struct got_error *err = NULL;
1302 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1307 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1310 static const struct got_error *
1311 read_packed_tag_privsep(struct got_tag_object **tag,
1312 struct got_pack *pack, struct got_packidx *packidx, int idx,
1313 struct got_object_id *id)
1315 const struct got_error *err = NULL;
1317 if (pack->privsep_child)
1318 return request_packed_tag(tag, pack, idx, id);
1320 err = start_pack_privsep_child(pack, packidx);
1324 return request_packed_tag(tag, pack, idx, id);
1327 static const struct got_error *
1328 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1331 const struct got_error *err = NULL;
1332 struct imsgbuf *ibuf;
1334 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1336 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1340 return got_privsep_recv_tag(tag, ibuf);
1343 static const struct got_error *
1344 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1345 struct got_repository *repo)
1347 const struct got_error *err;
1350 struct imsgbuf *ibuf;
1352 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1353 return request_tag(tag, repo, obj_fd);
1355 ibuf = calloc(1, sizeof(*ibuf));
1357 return got_error_from_errno("calloc");
1359 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1360 err = got_error_from_errno("socketpair");
1367 err = got_error_from_errno("fork");
1371 else if (pid == 0) {
1372 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1377 if (close(imsg_fds[1]) != 0) {
1378 err = got_error_from_errno("close");
1382 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1384 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1385 imsg_init(ibuf, imsg_fds[0]);
1386 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1388 return request_tag(tag, repo, obj_fd);
1391 static const struct got_error *
1392 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1393 struct got_object_id *id, int check_cache)
1395 const struct got_error *err = NULL;
1396 struct got_packidx *packidx = NULL;
1398 char *path_packfile = NULL;
1399 struct got_object *obj = NULL;
1400 int obj_type = GOT_OBJ_TYPE_ANY;
1403 *tag = got_repo_get_cached_tag(repo, id);
1411 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1413 struct got_pack *pack = NULL;
1415 err = get_packfile_path(&path_packfile, packidx);
1419 pack = got_repo_get_cached_pack(repo, path_packfile);
1421 err = got_repo_cache_pack(&pack, repo, path_packfile,
1427 /* Beware of "leightweight" tags: Check object type first. */
1428 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1432 obj_type = obj->type;
1433 got_object_close(obj);
1434 if (obj_type != GOT_OBJ_TYPE_TAG) {
1435 err = got_error(GOT_ERR_OBJ_TYPE);
1438 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1439 } else if (err->code == GOT_ERR_NO_OBJ) {
1442 err = open_loose_object(&fd, id, repo);
1445 err = read_object_header_privsep(&obj, repo, fd);
1448 obj_type = obj->type;
1449 got_object_close(obj);
1450 if (obj_type != GOT_OBJ_TYPE_TAG)
1451 return got_error(GOT_ERR_OBJ_TYPE);
1453 err = open_loose_object(&fd, id, repo);
1456 err = read_tag_privsep(tag, fd, repo);
1461 err = got_repo_cache_tag(repo, id, *tag);
1464 free(path_packfile);
1468 const struct got_error *
1469 got_object_open_as_tag(struct got_tag_object **tag,
1470 struct got_repository *repo, struct got_object_id *id)
1472 *tag = got_repo_get_cached_tag(repo, id);
1478 return open_tag(tag, repo, id, 0);
1481 const struct got_error *
1482 got_object_tag_open(struct got_tag_object **tag,
1483 struct got_repository *repo, struct got_object *obj)
1485 return open_tag(tag, repo, got_object_get_id(obj), 1);
1489 got_object_tag_get_name(struct got_tag_object *tag)
1495 got_object_tag_get_object_type(struct got_tag_object *tag)
1497 return tag->obj_type;
1500 struct got_object_id *
1501 got_object_tag_get_object_id(struct got_tag_object *tag)
1507 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1509 return tag->tagger_time;
1513 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1515 return tag->tagger_gmtoff;
1519 got_object_tag_get_tagger(struct got_tag_object *tag)
1525 got_object_tag_get_message(struct got_tag_object *tag)
1530 static struct got_tree_entry *
1531 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1535 /* Note that tree entries are sorted in strncmp() order. */
1536 for (i = 0; i < tree->nentries; i++) {
1537 struct got_tree_entry *te = &tree->entries[i];
1538 int cmp = strncmp(te->name, name, len);
1543 if (te->name[len] == '\0')
1549 struct got_tree_entry *
1550 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1552 return find_entry_by_name(tree, name, strlen(name));
1555 const struct got_error *
1556 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1557 struct got_object_id *commit_id, const char *path)
1559 const struct got_error *err = NULL;
1560 struct got_commit_object *commit = NULL;
1561 struct got_tree_object *tree = NULL;
1562 struct got_tree_entry *te = NULL;
1563 const char *seg, *s;
1568 err = got_object_open_as_commit(&commit, repo, commit_id);
1572 /* Handle opening of root of commit's tree. */
1573 if (got_path_is_root_dir(path)) {
1574 *id = got_object_id_dup(commit->tree_id);
1576 err = got_error_from_errno("got_object_id_dup");
1580 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1590 struct got_tree_object *next_tree;
1599 te = find_entry_by_name(tree, seg, seglen);
1601 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1612 err = got_object_open_as_tree(&next_tree, repo,
1617 got_object_tree_close(tree);
1623 *id = got_object_id_dup(&te->id);
1625 return got_error_from_errno("got_object_id_dup");
1627 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1630 got_object_commit_close(commit);
1632 got_object_tree_close(tree);
1636 const struct got_error *
1637 got_object_tree_path_changed(int *changed,
1638 struct got_tree_object *tree01, struct got_tree_object *tree02,
1639 const char *path, struct got_repository *repo)
1641 const struct got_error *err = NULL;
1642 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1643 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1644 const char *seg, *s;
1649 /* We are expecting an absolute in-repository path. */
1651 return got_error(GOT_ERR_NOT_ABSPATH);
1653 /* We not do support comparing the root path. */
1654 if (path[1] == '\0')
1655 return got_error(GOT_ERR_BAD_PATH);
1660 s++; /* skip leading '/' */
1664 struct got_tree_object *next_tree1, *next_tree2;
1673 te1 = find_entry_by_name(tree1, seg, seglen);
1675 err = got_error(GOT_ERR_NO_OBJ);
1679 te2 = find_entry_by_name(tree2, seg, seglen);
1685 if (te1->mode != te2->mode) {
1690 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1695 if (*s == '\0') { /* final path element */
1704 err = got_object_open_as_tree(&next_tree1, repo,
1709 if (tree1 != tree01)
1710 got_object_tree_close(tree1);
1713 err = got_object_open_as_tree(&next_tree2, repo,
1718 if (tree2 != tree02)
1719 got_object_tree_close(tree2);
1724 if (tree1 && tree1 != tree01)
1725 got_object_tree_close(tree1);
1726 if (tree2 && tree2 != tree02)
1727 got_object_tree_close(tree2);
1731 const struct got_error *
1732 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1733 struct got_tree_entry *te)
1735 const struct got_error *err = NULL;
1737 *new_te = calloc(1, sizeof(**new_te));
1738 if (*new_te == NULL)
1739 return got_error_from_errno("calloc");
1741 (*new_te)->mode = te->mode;
1742 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1743 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1748 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1750 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);