2 * Copyright (c) 2018, 2019, 2020 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/socket.h>
22 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
66 got_packidx_bloom_filter_cmp);
69 got_repo_get_path(struct got_repository *repo)
75 got_repo_get_path_git_dir(struct got_repository *repo)
77 return repo->path_git_dir;
81 got_repo_get_fd(struct got_repository *repo)
83 return repo->gitdir_fd;
87 got_repo_get_gitconfig_author_name(struct got_repository *repo)
89 return repo->gitconfig_author_name;
93 got_repo_get_gitconfig_author_email(struct got_repository *repo)
95 return repo->gitconfig_author_email;
99 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
101 return repo->global_gitconfig_author_name;
105 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
107 return repo->global_gitconfig_author_email;
111 got_repo_get_gitconfig_owner(struct got_repository *repo)
113 return repo->gitconfig_owner;
117 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
118 struct got_repository *repo)
120 *extensions = repo->extensions;
121 *nextensions = repo->nextensions;
125 got_repo_is_bare(struct got_repository *repo)
127 return (strcmp(repo->path, repo->path_git_dir) == 0);
131 get_path_git_child(struct got_repository *repo, const char *basename)
135 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
143 got_repo_get_path_objects(struct got_repository *repo)
145 return get_path_git_child(repo, GOT_OBJECTS_DIR);
149 got_repo_get_path_objects_pack(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
155 got_repo_get_path_refs(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_REFS_DIR);
161 got_repo_get_path_packed_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
167 get_path_head(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_HEAD_FILE);
173 got_repo_get_path_gitconfig(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_GITCONFIG);
179 got_repo_get_path_gotconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
184 const struct got_gotconfig *
185 got_repo_get_gotconfig(struct got_repository *repo)
187 return repo->gotconfig;
191 got_repo_get_gitconfig_remotes(int *nremotes,
192 const struct got_remote_repo **remotes, struct got_repository *repo)
194 *nremotes = repo->ngitconfig_remotes;
195 *remotes = repo->gitconfig_remotes;
199 is_git_repo(struct got_repository *repo)
201 const char *path_git = got_repo_get_path_git_dir(repo);
202 char *path_objects = got_repo_get_path_objects(repo);
203 char *path_refs = got_repo_get_path_refs(repo);
204 char *path_head = get_path_head(repo);
207 struct got_reference *head_ref;
209 if (lstat(path_git, &sb) == -1)
211 if (!S_ISDIR(sb.st_mode))
214 if (lstat(path_objects, &sb) == -1)
216 if (!S_ISDIR(sb.st_mode))
219 if (lstat(path_refs, &sb) == -1)
221 if (!S_ISDIR(sb.st_mode))
224 if (lstat(path_head, &sb) == -1)
226 if (!S_ISREG(sb.st_mode))
229 /* Check if the HEAD reference can be opened. */
230 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
232 got_ref_close(head_ref);
243 const struct got_error *
244 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
245 struct got_object *obj)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->objcache, id, obj);
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
262 got_repo_get_cached_object(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
268 const struct got_error *
269 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
270 struct got_tree_object *tree)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->treecache, id, tree);
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
286 struct got_tree_object *
287 got_repo_get_cached_tree(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_tree_object *)got_object_cache_get(
291 &repo->treecache, id);
294 const struct got_error *
295 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
296 struct got_commit_object *commit)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->commitcache, id, commit);
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
312 struct got_commit_object *
313 got_repo_get_cached_commit(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_commit_object *)got_object_cache_get(
317 &repo->commitcache, id);
320 const struct got_error *
321 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
322 struct got_tag_object *tag)
324 #ifndef GOT_NO_OBJ_CACHE
325 const struct got_error *err = NULL;
326 err = got_object_cache_add(&repo->tagcache, id, tag);
328 if (err->code == GOT_ERR_OBJ_EXISTS ||
329 err->code == GOT_ERR_OBJ_TOO_LARGE)
338 struct got_tag_object *
339 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
341 return (struct got_tag_object *)got_object_cache_get(
342 &repo->tagcache, id);
345 const struct got_error *
346 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
347 struct got_raw_object *raw)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->rawcache, id, raw);
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
364 struct got_raw_object *
365 got_repo_get_cached_raw_object(struct got_repository *repo,
366 struct got_object_id *id)
368 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
372 static const struct got_error *
373 open_repo(struct got_repository *repo, const char *path)
375 const struct got_error *err = NULL;
377 repo->gitdir_fd = -1;
379 /* bare git repository? */
380 repo->path_git_dir = strdup(path);
381 if (repo->path_git_dir == NULL)
382 return got_error_from_errno("strdup");
383 if (is_git_repo(repo)) {
384 repo->path = strdup(repo->path_git_dir);
385 if (repo->path == NULL) {
386 err = got_error_from_errno("strdup");
389 repo->gitdir_fd = open(repo->path_git_dir,
390 O_DIRECTORY | O_CLOEXEC);
391 if (repo->gitdir_fd == -1) {
392 err = got_error_from_errno2("open",
399 /* git repository with working tree? */
400 free(repo->path_git_dir);
401 repo->path_git_dir = NULL;
402 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
403 err = got_error_from_errno("asprintf");
406 if (is_git_repo(repo)) {
407 repo->path = strdup(path);
408 if (repo->path == NULL) {
409 err = got_error_from_errno("strdup");
412 repo->gitdir_fd = open(repo->path_git_dir,
413 O_DIRECTORY | O_CLOEXEC);
414 if (repo->gitdir_fd == -1) {
415 err = got_error_from_errno2("open",
422 err = got_error(GOT_ERR_NOT_GIT_REPO);
427 free(repo->path_git_dir);
428 repo->path_git_dir = NULL;
429 if (repo->gitdir_fd != -1)
430 close(repo->gitdir_fd);
431 repo->gitdir_fd = -1;
437 static const struct got_error *
438 parse_gitconfig_file(int *gitconfig_repository_format_version,
439 char **gitconfig_author_name, char **gitconfig_author_email,
440 struct got_remote_repo **remotes, int *nremotes,
441 char **gitconfig_owner, char ***extensions, int *nextensions,
442 const char *gitconfig_path)
444 const struct got_error *err = NULL, *child_err = NULL;
446 int imsg_fds[2] = { -1, -1 };
448 struct imsgbuf *ibuf;
450 *gitconfig_repository_format_version = 0;
455 *gitconfig_author_name = NULL;
456 *gitconfig_author_email = NULL;
462 *gitconfig_owner = NULL;
464 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
468 return got_error_from_errno2("open", gitconfig_path);
471 ibuf = calloc(1, sizeof(*ibuf));
473 err = got_error_from_errno("calloc");
477 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
478 err = got_error_from_errno("socketpair");
484 err = got_error_from_errno("fork");
486 } else if (pid == 0) {
487 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
492 if (close(imsg_fds[1]) == -1) {
493 err = got_error_from_errno("close");
497 imsg_init(ibuf, imsg_fds[0]);
499 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
504 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
508 err = got_privsep_recv_gitconfig_int(
509 gitconfig_repository_format_version, ibuf);
513 if (extensions && nextensions) {
514 err = got_privsep_send_gitconfig_repository_extensions_req(
518 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
521 if (*nextensions > 0) {
523 *extensions = calloc(*nextensions, sizeof(char *));
524 if (*extensions == NULL) {
525 err = got_error_from_errno("calloc");
528 for (i = 0; i < *nextensions; i++) {
530 err = got_privsep_recv_gitconfig_str(&ext,
534 (*extensions)[i] = ext;
539 err = got_privsep_send_gitconfig_author_name_req(ibuf);
543 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
547 err = got_privsep_send_gitconfig_author_email_req(ibuf);
551 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
555 if (remotes && nremotes) {
556 err = got_privsep_send_gitconfig_remotes_req(ibuf);
560 err = got_privsep_recv_gitconfig_remotes(remotes,
566 if (gitconfig_owner) {
567 err = got_privsep_send_gitconfig_owner_req(ibuf);
570 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
575 err = got_privsep_send_stop(imsg_fds[0]);
576 child_err = got_privsep_wait_for_child(pid);
577 if (child_err && err == NULL)
580 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
581 err = got_error_from_errno("close");
582 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
583 err = got_error_from_errno("close");
584 if (fd != -1 && close(fd) == -1 && err == NULL)
585 err = got_error_from_errno2("close", gitconfig_path);
590 static const struct got_error *
591 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
593 const struct got_error *err = NULL;
594 char *repo_gitconfig_path = NULL;
596 if (global_gitconfig_path) {
597 /* Read settings from ~/.gitconfig. */
598 int dummy_repo_version;
599 err = parse_gitconfig_file(&dummy_repo_version,
600 &repo->global_gitconfig_author_name,
601 &repo->global_gitconfig_author_email,
602 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
607 /* Read repository's .git/config file. */
608 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
609 if (repo_gitconfig_path == NULL)
610 return got_error_from_errno("got_repo_get_path_gitconfig");
612 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
613 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
614 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
615 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
616 repo_gitconfig_path);
620 free(repo_gitconfig_path);
624 static const struct got_error *
625 read_gotconfig(struct got_repository *repo)
627 const struct got_error *err = NULL;
628 char *gotconfig_path;
630 gotconfig_path = got_repo_get_path_gotconfig(repo);
631 if (gotconfig_path == NULL)
632 return got_error_from_errno("got_repo_get_path_gotconfig");
634 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
635 free(gotconfig_path);
639 /* Supported repository format extensions. */
640 static const char *repo_extensions[] = {
641 "noop", /* Got supports repository format version 1. */
642 "preciousObjects", /* Supported by gotadmin cleanup. */
643 "worktreeConfig", /* Got does not care about Git work trees. */
646 const struct got_error *
647 got_repo_open(struct got_repository **repop, const char *path,
648 const char *global_gitconfig_path)
650 struct got_repository *repo = NULL;
651 const struct got_error *err = NULL;
652 char *repo_path = NULL;
658 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
659 return got_error_from_errno("getrlimit");
661 repo = calloc(1, sizeof(*repo));
663 err = got_error_from_errno("calloc");
667 RB_INIT(&repo->packidx_bloom_filters);
669 for (i = 0; i < nitems(repo->privsep_children); i++) {
670 memset(&repo->privsep_children[i], 0,
671 sizeof(repo->privsep_children[0]));
672 repo->privsep_children[i].imsg_fd = -1;
675 err = got_object_cache_init(&repo->objcache,
676 GOT_OBJECT_CACHE_TYPE_OBJ);
679 err = got_object_cache_init(&repo->treecache,
680 GOT_OBJECT_CACHE_TYPE_TREE);
683 err = got_object_cache_init(&repo->commitcache,
684 GOT_OBJECT_CACHE_TYPE_COMMIT);
687 err = got_object_cache_init(&repo->tagcache,
688 GOT_OBJECT_CACHE_TYPE_TAG);
691 err = got_object_cache_init(&repo->rawcache,
692 GOT_OBJECT_CACHE_TYPE_RAW);
696 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
697 if (repo->pack_cache_size > rl.rlim_cur / 8)
698 repo->pack_cache_size = rl.rlim_cur / 8;
700 repo_path = realpath(path, NULL);
701 if (repo_path == NULL) {
702 err = got_error_from_errno2("realpath", path);
709 err = open_repo(repo, repo_path);
712 if (err->code != GOT_ERR_NOT_GIT_REPO)
714 if (repo_path[0] == '/' && repo_path[1] == '\0') {
715 err = got_error(GOT_ERR_NOT_GIT_REPO);
718 err = got_path_dirname(&parent_path, repo_path);
722 repo_path = parent_path;
725 err = read_gotconfig(repo);
729 err = read_gitconfig(repo, global_gitconfig_path);
732 if (repo->gitconfig_repository_format_version != 0)
733 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
734 for (i = 0; i < repo->nextensions; i++) {
735 char *ext = repo->extensions[i];
736 int j, supported = 0;
737 for (j = 0; j < nitems(repo_extensions); j++) {
738 if (strcmp(ext, repo_extensions[j]) == 0) {
744 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
750 got_repo_close(repo);
757 const struct got_error *
758 got_repo_close(struct got_repository *repo)
760 const struct got_error *err = NULL, *child_err;
761 struct got_packidx_bloom_filter *bf;
764 for (i = 0; i < repo->pack_cache_size; i++) {
765 if (repo->packidx_cache[i] == NULL)
767 got_packidx_close(repo->packidx_cache[i]);
770 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
771 &repo->packidx_bloom_filters))) {
772 RB_REMOVE(got_packidx_bloom_filter_tree,
773 &repo->packidx_bloom_filters, bf);
778 for (i = 0; i < repo->pack_cache_size; i++) {
779 if (repo->packs[i].path_packfile == NULL)
781 got_pack_close(&repo->packs[i]);
785 free(repo->path_git_dir);
787 got_object_cache_close(&repo->objcache);
788 got_object_cache_close(&repo->treecache);
789 got_object_cache_close(&repo->commitcache);
790 got_object_cache_close(&repo->tagcache);
791 got_object_cache_close(&repo->rawcache);
793 for (i = 0; i < nitems(repo->privsep_children); i++) {
794 if (repo->privsep_children[i].imsg_fd == -1)
796 imsg_clear(repo->privsep_children[i].ibuf);
797 free(repo->privsep_children[i].ibuf);
798 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
799 child_err = got_privsep_wait_for_child(
800 repo->privsep_children[i].pid);
801 if (child_err && err == NULL)
803 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
805 err = got_error_from_errno("close");
808 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
810 err = got_error_from_errno("close");
813 got_gotconfig_free(repo->gotconfig);
814 free(repo->gitconfig_author_name);
815 free(repo->gitconfig_author_email);
816 for (i = 0; i < repo->ngitconfig_remotes; i++)
817 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
818 free(repo->gitconfig_remotes);
819 for (i = 0; i < repo->nextensions; i++)
820 free(repo->extensions[i]);
821 free(repo->extensions);
828 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
834 free(repo->fetch_url);
835 repo->fetch_url = NULL;
836 free(repo->send_url);
837 repo->send_url = NULL;
838 for (i = 0; i < repo->nfetch_branches; i++)
839 free(repo->fetch_branches[i]);
840 free(repo->fetch_branches);
841 repo->fetch_branches = NULL;
842 repo->nfetch_branches = 0;
843 for (i = 0; i < repo->nsend_branches; i++)
844 free(repo->send_branches[i]);
845 free(repo->send_branches);
846 repo->send_branches = NULL;
847 repo->nsend_branches = 0;
850 const struct got_error *
851 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
852 const char *input_path)
854 const struct got_error *err = NULL;
855 const char *repo_abspath = NULL;
857 char *canonpath, *path = NULL;
859 *in_repo_path = NULL;
861 canonpath = strdup(input_path);
862 if (canonpath == NULL) {
863 err = got_error_from_errno("strdup");
866 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
870 repo_abspath = got_repo_get_path(repo);
872 if (canonpath[0] == '\0') {
873 path = strdup(canonpath);
875 err = got_error_from_errno("strdup");
879 path = realpath(canonpath, NULL);
881 if (errno != ENOENT) {
882 err = got_error_from_errno2("realpath",
887 * Path is not on disk.
888 * Assume it is already relative to repository root.
890 path = strdup(canonpath);
892 err = got_error_from_errno("strdup");
897 repolen = strlen(repo_abspath);
901 if (strcmp(path, repo_abspath) == 0) {
905 err = got_error_from_errno("strdup");
908 } else if (len > repolen &&
909 got_path_is_child(path, repo_abspath, repolen)) {
910 /* Matched an on-disk path inside repository. */
911 if (got_repo_is_bare(repo)) {
913 * Matched an on-disk path inside repository
914 * database. Treat input as repository-relative.
921 /* Strip common prefix with repository path. */
922 err = got_path_skip_common_ancestor(&child,
931 * Matched unrelated on-disk path.
932 * Treat input as repository-relative.
940 /* Make in-repository path absolute */
941 if (path[0] != '/') {
943 if (asprintf(&abspath, "/%s", path) == -1) {
944 err = got_error_from_errno("asprintf");
956 *in_repo_path = path;
960 static const struct got_error *
961 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
962 const char *path_packidx)
964 const struct got_error *err = NULL;
967 for (i = 0; i < repo->pack_cache_size; i++) {
968 if (repo->packidx_cache[i] == NULL)
970 if (strcmp(repo->packidx_cache[i]->path_packidx,
971 path_packidx) == 0) {
972 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
975 if (i == repo->pack_cache_size) {
976 i = repo->pack_cache_size - 1;
977 err = got_packidx_close(repo->packidx_cache[i]);
982 repo->packidx_cache[i] = packidx;
988 got_repo_is_packidx_filename(const char *name, size_t len)
990 if (len != GOT_PACKIDX_NAMELEN)
993 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
996 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
997 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1003 static struct got_packidx_bloom_filter *
1004 get_packidx_bloom_filter(struct got_repository *repo,
1005 const char *path, size_t path_len)
1007 struct got_packidx_bloom_filter key;
1009 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1010 return NULL; /* XXX */
1011 key.path_len = path_len;
1013 return RB_FIND(got_packidx_bloom_filter_tree,
1014 &repo->packidx_bloom_filters, &key);
1018 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1019 const char *path_packidx, struct got_object_id *id)
1021 struct got_packidx_bloom_filter *bf;
1023 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1025 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1027 /* No bloom filter means this pack index must be searched. */
1031 static const struct got_error *
1032 add_packidx_bloom_filter(struct got_repository *repo,
1033 struct got_packidx *packidx, const char *path_packidx)
1035 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1036 struct got_packidx_bloom_filter *bf;
1040 * Don't use bloom filters for very large pack index files.
1041 * Large pack files will contain a relatively large fraction
1042 * of our objects so we will likely need to visit them anyway.
1043 * The more objects a pack file contains the higher the probability
1044 * of a false-positive match from the bloom filter. And reading
1045 * all object IDs from a large pack index file can be expensive.
1047 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1050 /* Do we already have a filter for this pack index? */
1051 if (get_packidx_bloom_filter(repo, path_packidx,
1052 strlen(path_packidx)) != NULL)
1055 bf = calloc(1, sizeof(*bf));
1057 return got_error_from_errno("calloc");
1058 bf->bloom = calloc(1, sizeof(*bf->bloom));
1059 if (bf->bloom == NULL) {
1061 return got_error_from_errno("calloc");
1064 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1065 if (len >= sizeof(bf->path)) {
1068 return got_error(GOT_ERR_NO_SPACE);
1072 /* Minimum size supported by our bloom filter is 1000 entries. */
1073 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1074 for (i = 0; i < nobjects; i++) {
1075 struct got_packidx_object_id *id;
1076 id = &packidx->hdr.sorted_ids[i];
1077 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1080 RB_INSERT(got_packidx_bloom_filter_tree,
1081 &repo->packidx_bloom_filters, bf);
1085 const struct got_error *
1086 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1087 struct got_repository *repo, struct got_object_id *id)
1089 const struct got_error *err;
1090 DIR *packdir = NULL;
1091 struct dirent *dent;
1096 /* Search pack index cache. */
1097 for (i = 0; i < repo->pack_cache_size; i++) {
1098 if (repo->packidx_cache[i] == NULL)
1100 if (!got_repo_check_packidx_bloom_filter(repo,
1101 repo->packidx_cache[i]->path_packidx, id))
1102 continue; /* object will not be found in this index */
1103 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1105 *packidx = repo->packidx_cache[i];
1107 * Move this cache entry to the front. Repeatedly
1108 * searching a wrong pack index can be expensive.
1111 memmove(&repo->packidx_cache[1],
1112 &repo->packidx_cache[0],
1113 i * sizeof(repo->packidx_cache[0]));
1114 repo->packidx_cache[0] = *packidx;
1119 /* No luck. Search the filesystem. */
1121 packdir_fd = openat(got_repo_get_fd(repo),
1122 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1123 if (packdir_fd == -1) {
1124 if (errno == ENOENT)
1125 err = got_error_no_obj(id);
1127 err = got_error_from_errno_fmt("openat: %s/%s",
1128 got_repo_get_path_git_dir(repo),
1129 GOT_OBJECTS_PACK_DIR);
1133 packdir = fdopendir(packdir_fd);
1134 if (packdir == NULL) {
1135 err = got_error_from_errno("fdopendir");
1139 while ((dent = readdir(packdir)) != NULL) {
1142 if (!got_repo_is_packidx_filename(dent->d_name,
1143 strlen(dent->d_name)))
1146 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1147 dent->d_name) == -1) {
1148 err = got_error_from_errno("asprintf");
1152 if (!got_repo_check_packidx_bloom_filter(repo,
1153 path_packidx, id)) {
1155 continue; /* object will not be found in this index */
1158 for (i = 0; i < repo->pack_cache_size; i++) {
1159 if (repo->packidx_cache[i] == NULL)
1161 if (strcmp(repo->packidx_cache[i]->path_packidx,
1162 path_packidx) == 0) {
1169 continue; /* already searched */
1172 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1179 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1185 err = cache_packidx(repo, *packidx, path_packidx);
1190 *idx = got_packidx_get_object_idx(*packidx, id);
1192 err = NULL; /* found the object */
1197 err = got_error_no_obj(id);
1199 if (packdir && closedir(packdir) != 0 && err == NULL)
1200 err = got_error_from_errno("closedir");
1204 const struct got_error *
1205 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1206 struct got_repository *repo)
1208 const struct got_error *err = NULL;
1209 DIR *packdir = NULL;
1210 struct dirent *dent;
1211 char *path_packidx = NULL;
1214 packdir_fd = openat(got_repo_get_fd(repo),
1215 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1216 if (packdir_fd == -1) {
1217 return got_error_from_errno_fmt("openat: %s/%s",
1218 got_repo_get_path_git_dir(repo),
1219 GOT_OBJECTS_PACK_DIR);
1222 packdir = fdopendir(packdir_fd);
1223 if (packdir == NULL) {
1224 err = got_error_from_errno("fdopendir");
1228 while ((dent = readdir(packdir)) != NULL) {
1229 if (!got_repo_is_packidx_filename(dent->d_name,
1230 strlen(dent->d_name)))
1233 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1234 dent->d_name) == -1) {
1235 err = got_error_from_errno("asprintf");
1236 path_packidx = NULL;
1240 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1247 if (packdir && closedir(packdir) != 0 && err == NULL)
1248 err = got_error_from_errno("closedir");
1252 const struct got_error *
1253 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1254 struct got_repository *repo)
1256 const struct got_error *err;
1261 /* Search pack index cache. */
1262 for (i = 0; i < repo->pack_cache_size; i++) {
1263 if (repo->packidx_cache[i] == NULL)
1265 if (strcmp(repo->packidx_cache[i]->path_packidx,
1266 path_packidx) == 0) {
1267 *packidx = repo->packidx_cache[i];
1271 /* No luck. Search the filesystem. */
1273 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1278 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1282 err = cache_packidx(repo, *packidx, path_packidx);
1285 got_packidx_close(*packidx);
1291 static const struct got_error *
1292 read_packfile_hdr(int fd, struct got_packidx *packidx)
1294 const struct got_error *err = NULL;
1295 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1296 struct got_packfile_hdr hdr;
1299 n = read(fd, &hdr, sizeof(hdr));
1301 return got_error_from_errno("read");
1302 if (n != sizeof(hdr))
1303 return got_error(GOT_ERR_BAD_PACKFILE);
1305 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1306 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1307 be32toh(hdr.nobjects) != totobj)
1308 err = got_error(GOT_ERR_BAD_PACKFILE);
1313 static const struct got_error *
1314 open_packfile(int *fd, struct got_repository *repo,
1315 const char *relpath, struct got_packidx *packidx)
1317 const struct got_error *err = NULL;
1319 *fd = openat(got_repo_get_fd(repo), relpath,
1320 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1322 return got_error_from_errno_fmt("openat: %s/%s",
1323 got_repo_get_path_git_dir(repo), relpath);
1326 err = read_packfile_hdr(*fd, packidx);
1336 const struct got_error *
1337 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1338 const char *path_packfile, struct got_packidx *packidx)
1340 const struct got_error *err = NULL;
1341 struct got_pack *pack = NULL;
1348 for (i = 0; i < repo->pack_cache_size; i++) {
1349 pack = &repo->packs[i];
1350 if (pack->path_packfile == NULL)
1352 if (strcmp(pack->path_packfile, path_packfile) == 0)
1353 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1356 if (i == repo->pack_cache_size) {
1357 err = got_pack_close(&repo->packs[i - 1]);
1360 memmove(&repo->packs[1], &repo->packs[0],
1361 sizeof(repo->packs) - sizeof(repo->packs[0]));
1365 pack = &repo->packs[i];
1367 pack->path_packfile = strdup(path_packfile);
1368 if (pack->path_packfile == NULL) {
1369 err = got_error_from_errno("strdup");
1373 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1377 if (fstat(pack->fd, &sb) != 0) {
1378 err = got_error_from_errno("fstat");
1381 pack->filesize = sb.st_size;
1383 pack->privsep_child = NULL;
1385 #ifndef GOT_PACK_NO_MMAP
1386 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1388 if (pack->map == MAP_FAILED) {
1389 if (errno != ENOMEM) {
1390 err = got_error_from_errno("mmap");
1393 pack->map = NULL; /* fall back to read(2) */
1399 free(pack->path_packfile);
1400 memset(pack, 0, sizeof(*pack));
1408 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1410 struct got_pack *pack = NULL;
1413 for (i = 0; i < repo->pack_cache_size; i++) {
1414 pack = &repo->packs[i];
1415 if (pack->path_packfile == NULL)
1417 if (strcmp(pack->path_packfile, path_packfile) == 0)
1424 const struct got_error *
1425 got_repo_init(const char *repo_path)
1427 const struct got_error *err = NULL;
1428 const char *dirnames[] = {
1430 GOT_OBJECTS_PACK_DIR,
1433 const char *description_str = "Unnamed repository; "
1434 "edit this file 'description' to name the repository.";
1435 const char *headref_str = "ref: refs/heads/main";
1436 const char *gitconfig_str = "[core]\n"
1437 "\trepositoryformatversion = 0\n"
1438 "\tfilemode = true\n"
1443 if (!got_path_dir_is_empty(repo_path))
1444 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1446 for (i = 0; i < nitems(dirnames); i++) {
1447 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1448 return got_error_from_errno("asprintf");
1450 err = got_path_mkdir(path);
1456 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1457 return got_error_from_errno("asprintf");
1458 err = got_path_create_file(path, description_str);
1463 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1464 return got_error_from_errno("asprintf");
1465 err = got_path_create_file(path, headref_str);
1470 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1471 return got_error_from_errno("asprintf");
1472 err = got_path_create_file(path, gitconfig_str);
1480 static const struct got_error *
1481 match_packed_object(struct got_object_id **unique_id,
1482 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1484 const struct got_error *err = NULL;
1485 DIR *packdir = NULL;
1486 struct dirent *dent;
1488 struct got_object_id_queue matched_ids;
1491 STAILQ_INIT(&matched_ids);
1493 packdir_fd = openat(got_repo_get_fd(repo),
1494 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1495 if (packdir_fd == -1) {
1496 if (errno != ENOENT) {
1497 err = got_error_from_errno2("openat",
1498 GOT_OBJECTS_PACK_DIR);
1503 packdir = fdopendir(packdir_fd);
1504 if (packdir == NULL) {
1505 err = got_error_from_errno("fdopendir");
1509 while ((dent = readdir(packdir)) != NULL) {
1510 struct got_packidx *packidx;
1511 struct got_object_qid *qid;
1513 if (!got_repo_is_packidx_filename(dent->d_name,
1514 strlen(dent->d_name)))
1517 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1518 dent->d_name) == -1) {
1519 err = got_error_from_errno("strdup");
1523 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1529 err = got_packidx_match_id_str_prefix(&matched_ids,
1530 packidx, id_str_prefix);
1532 got_packidx_close(packidx);
1535 err = got_packidx_close(packidx);
1539 STAILQ_FOREACH(qid, &matched_ids, entry) {
1540 if (obj_type != GOT_OBJ_TYPE_ANY) {
1542 err = got_object_get_type(&matched_type, repo,
1546 if (matched_type != obj_type)
1549 if (*unique_id == NULL) {
1550 *unique_id = got_object_id_dup(qid->id);
1551 if (*unique_id == NULL) {
1552 err = got_error_from_errno("malloc");
1556 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1557 continue; /* packed multiple times */
1558 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1564 got_object_id_queue_free(&matched_ids);
1565 if (packdir && closedir(packdir) != 0 && err == NULL)
1566 err = got_error_from_errno("closedir");
1574 static const struct got_error *
1575 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1576 const char *object_dir, const char *id_str_prefix, int obj_type,
1577 struct got_repository *repo)
1579 const struct got_error *err = NULL;
1582 struct dirent *dent;
1583 struct got_object_id id;
1585 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1586 err = got_error_from_errno("asprintf");
1590 dir = opendir(path);
1592 if (errno == ENOENT) {
1596 err = got_error_from_errno2("opendir", path);
1599 while ((dent = readdir(dir)) != NULL) {
1603 if (strcmp(dent->d_name, ".") == 0 ||
1604 strcmp(dent->d_name, "..") == 0)
1607 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1608 err = got_error_from_errno("asprintf");
1612 if (!got_parse_sha1_digest(id.sha1, id_str))
1616 * Directory entries do not necessarily appear in
1617 * sorted order, so we must iterate over all of them.
1619 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1625 if (*unique_id == NULL) {
1626 if (obj_type != GOT_OBJ_TYPE_ANY) {
1628 err = got_object_get_type(&matched_type, repo,
1632 if (matched_type != obj_type)
1635 *unique_id = got_object_id_dup(&id);
1636 if (*unique_id == NULL) {
1637 err = got_error_from_errno("got_object_id_dup");
1642 if (got_object_id_cmp(*unique_id, &id) == 0)
1643 continue; /* both packed and loose */
1644 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1650 if (dir && closedir(dir) != 0 && err == NULL)
1651 err = got_error_from_errno("closedir");
1660 const struct got_error *
1661 got_repo_match_object_id_prefix(struct got_object_id **id,
1662 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1664 const struct got_error *err = NULL;
1665 char *path_objects = got_repo_get_path_objects(repo);
1666 char *object_dir = NULL;
1672 for (i = 0; i < strlen(id_str_prefix); i++) {
1673 if (isxdigit((unsigned char)id_str_prefix[i]))
1675 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1678 len = strlen(id_str_prefix);
1680 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1683 object_dir = strndup(id_str_prefix, 2);
1684 if (object_dir == NULL) {
1685 err = got_error_from_errno("strdup");
1688 err = match_loose_object(id, path_objects, object_dir,
1689 id_str_prefix, obj_type, repo);
1690 } else if (len == 1) {
1692 for (i = 0; i < 0xf; i++) {
1693 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1695 err = got_error_from_errno("asprintf");
1698 err = match_packed_object(id, repo, object_dir,
1702 err = match_loose_object(id, path_objects, object_dir,
1703 id_str_prefix, obj_type, repo);
1708 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1716 } else if (*id == NULL) {
1718 case GOT_OBJ_TYPE_BLOB:
1719 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1720 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1722 case GOT_OBJ_TYPE_TREE:
1723 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1724 GOT_OBJ_LABEL_TREE, id_str_prefix);
1726 case GOT_OBJ_TYPE_COMMIT:
1727 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1728 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1730 case GOT_OBJ_TYPE_TAG:
1731 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1732 GOT_OBJ_LABEL_TAG, id_str_prefix);
1735 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1743 const struct got_error *
1744 got_repo_match_object_id(struct got_object_id **id, char **label,
1745 const char *id_str, int obj_type, struct got_reflist_head *refs,
1746 struct got_repository *repo)
1748 const struct got_error *err;
1749 struct got_tag_object *tag;
1750 struct got_reference *ref = NULL;
1757 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1760 *id = got_object_id_dup(
1761 got_object_tag_get_object_id(tag));
1763 err = got_error_from_errno("got_object_id_dup");
1764 else if (label && asprintf(label, "refs/tags/%s",
1765 got_object_tag_get_name(tag)) == -1) {
1766 err = got_error_from_errno("asprintf");
1770 got_object_tag_close(tag);
1772 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1773 err->code != GOT_ERR_NO_OBJ)
1777 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1779 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1781 err = got_ref_open(&ref, repo, id_str, 0);
1785 *label = strdup(got_ref_get_name(ref));
1786 if (*label == NULL) {
1787 err = got_error_from_errno("strdup");
1791 err = got_ref_resolve(id, repo, ref);
1793 err = got_object_id_str(label, *id);
1794 if (*label == NULL) {
1795 err = got_error_from_errno("strdup");
1805 const struct got_error *
1806 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1807 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1809 const struct got_error *err = NULL;
1810 struct got_reflist_entry *re;
1811 struct got_object_id *tag_id;
1812 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1816 TAILQ_FOREACH(re, refs, entry) {
1817 const char *refname;
1818 refname = got_ref_get_name(re->ref);
1819 if (got_ref_is_symbolic(re->ref))
1821 if (strncmp(refname, "refs/tags/", 10) != 0)
1823 if (!name_is_absolute)
1824 refname += strlen("refs/tags/");
1825 if (strcmp(refname, name) != 0)
1827 err = got_ref_resolve(&tag_id, repo, re->ref);
1830 err = got_object_open_as_tag(tag, repo, tag_id);
1834 if (obj_type == GOT_OBJ_TYPE_ANY ||
1835 got_object_tag_get_object_type(*tag) == obj_type)
1837 got_object_tag_close(*tag);
1841 if (err == NULL && *tag == NULL)
1842 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1843 GOT_OBJ_LABEL_TAG, name);
1847 static const struct got_error *
1848 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1849 const char *name, mode_t mode, struct got_object_id *blob_id)
1851 const struct got_error *err = NULL;
1855 *new_te = calloc(1, sizeof(**new_te));
1856 if (*new_te == NULL)
1857 return got_error_from_errno("calloc");
1859 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1860 sizeof((*new_te)->name)) {
1861 err = got_error(GOT_ERR_NO_SPACE);
1865 if (S_ISLNK(mode)) {
1866 (*new_te)->mode = S_IFLNK;
1868 (*new_te)->mode = S_IFREG;
1869 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1871 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1873 if (err && *new_te) {
1880 static const struct got_error *
1881 import_file(struct got_tree_entry **new_te, struct dirent *de,
1882 const char *path, struct got_repository *repo)
1884 const struct got_error *err;
1885 struct got_object_id *blob_id = NULL;
1889 if (asprintf(&filepath, "%s%s%s", path,
1890 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1891 return got_error_from_errno("asprintf");
1893 if (lstat(filepath, &sb) != 0) {
1894 err = got_error_from_errno2("lstat", path);
1898 err = got_object_blob_create(&blob_id, filepath, repo);
1902 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1911 static const struct got_error *
1912 insert_tree_entry(struct got_tree_entry *new_te,
1913 struct got_pathlist_head *paths)
1915 const struct got_error *err = NULL;
1916 struct got_pathlist_entry *new_pe;
1918 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1922 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1926 static const struct got_error *write_tree(struct got_object_id **,
1927 const char *, struct got_pathlist_head *, struct got_repository *,
1928 got_repo_import_cb progress_cb, void *progress_arg);
1930 static const struct got_error *
1931 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1932 const char *path, struct got_pathlist_head *ignores,
1933 struct got_repository *repo,
1934 got_repo_import_cb progress_cb, void *progress_arg)
1936 const struct got_error *err;
1937 struct got_object_id *id = NULL;
1940 if (asprintf(&subdirpath, "%s%s%s", path,
1941 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1942 return got_error_from_errno("asprintf");
1944 (*new_te) = calloc(1, sizeof(**new_te));
1945 if (*new_te == NULL)
1946 return got_error_from_errno("calloc");
1947 (*new_te)->mode = S_IFDIR;
1948 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1949 sizeof((*new_te)->name)) {
1950 err = got_error(GOT_ERR_NO_SPACE);
1953 err = write_tree(&id, subdirpath, ignores, repo,
1954 progress_cb, progress_arg);
1957 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1969 static const struct got_error *
1970 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1971 struct got_pathlist_head *ignores, struct got_repository *repo,
1972 got_repo_import_cb progress_cb, void *progress_arg)
1974 const struct got_error *err = NULL;
1978 struct got_tree_entry *new_te = NULL;
1979 struct got_pathlist_head paths;
1980 struct got_pathlist_entry *pe;
1982 *new_tree_id = NULL;
1986 dir = opendir(path_dir);
1988 err = got_error_from_errno2("opendir", path_dir);
1993 while ((de = readdir(dir)) != NULL) {
1997 if (strcmp(de->d_name, ".") == 0 ||
1998 strcmp(de->d_name, "..") == 0)
2001 TAILQ_FOREACH(pe, ignores, entry) {
2002 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2010 err = got_path_dirent_type(&type, path_dir, de);
2014 if (type == DT_DIR) {
2015 err = import_subdir(&new_te, de, path_dir,
2016 ignores, repo, progress_cb, progress_arg);
2018 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2023 } else if (type == DT_REG || type == DT_LNK) {
2024 err = import_file(&new_te, de, path_dir, repo);
2030 err = insert_tree_entry(new_te, &paths);
2036 if (TAILQ_EMPTY(&paths)) {
2037 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2038 "cannot create tree without any entries");
2042 TAILQ_FOREACH(pe, &paths, entry) {
2043 struct got_tree_entry *te = pe->data;
2045 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2047 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2048 err = got_error_from_errno("asprintf");
2051 err = (*progress_cb)(progress_arg, path);
2057 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2061 got_pathlist_free(&paths);
2065 const struct got_error *
2066 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2067 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2068 struct got_repository *repo, got_repo_import_cb progress_cb,
2071 const struct got_error *err;
2072 struct got_object_id *new_tree_id;
2074 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2075 progress_cb, progress_arg);
2079 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2080 author, time(NULL), author, time(NULL), logmsg, repo);
2085 const struct got_error *
2086 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2087 struct got_repository *repo)
2089 const struct got_error *err = NULL;
2090 char *path_objects = NULL, *path = NULL;
2092 struct got_object_id id;
2098 path_objects = got_repo_get_path_objects(repo);
2099 if (path_objects == NULL)
2100 return got_error_from_errno("got_repo_get_path_objects");
2102 for (i = 0; i <= 0xff; i++) {
2103 struct dirent *dent;
2105 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2106 err = got_error_from_errno("asprintf");
2110 dir = opendir(path);
2112 if (errno == ENOENT) {
2116 err = got_error_from_errno2("opendir", path);
2120 while ((dent = readdir(dir)) != NULL) {
2125 if (strcmp(dent->d_name, ".") == 0 ||
2126 strcmp(dent->d_name, "..") == 0)
2129 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2130 err = got_error_from_errno("asprintf");
2134 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2140 err = got_object_open_loose_fd(&fd, &id, repo);
2144 if (fstat(fd, &sb) == -1) {
2145 err = got_error_from_errno("fstat");
2150 (*ondisk_size) += sb.st_size;
2152 if (close(fd) == -1) {
2153 err = got_error_from_errno("close");
2158 if (closedir(dir) != 0) {
2159 err = got_error_from_errno("closedir");
2168 if (dir && closedir(dir) != 0 && err == NULL)
2169 err = got_error_from_errno("closedir");
2180 const struct got_error *
2181 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2182 off_t *total_packsize, struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 DIR *packdir = NULL;
2186 struct dirent *dent;
2187 struct got_packidx *packidx = NULL;
2189 char *path_packfile;
2195 *total_packsize = 0;
2197 packdir_fd = openat(got_repo_get_fd(repo),
2198 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2199 if (packdir_fd == -1) {
2200 return got_error_from_errno_fmt("openat: %s/%s",
2201 got_repo_get_path_git_dir(repo),
2202 GOT_OBJECTS_PACK_DIR);
2205 packdir = fdopendir(packdir_fd);
2206 if (packdir == NULL) {
2207 err = got_error_from_errno("fdopendir");
2211 while ((dent = readdir(packdir)) != NULL) {
2212 if (!got_repo_is_packidx_filename(dent->d_name,
2213 strlen(dent->d_name)))
2216 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2217 dent->d_name) == -1) {
2218 err = got_error_from_errno("asprintf");
2222 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2228 if (fstat(packidx->fd, &sb) == -1)
2230 *total_packsize += sb.st_size;
2232 err = got_packidx_get_packfile_path(&path_packfile,
2233 packidx->path_packidx);
2237 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2239 free(path_packfile);
2242 free(path_packfile);
2243 *total_packsize += sb.st_size;
2245 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2249 got_packidx_close(packidx);
2254 got_packidx_close(packidx);
2255 if (packdir && closedir(packdir) != 0 && err == NULL)
2256 err = got_error_from_errno("closedir");
2260 *total_packsize = 0;
2265 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2266 got_packidx_bloom_filter_cmp);