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/resource.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
128 static const struct got_error *
129 open_loose_object(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_from_errno2("open", path);
148 static const struct got_error *
149 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
153 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
154 size = strlen(packidx->path_packidx) + 2;
155 if (size < GOT_PACKFILE_NAMELEN + 1)
156 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
158 *path_packfile = malloc(size);
159 if (*path_packfile == NULL)
160 return got_error_from_errno("malloc");
162 /* Copy up to and excluding ".idx". */
163 if (strlcpy(*path_packfile, packidx->path_packidx,
164 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
165 return got_error(GOT_ERR_NO_SPACE);
167 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
168 return got_error(GOT_ERR_NO_SPACE);
173 static const struct got_error *
174 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
175 struct got_object_id *id)
177 const struct got_error *err = NULL;
178 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
180 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
184 err = got_privsep_recv_obj(obj, ibuf);
188 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
194 set_max_datasize(void)
198 if (getrlimit(RLIMIT_DATA, &rl) != 0)
201 rl.rlim_cur = rl.rlim_max;
202 setrlimit(RLIMIT_DATA, &rl);
205 static const struct got_error *
206 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
208 const struct got_error *err = NULL;
211 struct imsgbuf *ibuf;
213 ibuf = calloc(1, sizeof(*ibuf));
215 return got_error_from_errno("calloc");
217 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
218 if (pack->privsep_child == NULL) {
219 err = got_error_from_errno("calloc");
224 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
225 err = got_error_from_errno("socketpair");
231 err = got_error_from_errno("fork");
233 } else if (pid == 0) {
235 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
236 pack->path_packfile);
240 if (close(imsg_fds[1]) != 0)
241 return got_error_from_errno("close");
242 pack->privsep_child->imsg_fd = imsg_fds[0];
243 pack->privsep_child->pid = pid;
244 imsg_init(ibuf, imsg_fds[0]);
245 pack->privsep_child->ibuf = ibuf;
247 err = got_privsep_init_pack_child(ibuf, pack, packidx);
249 const struct got_error *child_err;
250 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
251 child_err = got_privsep_wait_for_child(
252 pack->privsep_child->pid);
253 if (child_err && err == NULL)
259 free(pack->privsep_child);
260 pack->privsep_child = NULL;
265 static const struct got_error *
266 read_packed_object_privsep(struct got_object **obj,
267 struct got_repository *repo, struct got_pack *pack,
268 struct got_packidx *packidx, int idx, struct got_object_id *id)
270 const struct got_error *err = NULL;
272 if (pack->privsep_child)
273 return request_packed_object(obj, pack, idx, id);
275 err = start_pack_privsep_child(pack, packidx);
279 return request_packed_object(obj, pack, idx, id);
283 static const struct got_error *
284 open_packed_object(struct got_object **obj, struct got_object_id *id,
285 struct got_repository *repo)
287 const struct got_error *err = NULL;
288 struct got_pack *pack = NULL;
289 struct got_packidx *packidx = NULL;
293 err = got_repo_search_packidx(&packidx, &idx, repo, id);
297 err = get_packfile_path(&path_packfile, packidx);
301 pack = got_repo_get_cached_pack(repo, path_packfile);
303 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
308 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
316 static const struct got_error *
317 request_object(struct got_object **obj, struct got_repository *repo, int fd)
319 const struct got_error *err = NULL;
320 struct imsgbuf *ibuf;
322 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
324 err = got_privsep_send_obj_req(ibuf, fd);
328 return got_privsep_recv_obj(obj, ibuf);
331 static const struct got_error *
332 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
335 const struct got_error *err;
338 struct imsgbuf *ibuf;
340 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
341 return request_object(obj, repo, obj_fd);
343 ibuf = calloc(1, sizeof(*ibuf));
345 return got_error_from_errno("calloc");
347 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
348 err = got_error_from_errno("socketpair");
355 err = got_error_from_errno("fork");
360 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
365 if (close(imsg_fds[1]) != 0) {
366 err = got_error_from_errno("close");
370 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
372 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
373 imsg_init(ibuf, imsg_fds[0]);
374 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
376 return request_object(obj, repo, obj_fd);
380 const struct got_error *
381 got_object_open(struct got_object **obj, struct got_repository *repo,
382 struct got_object_id *id)
384 const struct got_error *err = NULL;
388 *obj = got_repo_get_cached_object(repo, id);
394 err = open_packed_object(obj, id, repo);
395 if (err && err->code != GOT_ERR_NO_OBJ)
399 return got_repo_cache_object(repo, id, *obj);
402 err = got_object_get_path(&path, id, repo);
406 fd = open(path, O_RDONLY | O_NOFOLLOW);
409 err = got_error_no_obj(id);
411 err = got_error_from_errno2("open", path);
414 err = read_object_header_privsep(obj, repo, fd);
417 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
421 err = got_repo_cache_object(repo, id, *obj);
428 const struct got_error *
429 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
432 struct got_object_id id;
434 if (!got_parse_sha1_digest(id.sha1, id_str))
435 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
437 return got_object_open(obj, repo, &id);
440 const struct got_error *
441 got_object_resolve_id_str(struct got_object_id **id,
442 struct got_repository *repo, const char *id_str)
444 const struct got_error *err = NULL;
445 struct got_object *obj;
447 err = got_object_open_by_id_str(&obj, repo, id_str);
451 *id = got_object_id_dup(got_object_get_id(obj));
452 got_object_close(obj);
454 return got_error_from_errno("got_object_id_dup");
459 static const struct got_error *
460 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
461 int pack_idx, struct got_object_id *id)
463 const struct got_error *err = NULL;
465 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
470 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
474 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
478 static const struct got_error *
479 read_packed_commit_privsep(struct got_commit_object **commit,
480 struct got_pack *pack, struct got_packidx *packidx, int idx,
481 struct got_object_id *id)
483 const struct got_error *err = NULL;
485 if (pack->privsep_child)
486 return request_packed_commit(commit, pack, idx, id);
488 err = start_pack_privsep_child(pack, packidx);
492 return request_packed_commit(commit, pack, idx, id);
495 static const struct got_error *
496 request_commit(struct got_commit_object **commit, struct got_repository *repo,
499 const struct got_error *err = NULL;
500 struct imsgbuf *ibuf;
502 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
504 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
508 return got_privsep_recv_commit(commit, ibuf);
511 static const struct got_error *
512 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
513 struct got_repository *repo)
515 const struct got_error *err;
518 struct imsgbuf *ibuf;
520 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
521 return request_commit(commit, repo, obj_fd);
523 ibuf = calloc(1, sizeof(*ibuf));
525 return got_error_from_errno("calloc");
527 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
528 err = got_error_from_errno("socketpair");
535 err = got_error_from_errno("fork");
540 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
545 if (close(imsg_fds[1]) != 0) {
546 err = got_error_from_errno("close");
550 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
552 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
553 imsg_init(ibuf, imsg_fds[0]);
554 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
556 return request_commit(commit, repo, obj_fd);
560 static const struct got_error *
561 open_commit(struct got_commit_object **commit,
562 struct got_repository *repo, struct got_object_id *id, int check_cache)
564 const struct got_error *err = NULL;
565 struct got_packidx *packidx = NULL;
567 char *path_packfile = NULL;
570 *commit = got_repo_get_cached_commit(repo, id);
571 if (*commit != NULL) {
578 err = got_repo_search_packidx(&packidx, &idx, repo, id);
580 struct got_pack *pack = NULL;
582 err = get_packfile_path(&path_packfile, packidx);
586 pack = got_repo_get_cached_pack(repo, path_packfile);
588 err = got_repo_cache_pack(&pack, repo, path_packfile,
593 err = read_packed_commit_privsep(commit, pack,
595 } else if (err->code == GOT_ERR_NO_OBJ) {
598 err = open_loose_object(&fd, id, repo);
601 err = read_commit_privsep(commit, fd, repo);
606 err = got_repo_cache_commit(repo, id, *commit);
613 const struct got_error *
614 got_object_open_as_commit(struct got_commit_object **commit,
615 struct got_repository *repo, struct got_object_id *id)
617 *commit = got_repo_get_cached_commit(repo, id);
618 if (*commit != NULL) {
623 return open_commit(commit, repo, id, 0);
626 const struct got_error *
627 got_object_commit_open(struct got_commit_object **commit,
628 struct got_repository *repo, struct got_object *obj)
630 return open_commit(commit, repo, got_object_get_id(obj), 1);
633 const struct got_error *
634 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
636 const struct got_error *err = NULL;
638 *qid = calloc(1, sizeof(**qid));
640 return got_error_from_errno("calloc");
642 (*qid)->id = got_object_id_dup(id);
643 if ((*qid)->id == NULL) {
644 err = got_error_from_errno("got_object_id_dup");
645 got_object_qid_free(*qid);
653 static const struct got_error *
654 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
655 int pack_idx, struct got_object_id *id)
657 const struct got_error *err = NULL;
659 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
664 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
667 static const struct got_error *
668 read_packed_tree_privsep(struct got_tree_object **tree,
669 struct got_pack *pack, struct got_packidx *packidx, int idx,
670 struct got_object_id *id)
672 const struct got_error *err = NULL;
674 if (pack->privsep_child)
675 return request_packed_tree(tree, pack, idx, id);
677 err = start_pack_privsep_child(pack, packidx);
681 return request_packed_tree(tree, pack, idx, id);
684 static const struct got_error *
685 request_tree(struct got_tree_object **tree, struct got_repository *repo,
688 const struct got_error *err = NULL;
689 struct imsgbuf *ibuf;
691 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
693 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
697 return got_privsep_recv_tree(tree, ibuf);
700 const struct got_error *
701 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
702 struct got_repository *repo)
704 const struct got_error *err;
707 struct imsgbuf *ibuf;
709 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
710 return request_tree(tree, repo, obj_fd);
712 ibuf = calloc(1, sizeof(*ibuf));
714 return got_error_from_errno("calloc");
716 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
717 err = got_error_from_errno("socketpair");
724 err = got_error_from_errno("fork");
729 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
734 if (close(imsg_fds[1]) != 0) {
735 err = got_error_from_errno("close");
739 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
741 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
742 imsg_init(ibuf, imsg_fds[0]);
743 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
746 return request_tree(tree, repo, obj_fd);
749 static const struct got_error *
750 open_tree(struct got_tree_object **tree, struct got_repository *repo,
751 struct got_object_id *id, int check_cache)
753 const struct got_error *err = NULL;
754 struct got_packidx *packidx = NULL;
756 char *path_packfile = NULL;
759 *tree = got_repo_get_cached_tree(repo, id);
767 err = got_repo_search_packidx(&packidx, &idx, repo, id);
769 struct got_pack *pack = NULL;
771 err = get_packfile_path(&path_packfile, packidx);
775 pack = got_repo_get_cached_pack(repo, path_packfile);
777 err = got_repo_cache_pack(&pack, repo, path_packfile,
782 err = read_packed_tree_privsep(tree, pack,
784 } else if (err->code == GOT_ERR_NO_OBJ) {
787 err = open_loose_object(&fd, id, repo);
790 err = read_tree_privsep(tree, fd, repo);
795 err = got_repo_cache_tree(repo, id, *tree);
802 const struct got_error *
803 got_object_open_as_tree(struct got_tree_object **tree,
804 struct got_repository *repo, struct got_object_id *id)
806 *tree = got_repo_get_cached_tree(repo, id);
812 return open_tree(tree, repo, id, 0);
815 const struct got_error *
816 got_object_tree_open(struct got_tree_object **tree,
817 struct got_repository *repo, struct got_object *obj)
819 return open_tree(tree, repo, got_object_get_id(obj), 1);
823 got_object_tree_get_nentries(struct got_tree_object *tree)
825 return tree->nentries;
828 struct got_tree_entry *
829 got_object_tree_get_first_entry(struct got_tree_object *tree)
831 return got_object_tree_get_entry(tree, 0);
834 struct got_tree_entry *
835 got_object_tree_get_last_entry(struct got_tree_object *tree)
837 return got_object_tree_get_entry(tree, tree->nentries - 1);
840 struct got_tree_entry *
841 got_object_tree_get_entry(struct got_tree_object *tree, int i)
843 if (i < 0 || i >= tree->nentries)
845 return &tree->entries[i];
849 got_tree_entry_get_mode(struct got_tree_entry *te)
855 got_tree_entry_get_name(struct got_tree_entry *te)
860 struct got_object_id *
861 got_tree_entry_get_id(struct got_tree_entry *te)
866 const struct got_error *
867 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
869 const struct got_error *err = NULL;
870 size_t len, totlen, hdrlen, offset;
874 hdrlen = got_object_blob_get_hdrlen(blob);
880 err = got_object_blob_read_block(&len, blob);
887 totlen += len - hdrlen;
888 p = realloc(*s, totlen + 1);
890 err = got_error_from_errno("realloc");
896 /* Skip blob object header first time around. */
898 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
907 const struct got_error *
908 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
909 struct got_repository *repo)
911 const struct got_error *err = NULL;
912 struct got_blob_object *blob = NULL;
916 if (!got_object_tree_entry_is_symlink(te))
917 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
919 err = got_object_open_as_blob(&blob, repo,
920 got_tree_entry_get_id(te), PATH_MAX);
924 err = got_object_blob_read_to_str(link_target, blob);
925 got_object_blob_close(blob);
934 got_tree_entry_get_index(struct got_tree_entry *te)
939 struct got_tree_entry *
940 got_tree_entry_get_next(struct got_tree_object *tree,
941 struct got_tree_entry *te)
943 return got_object_tree_get_entry(tree, te->idx + 1);
946 struct got_tree_entry *
947 got_tree_entry_get_prev(struct got_tree_object *tree,
948 struct got_tree_entry *te)
950 return got_object_tree_get_entry(tree, te->idx - 1);
953 static const struct got_error *
954 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
955 struct got_pack *pack, struct got_packidx *packidx, int idx,
956 struct got_object_id *id)
958 const struct got_error *err = NULL;
960 int basefd, accumfd; /* temporary files for delta application */
962 basefd = got_opentempfd();
964 return got_error_from_errno("got_opentempfd");
965 accumfd = got_opentempfd();
967 return got_error_from_errno("got_opentempfd");
969 outfd_child = dup(outfd);
970 if (outfd_child == -1)
971 return got_error_from_errno("dup");
973 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
977 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
985 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
992 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
997 err = got_privsep_recv_blob(outbuf, size, hdrlen,
998 pack->privsep_child->ibuf);
1002 if (lseek(outfd, SEEK_SET, 0) == -1)
1003 err = got_error_from_errno("lseek");
1008 static const struct got_error *
1009 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1010 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1011 struct got_object_id *id)
1013 const struct got_error *err = NULL;
1015 if (pack->privsep_child == NULL) {
1016 err = start_pack_privsep_child(pack, packidx);
1021 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1025 static const struct got_error *
1026 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1027 int infd, struct imsgbuf *ibuf)
1029 const struct got_error *err = NULL;
1032 outfd_child = dup(outfd);
1033 if (outfd_child == -1)
1034 return got_error_from_errno("dup");
1036 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1040 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1044 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1048 if (lseek(outfd, SEEK_SET, 0) == -1)
1049 return got_error_from_errno("lseek");
1054 static const struct got_error *
1055 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1056 int outfd, int infd, struct got_repository *repo)
1058 const struct got_error *err;
1061 struct imsgbuf *ibuf;
1063 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1064 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1065 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1068 ibuf = calloc(1, sizeof(*ibuf));
1070 return got_error_from_errno("calloc");
1072 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1073 err = got_error_from_errno("socketpair");
1080 err = got_error_from_errno("fork");
1084 else if (pid == 0) {
1085 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1090 if (close(imsg_fds[1]) != 0) {
1091 err = got_error_from_errno("close");
1095 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1097 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1098 imsg_init(ibuf, imsg_fds[0]);
1099 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1101 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1104 static const struct got_error *
1105 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1106 struct got_object_id *id, size_t blocksize)
1108 const struct got_error *err = NULL;
1109 struct got_packidx *packidx = NULL;
1111 char *path_packfile = NULL;
1114 size_t size, hdrlen;
1117 *blob = calloc(1, sizeof(**blob));
1119 return got_error_from_errno("calloc");
1121 outfd = got_opentempfd();
1123 return got_error_from_errno("got_opentempfd");
1125 (*blob)->read_buf = malloc(blocksize);
1126 if ((*blob)->read_buf == NULL) {
1127 err = got_error_from_errno("malloc");
1131 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1133 struct got_pack *pack = NULL;
1135 err = get_packfile_path(&path_packfile, packidx);
1139 pack = got_repo_get_cached_pack(repo, path_packfile);
1141 err = got_repo_cache_pack(&pack, repo, path_packfile,
1146 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1147 pack, packidx, idx, id);
1148 } else if (err->code == GOT_ERR_NO_OBJ) {
1151 err = open_loose_object(&infd, id, repo);
1154 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1160 if (hdrlen > size) {
1161 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1166 if (close(outfd) != 0 && err == NULL)
1167 err = got_error_from_errno("close");
1169 (*blob)->f = fmemopen(outbuf, size, "rb");
1170 if ((*blob)->f == NULL) {
1171 err = got_error_from_errno("fmemopen");
1175 (*blob)->data = outbuf;
1177 if (fstat(outfd, &sb) == -1) {
1178 err = got_error_from_errno("fstat");
1182 if (sb.st_size != size) {
1183 err = got_error(GOT_ERR_PRIVSEP_LEN);
1187 (*blob)->f = fdopen(outfd, "rb");
1188 if ((*blob)->f == NULL) {
1189 err = got_error_from_errno("fdopen");
1196 (*blob)->hdrlen = hdrlen;
1197 (*blob)->blocksize = blocksize;
1198 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1201 free(path_packfile);
1204 got_object_blob_close(*blob);
1206 } else if (outfd != -1)
1212 const struct got_error *
1213 got_object_open_as_blob(struct got_blob_object **blob,
1214 struct got_repository *repo, struct got_object_id *id,
1217 return open_blob(blob, repo, id, blocksize);
1220 const struct got_error *
1221 got_object_blob_open(struct got_blob_object **blob,
1222 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1224 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1227 const struct got_error *
1228 got_object_blob_close(struct got_blob_object *blob)
1230 const struct got_error *err = NULL;
1231 free(blob->read_buf);
1232 if (blob->f && fclose(blob->f) != 0)
1233 err = got_error_from_errno("fclose");
1240 got_object_blob_rewind(struct got_blob_object *blob)
1247 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1249 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1253 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1255 return blob->hdrlen;
1259 got_object_blob_get_read_buf(struct got_blob_object *blob)
1261 return blob->read_buf;
1264 const struct got_error *
1265 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1269 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1270 if (n == 0 && ferror(blob->f))
1271 return got_ferror(blob->f, GOT_ERR_IO);
1276 const struct got_error *
1277 got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1278 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1280 const struct got_error *err = NULL;
1281 size_t n, len, hdrlen;
1284 size_t noffsets = 0;
1285 off_t off = 0, total_len = 0;
1288 *line_offsets = NULL;
1294 hdrlen = got_object_blob_get_hdrlen(blob);
1296 err = got_object_blob_read_block(&len, blob);
1301 buf = got_object_blob_get_read_buf(blob);
1304 if (line_offsets && *line_offsets == NULL) {
1305 /* Have some data but perhaps no '\n'. */
1308 *line_offsets = calloc(1, sizeof(**line_offsets));
1309 if (*line_offsets == NULL)
1310 return got_error_from_errno("calloc");
1312 /* Skip forward over end of first line. */
1319 /* Scan '\n' offsets in remaining chunk of data. */
1321 if (buf[i] != '\n') {
1326 if (line_offsets && noffsets < *nlines) {
1327 off_t *o = recallocarray(*line_offsets,
1329 sizeof(**line_offsets));
1331 free(*line_offsets);
1332 *line_offsets = NULL;
1333 return got_error_from_errno(
1340 off = total_len + i - hdrlen + 1;
1341 (*line_offsets)[*nlines - 1] = off;
1346 /* Skip blob object header first time around. */
1347 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1348 if (n != len - hdrlen)
1349 return got_ferror(outfile, GOT_ERR_IO);
1350 total_len += len - hdrlen;
1354 if (fflush(outfile) != 0)
1355 return got_error_from_errno("fflush");
1359 *filesize = total_len;
1364 static const struct got_error *
1365 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1366 int pack_idx, struct got_object_id *id)
1368 const struct got_error *err = NULL;
1370 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1375 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1378 static const struct got_error *
1379 read_packed_tag_privsep(struct got_tag_object **tag,
1380 struct got_pack *pack, struct got_packidx *packidx, int idx,
1381 struct got_object_id *id)
1383 const struct got_error *err = NULL;
1385 if (pack->privsep_child)
1386 return request_packed_tag(tag, pack, idx, id);
1388 err = start_pack_privsep_child(pack, packidx);
1392 return request_packed_tag(tag, pack, idx, id);
1395 static const struct got_error *
1396 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1399 const struct got_error *err = NULL;
1400 struct imsgbuf *ibuf;
1402 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1404 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1408 return got_privsep_recv_tag(tag, ibuf);
1411 static const struct got_error *
1412 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1413 struct got_repository *repo)
1415 const struct got_error *err;
1418 struct imsgbuf *ibuf;
1420 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1421 return request_tag(tag, repo, obj_fd);
1423 ibuf = calloc(1, sizeof(*ibuf));
1425 return got_error_from_errno("calloc");
1427 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1428 err = got_error_from_errno("socketpair");
1435 err = got_error_from_errno("fork");
1439 else if (pid == 0) {
1440 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1445 if (close(imsg_fds[1]) != 0) {
1446 err = got_error_from_errno("close");
1450 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1452 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1453 imsg_init(ibuf, imsg_fds[0]);
1454 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1456 return request_tag(tag, repo, obj_fd);
1459 static const struct got_error *
1460 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1461 struct got_object_id *id, int check_cache)
1463 const struct got_error *err = NULL;
1464 struct got_packidx *packidx = NULL;
1466 char *path_packfile = NULL;
1467 struct got_object *obj = NULL;
1468 int obj_type = GOT_OBJ_TYPE_ANY;
1471 *tag = got_repo_get_cached_tag(repo, id);
1479 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1481 struct got_pack *pack = NULL;
1483 err = get_packfile_path(&path_packfile, packidx);
1487 pack = got_repo_get_cached_pack(repo, path_packfile);
1489 err = got_repo_cache_pack(&pack, repo, path_packfile,
1495 /* Beware of "lightweight" tags: Check object type first. */
1496 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1500 obj_type = obj->type;
1501 got_object_close(obj);
1502 if (obj_type != GOT_OBJ_TYPE_TAG) {
1503 err = got_error(GOT_ERR_OBJ_TYPE);
1506 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1507 } else if (err->code == GOT_ERR_NO_OBJ) {
1510 err = open_loose_object(&fd, id, repo);
1513 err = read_object_header_privsep(&obj, repo, fd);
1516 obj_type = obj->type;
1517 got_object_close(obj);
1518 if (obj_type != GOT_OBJ_TYPE_TAG)
1519 return got_error(GOT_ERR_OBJ_TYPE);
1521 err = open_loose_object(&fd, id, repo);
1524 err = read_tag_privsep(tag, fd, repo);
1529 err = got_repo_cache_tag(repo, id, *tag);
1532 free(path_packfile);
1536 const struct got_error *
1537 got_object_open_as_tag(struct got_tag_object **tag,
1538 struct got_repository *repo, struct got_object_id *id)
1540 *tag = got_repo_get_cached_tag(repo, id);
1546 return open_tag(tag, repo, id, 0);
1549 const struct got_error *
1550 got_object_tag_open(struct got_tag_object **tag,
1551 struct got_repository *repo, struct got_object *obj)
1553 return open_tag(tag, repo, got_object_get_id(obj), 1);
1557 got_object_tag_get_name(struct got_tag_object *tag)
1563 got_object_tag_get_object_type(struct got_tag_object *tag)
1565 return tag->obj_type;
1568 struct got_object_id *
1569 got_object_tag_get_object_id(struct got_tag_object *tag)
1575 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1577 return tag->tagger_time;
1581 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1583 return tag->tagger_gmtoff;
1587 got_object_tag_get_tagger(struct got_tag_object *tag)
1593 got_object_tag_get_message(struct got_tag_object *tag)
1598 static struct got_tree_entry *
1599 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1603 /* Note that tree entries are sorted in strncmp() order. */
1604 for (i = 0; i < tree->nentries; i++) {
1605 struct got_tree_entry *te = &tree->entries[i];
1606 int cmp = strncmp(te->name, name, len);
1611 if (te->name[len] == '\0')
1617 struct got_tree_entry *
1618 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1620 return find_entry_by_name(tree, name, strlen(name));
1623 const struct got_error *
1624 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1625 struct got_object_id *commit_id, const char *path)
1627 const struct got_error *err = NULL;
1628 struct got_commit_object *commit = NULL;
1629 struct got_tree_object *tree = NULL;
1630 struct got_tree_entry *te = NULL;
1631 const char *seg, *s;
1636 err = got_object_open_as_commit(&commit, repo, commit_id);
1640 /* Handle opening of root of commit's tree. */
1641 if (got_path_is_root_dir(path)) {
1642 *id = got_object_id_dup(commit->tree_id);
1644 err = got_error_from_errno("got_object_id_dup");
1648 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1658 struct got_tree_object *next_tree;
1667 te = find_entry_by_name(tree, seg, seglen);
1669 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1680 err = got_object_open_as_tree(&next_tree, repo,
1685 got_object_tree_close(tree);
1691 *id = got_object_id_dup(&te->id);
1693 return got_error_from_errno("got_object_id_dup");
1695 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1698 got_object_commit_close(commit);
1700 got_object_tree_close(tree);
1705 * Normalize file mode bits to avoid false positive tree entry differences
1706 * in case tree entries have unexpected mode bits set.
1709 normalize_mode_for_comparison(mode_t mode)
1712 * For directories, the only relevant bit is the IFDIR bit.
1713 * This allows us to detect paths changing from a directory
1714 * to a file and vice versa.
1717 return mode & S_IFDIR;
1720 * For symlinks, the only relevant bit is the IFLNK bit.
1721 * This allows us to detect paths changing from a symlinks
1722 * to a file or directory and vice versa.
1725 return mode & S_IFLNK;
1727 /* For files, the only change we care about is the executable bit. */
1728 return mode & S_IXUSR;
1731 const struct got_error *
1732 got_object_tree_path_changed(int *changed,
1733 struct got_tree_object *tree01, struct got_tree_object *tree02,
1734 const char *path, struct got_repository *repo)
1736 const struct got_error *err = NULL;
1737 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1738 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1739 const char *seg, *s;
1744 /* We not do support comparing the root path. */
1745 if (got_path_is_root_dir(path))
1746 return got_error_path(path, GOT_ERR_BAD_PATH);
1756 struct got_tree_object *next_tree1, *next_tree2;
1757 mode_t mode1, mode2;
1766 te1 = find_entry_by_name(tree1, seg, seglen);
1768 err = got_error(GOT_ERR_NO_OBJ);
1772 te2 = find_entry_by_name(tree2, seg, seglen);
1778 mode1 = normalize_mode_for_comparison(te1->mode);
1779 mode2 = normalize_mode_for_comparison(te2->mode);
1780 if (mode1 != mode2) {
1785 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1790 if (*s == '\0') { /* final path element */
1799 err = got_object_open_as_tree(&next_tree1, repo,
1804 if (tree1 != tree01)
1805 got_object_tree_close(tree1);
1808 err = got_object_open_as_tree(&next_tree2, repo,
1813 if (tree2 != tree02)
1814 got_object_tree_close(tree2);
1819 if (tree1 && tree1 != tree01)
1820 got_object_tree_close(tree1);
1821 if (tree2 && tree2 != tree02)
1822 got_object_tree_close(tree2);
1826 const struct got_error *
1827 got_object_tree_entry_dup(struct got_tree_entry **new_te,
1828 struct got_tree_entry *te)
1830 const struct got_error *err = NULL;
1832 *new_te = calloc(1, sizeof(**new_te));
1833 if (*new_te == NULL)
1834 return got_error_from_errno("calloc");
1836 (*new_te)->mode = te->mode;
1837 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1838 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1843 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1845 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1849 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
1851 /* S_IFDIR check avoids confusing symlinks with submodules. */
1852 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
1855 static const struct got_error *
1856 resolve_symlink(char **link_target, const char *path,
1857 struct got_object_id *commit_id, struct got_repository *repo)
1859 const struct got_error *err = NULL;
1861 char *name, *parent_path = NULL;
1862 struct got_object_id *tree_obj_id = NULL;
1863 struct got_tree_object *tree = NULL;
1864 struct got_tree_entry *te = NULL;
1866 *link_target = NULL;
1868 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
1869 return got_error(GOT_ERR_NO_SPACE);
1871 name = basename(buf);
1873 return got_error_from_errno2("basename", path);
1875 err = got_path_dirname(&parent_path, path);
1879 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
1882 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
1883 /* Display the complete path in error message. */
1884 err = got_error_path(path, err->code);
1889 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
1893 te = got_object_tree_find_entry(tree, name);
1895 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1899 if (got_object_tree_entry_is_symlink(te)) {
1900 err = got_tree_entry_get_symlink_target(link_target, te, repo);
1903 if (!got_path_is_absolute(*link_target)) {
1905 if (asprintf(&abspath, "%s/%s", parent_path,
1906 *link_target) == -1) {
1907 err = got_error_from_errno("asprintf");
1911 *link_target = malloc(PATH_MAX);
1912 if (*link_target == NULL) {
1913 err = got_error_from_errno("malloc");
1916 err = got_canonpath(abspath, *link_target, PATH_MAX);
1925 got_object_tree_close(tree);
1928 *link_target = NULL;
1933 const struct got_error *
1934 got_object_resolve_symlinks(char **link_target, const char *path,
1935 struct got_object_id *commit_id, struct got_repository *repo)
1937 const struct got_error *err = NULL;
1938 char *next_target = NULL;
1939 int max_recursion = 40; /* matches Git */
1941 *link_target = NULL;
1944 err = resolve_symlink(&next_target,
1945 *link_target ? *link_target : path, commit_id, repo);
1950 if (--max_recursion == 0) {
1951 err = got_error_path(path, GOT_ERR_RECURSION);
1952 *link_target = NULL;
1955 *link_target = next_target;
1957 } while (next_target);
1962 const struct got_error *
1963 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1964 struct got_object_id *commit_id, const char *path,
1965 struct got_repository *repo)
1967 const struct got_error *err = NULL;
1968 struct got_pack *pack = NULL;
1969 struct got_packidx *packidx = NULL;
1970 char *path_packfile = NULL;
1971 struct got_commit_object *changed_commit = NULL;
1972 struct got_object_id *changed_commit_id = NULL;
1975 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1977 if (err->code != GOT_ERR_NO_OBJ)
1982 err = get_packfile_path(&path_packfile, packidx);
1986 pack = got_repo_get_cached_pack(repo, path_packfile);
1988 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1993 if (pack->privsep_child == NULL) {
1994 err = start_pack_privsep_child(pack, packidx);
1999 err = got_privsep_send_commit_traversal_request(
2000 pack->privsep_child->ibuf, commit_id, idx, path);
2004 err = got_privsep_recv_traversed_commits(&changed_commit,
2005 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2009 if (changed_commit) {
2011 * Cache the commit in which the path was changed.
2012 * This commit might be opened again soon.
2014 changed_commit->refcnt++;
2015 err = got_repo_cache_commit(repo, changed_commit_id,
2017 got_object_commit_close(changed_commit);
2020 free(path_packfile);
2021 free(changed_commit_id);