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>
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_cancel.h"
46 #include "got_object.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_object_cache.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_gotconfig.h"
61 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
65 got_packidx_bloom_filter_cmp);
68 got_repo_get_path(struct got_repository *repo)
74 got_repo_get_path_git_dir(struct got_repository *repo)
76 return repo->path_git_dir;
80 got_repo_get_fd(struct got_repository *repo)
82 return repo->gitdir_fd;
86 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 return repo->gitconfig_author_name;
92 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 return repo->gitconfig_author_email;
98 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
100 return repo->global_gitconfig_author_name;
104 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
106 return repo->global_gitconfig_author_email;
110 got_repo_get_gitconfig_owner(struct got_repository *repo)
112 return repo->gitconfig_owner;
116 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
117 struct got_repository *repo)
119 *extensions = repo->extensions;
120 *nextensions = repo->nextensions;
124 got_repo_is_bare(struct got_repository *repo)
126 return (strcmp(repo->path, repo->path_git_dir) == 0);
130 get_path_git_child(struct got_repository *repo, const char *basename)
134 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
142 got_repo_get_path_objects(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_DIR);
148 got_repo_get_path_objects_pack(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
154 got_repo_get_path_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_REFS_DIR);
160 got_repo_get_path_packed_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
166 get_path_head(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_HEAD_FILE);
172 got_repo_get_path_gitconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GITCONFIG);
178 got_repo_get_path_gotconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
183 const struct got_gotconfig *
184 got_repo_get_gotconfig(struct got_repository *repo)
186 return repo->gotconfig;
190 got_repo_get_gitconfig_remotes(int *nremotes,
191 const struct got_remote_repo **remotes, struct got_repository *repo)
193 *nremotes = repo->ngitconfig_remotes;
194 *remotes = repo->gitconfig_remotes;
198 is_git_repo(struct got_repository *repo)
200 const char *path_git = got_repo_get_path_git_dir(repo);
201 char *path_objects = got_repo_get_path_objects(repo);
202 char *path_refs = got_repo_get_path_refs(repo);
203 char *path_head = get_path_head(repo);
206 struct got_reference *head_ref;
208 if (lstat(path_git, &sb) == -1)
210 if (!S_ISDIR(sb.st_mode))
213 if (lstat(path_objects, &sb) == -1)
215 if (!S_ISDIR(sb.st_mode))
218 if (lstat(path_refs, &sb) == -1)
220 if (!S_ISDIR(sb.st_mode))
223 if (lstat(path_head, &sb) == -1)
225 if (!S_ISREG(sb.st_mode))
228 /* Check if the HEAD reference can be opened. */
229 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
231 got_ref_close(head_ref);
242 const struct got_error *
243 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
244 struct got_object *obj)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->objcache, id, obj);
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
261 got_repo_get_cached_object(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
267 const struct got_error *
268 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
269 struct got_tree_object *tree)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = got_object_cache_add(&repo->treecache, id, tree);
275 if (err->code == GOT_ERR_OBJ_EXISTS ||
276 err->code == GOT_ERR_OBJ_TOO_LARGE)
285 struct got_tree_object *
286 got_repo_get_cached_tree(struct got_repository *repo,
287 struct got_object_id *id)
289 return (struct got_tree_object *)got_object_cache_get(
290 &repo->treecache, id);
293 const struct got_error *
294 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
295 struct got_commit_object *commit)
297 #ifndef GOT_NO_OBJ_CACHE
298 const struct got_error *err = NULL;
299 err = got_object_cache_add(&repo->commitcache, id, commit);
301 if (err->code == GOT_ERR_OBJ_EXISTS ||
302 err->code == GOT_ERR_OBJ_TOO_LARGE)
311 struct got_commit_object *
312 got_repo_get_cached_commit(struct got_repository *repo,
313 struct got_object_id *id)
315 return (struct got_commit_object *)got_object_cache_get(
316 &repo->commitcache, id);
319 const struct got_error *
320 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
321 struct got_tag_object *tag)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->tagcache, id, tag);
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
337 struct got_tag_object *
338 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
340 return (struct got_tag_object *)got_object_cache_get(
341 &repo->tagcache, id);
344 const struct got_error *
345 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
346 struct got_raw_object *raw)
348 #ifndef GOT_NO_OBJ_CACHE
349 const struct got_error *err = NULL;
350 err = got_object_cache_add(&repo->rawcache, id, raw);
352 if (err->code == GOT_ERR_OBJ_EXISTS ||
353 err->code == GOT_ERR_OBJ_TOO_LARGE)
363 struct got_raw_object *
364 got_repo_get_cached_raw_object(struct got_repository *repo,
365 struct got_object_id *id)
367 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
371 static const struct got_error *
372 open_repo(struct got_repository *repo, const char *path)
374 const struct got_error *err = NULL;
376 repo->gitdir_fd = -1;
378 /* bare git repository? */
379 repo->path_git_dir = strdup(path);
380 if (repo->path_git_dir == NULL)
381 return got_error_from_errno("strdup");
382 if (is_git_repo(repo)) {
383 repo->path = strdup(repo->path_git_dir);
384 if (repo->path == NULL) {
385 err = got_error_from_errno("strdup");
388 repo->gitdir_fd = open(repo->path_git_dir,
389 O_DIRECTORY | O_CLOEXEC);
390 if (repo->gitdir_fd == -1) {
391 err = got_error_from_errno2("open",
398 /* git repository with working tree? */
399 free(repo->path_git_dir);
400 repo->path_git_dir = NULL;
401 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
402 err = got_error_from_errno("asprintf");
405 if (is_git_repo(repo)) {
406 repo->path = strdup(path);
407 if (repo->path == NULL) {
408 err = got_error_from_errno("strdup");
411 repo->gitdir_fd = open(repo->path_git_dir,
412 O_DIRECTORY | O_CLOEXEC);
413 if (repo->gitdir_fd == -1) {
414 err = got_error_from_errno2("open",
421 err = got_error(GOT_ERR_NOT_GIT_REPO);
426 free(repo->path_git_dir);
427 repo->path_git_dir = NULL;
428 if (repo->gitdir_fd != -1)
429 close(repo->gitdir_fd);
430 repo->gitdir_fd = -1;
436 static const struct got_error *
437 parse_gitconfig_file(int *gitconfig_repository_format_version,
438 char **gitconfig_author_name, char **gitconfig_author_email,
439 struct got_remote_repo **remotes, int *nremotes,
440 char **gitconfig_owner, char ***extensions, int *nextensions,
441 const char *gitconfig_path)
443 const struct got_error *err = NULL, *child_err = NULL;
445 int imsg_fds[2] = { -1, -1 };
447 struct imsgbuf *ibuf;
449 *gitconfig_repository_format_version = 0;
454 *gitconfig_author_name = NULL;
455 *gitconfig_author_email = NULL;
461 *gitconfig_owner = NULL;
463 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
467 return got_error_from_errno2("open", gitconfig_path);
470 ibuf = calloc(1, sizeof(*ibuf));
472 err = got_error_from_errno("calloc");
476 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
477 err = got_error_from_errno("socketpair");
483 err = got_error_from_errno("fork");
485 } else if (pid == 0) {
486 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
491 if (close(imsg_fds[1]) == -1) {
492 err = got_error_from_errno("close");
496 imsg_init(ibuf, imsg_fds[0]);
498 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
503 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
507 err = got_privsep_recv_gitconfig_int(
508 gitconfig_repository_format_version, ibuf);
512 if (extensions && nextensions) {
513 err = got_privsep_send_gitconfig_repository_extensions_req(
517 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
520 if (*nextensions > 0) {
522 *extensions = calloc(*nextensions, sizeof(char *));
523 if (*extensions == NULL) {
524 err = got_error_from_errno("calloc");
527 for (i = 0; i < *nextensions; i++) {
529 err = got_privsep_recv_gitconfig_str(&ext,
533 (*extensions)[i] = ext;
538 err = got_privsep_send_gitconfig_author_name_req(ibuf);
542 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
546 err = got_privsep_send_gitconfig_author_email_req(ibuf);
550 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
554 if (remotes && nremotes) {
555 err = got_privsep_send_gitconfig_remotes_req(ibuf);
559 err = got_privsep_recv_gitconfig_remotes(remotes,
565 if (gitconfig_owner) {
566 err = got_privsep_send_gitconfig_owner_req(ibuf);
569 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
574 err = got_privsep_send_stop(imsg_fds[0]);
575 child_err = got_privsep_wait_for_child(pid);
576 if (child_err && err == NULL)
579 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
580 err = got_error_from_errno("close");
581 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (fd != -1 && close(fd) == -1 && err == NULL)
584 err = got_error_from_errno2("close", gitconfig_path);
589 static const struct got_error *
590 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
592 const struct got_error *err = NULL;
593 char *repo_gitconfig_path = NULL;
595 if (global_gitconfig_path) {
596 /* Read settings from ~/.gitconfig. */
597 int dummy_repo_version;
598 err = parse_gitconfig_file(&dummy_repo_version,
599 &repo->global_gitconfig_author_name,
600 &repo->global_gitconfig_author_email,
601 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
606 /* Read repository's .git/config file. */
607 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
608 if (repo_gitconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gitconfig");
611 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
612 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
613 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
614 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
615 repo_gitconfig_path);
619 free(repo_gitconfig_path);
623 static const struct got_error *
624 read_gotconfig(struct got_repository *repo)
626 const struct got_error *err = NULL;
627 char *gotconfig_path;
629 gotconfig_path = got_repo_get_path_gotconfig(repo);
630 if (gotconfig_path == NULL)
631 return got_error_from_errno("got_repo_get_path_gotconfig");
633 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
634 free(gotconfig_path);
638 /* Supported repository format extensions. */
639 static const char *repo_extensions[] = {
640 "noop", /* Got supports repository format version 1. */
641 "preciousObjects", /* Supported by gotadmin cleanup. */
642 "worktreeConfig", /* Got does not care about Git work trees. */
645 const struct got_error *
646 got_repo_open(struct got_repository **repop, const char *path,
647 const char *global_gitconfig_path)
649 struct got_repository *repo = NULL;
650 const struct got_error *err = NULL;
651 char *repo_path = NULL;
657 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
658 return got_error_from_errno("getrlimit");
660 repo = calloc(1, sizeof(*repo));
662 err = got_error_from_errno("calloc");
666 RB_INIT(&repo->packidx_bloom_filters);
668 for (i = 0; i < nitems(repo->privsep_children); i++) {
669 memset(&repo->privsep_children[i], 0,
670 sizeof(repo->privsep_children[0]));
671 repo->privsep_children[i].imsg_fd = -1;
674 err = got_object_cache_init(&repo->objcache,
675 GOT_OBJECT_CACHE_TYPE_OBJ);
678 err = got_object_cache_init(&repo->treecache,
679 GOT_OBJECT_CACHE_TYPE_TREE);
682 err = got_object_cache_init(&repo->commitcache,
683 GOT_OBJECT_CACHE_TYPE_COMMIT);
686 err = got_object_cache_init(&repo->tagcache,
687 GOT_OBJECT_CACHE_TYPE_TAG);
690 err = got_object_cache_init(&repo->rawcache,
691 GOT_OBJECT_CACHE_TYPE_RAW);
695 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
696 if (repo->pack_cache_size > rl.rlim_cur / 8)
697 repo->pack_cache_size = rl.rlim_cur / 8;
699 repo_path = realpath(path, NULL);
700 if (repo_path == NULL) {
701 err = got_error_from_errno2("realpath", path);
708 err = open_repo(repo, repo_path);
711 if (err->code != GOT_ERR_NOT_GIT_REPO)
713 if (repo_path[0] == '/' && repo_path[1] == '\0') {
714 err = got_error(GOT_ERR_NOT_GIT_REPO);
717 err = got_path_dirname(&parent_path, repo_path);
721 repo_path = parent_path;
724 err = read_gotconfig(repo);
728 err = read_gitconfig(repo, global_gitconfig_path);
731 if (repo->gitconfig_repository_format_version != 0)
732 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
733 for (i = 0; i < repo->nextensions; i++) {
734 char *ext = repo->extensions[i];
735 int j, supported = 0;
736 for (j = 0; j < nitems(repo_extensions); j++) {
737 if (strcmp(ext, repo_extensions[j]) == 0) {
743 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
749 got_repo_close(repo);
756 const struct got_error *
757 got_repo_close(struct got_repository *repo)
759 const struct got_error *err = NULL, *child_err;
760 struct got_packidx_bloom_filter *bf;
763 for (i = 0; i < repo->pack_cache_size; i++) {
764 if (repo->packidx_cache[i] == NULL)
766 got_packidx_close(repo->packidx_cache[i]);
769 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
770 &repo->packidx_bloom_filters))) {
771 RB_REMOVE(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters, bf);
777 for (i = 0; i < repo->pack_cache_size; i++) {
778 if (repo->packs[i].path_packfile == NULL)
780 got_pack_close(&repo->packs[i]);
784 free(repo->path_git_dir);
786 got_object_cache_close(&repo->objcache);
787 got_object_cache_close(&repo->treecache);
788 got_object_cache_close(&repo->commitcache);
789 got_object_cache_close(&repo->tagcache);
790 got_object_cache_close(&repo->rawcache);
792 for (i = 0; i < nitems(repo->privsep_children); i++) {
793 if (repo->privsep_children[i].imsg_fd == -1)
795 imsg_clear(repo->privsep_children[i].ibuf);
796 free(repo->privsep_children[i].ibuf);
797 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
798 child_err = got_privsep_wait_for_child(
799 repo->privsep_children[i].pid);
800 if (child_err && err == NULL)
802 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
804 err = got_error_from_errno("close");
807 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
809 err = got_error_from_errno("close");
812 got_gotconfig_free(repo->gotconfig);
813 free(repo->gitconfig_author_name);
814 free(repo->gitconfig_author_email);
815 for (i = 0; i < repo->ngitconfig_remotes; i++)
816 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
817 free(repo->gitconfig_remotes);
818 for (i = 0; i < repo->nextensions; i++)
819 free(repo->extensions[i]);
820 free(repo->extensions);
827 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
833 free(repo->fetch_url);
834 repo->fetch_url = NULL;
835 free(repo->send_url);
836 repo->send_url = NULL;
837 for (i = 0; i < repo->nfetch_branches; i++)
838 free(repo->fetch_branches[i]);
839 free(repo->fetch_branches);
840 repo->fetch_branches = NULL;
841 repo->nfetch_branches = 0;
842 for (i = 0; i < repo->nsend_branches; i++)
843 free(repo->send_branches[i]);
844 free(repo->send_branches);
845 repo->send_branches = NULL;
846 repo->nsend_branches = 0;
849 const struct got_error *
850 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
851 const char *input_path)
853 const struct got_error *err = NULL;
854 const char *repo_abspath = NULL;
856 char *canonpath, *path = NULL;
858 *in_repo_path = NULL;
860 canonpath = strdup(input_path);
861 if (canonpath == NULL) {
862 err = got_error_from_errno("strdup");
865 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
869 repo_abspath = got_repo_get_path(repo);
871 if (canonpath[0] == '\0') {
872 path = strdup(canonpath);
874 err = got_error_from_errno("strdup");
878 path = realpath(canonpath, NULL);
880 if (errno != ENOENT) {
881 err = got_error_from_errno2("realpath",
886 * Path is not on disk.
887 * Assume it is already relative to repository root.
889 path = strdup(canonpath);
891 err = got_error_from_errno("strdup");
896 repolen = strlen(repo_abspath);
900 if (strcmp(path, repo_abspath) == 0) {
904 err = got_error_from_errno("strdup");
907 } else if (len > repolen &&
908 got_path_is_child(path, repo_abspath, repolen)) {
909 /* Matched an on-disk path inside repository. */
910 if (got_repo_is_bare(repo)) {
912 * Matched an on-disk path inside repository
913 * database. Treat input as repository-relative.
920 /* Strip common prefix with repository path. */
921 err = got_path_skip_common_ancestor(&child,
930 * Matched unrelated on-disk path.
931 * Treat input as repository-relative.
939 /* Make in-repository path absolute */
940 if (path[0] != '/') {
942 if (asprintf(&abspath, "/%s", path) == -1) {
943 err = got_error_from_errno("asprintf");
955 *in_repo_path = path;
959 static const struct got_error *
960 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
961 const char *path_packidx)
963 const struct got_error *err = NULL;
966 for (i = 0; i < repo->pack_cache_size; i++) {
967 if (repo->packidx_cache[i] == NULL)
969 if (strcmp(repo->packidx_cache[i]->path_packidx,
970 path_packidx) == 0) {
971 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
974 if (i == repo->pack_cache_size) {
975 i = repo->pack_cache_size - 1;
976 err = got_packidx_close(repo->packidx_cache[i]);
981 repo->packidx_cache[i] = packidx;
987 got_repo_is_packidx_filename(const char *name, size_t len)
989 if (len != GOT_PACKIDX_NAMELEN)
992 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
995 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
996 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1002 static struct got_packidx_bloom_filter *
1003 get_packidx_bloom_filter(struct got_repository *repo,
1004 const char *path, size_t path_len)
1006 struct got_packidx_bloom_filter key;
1008 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1009 return NULL; /* XXX */
1010 key.path_len = path_len;
1012 return RB_FIND(got_packidx_bloom_filter_tree,
1013 &repo->packidx_bloom_filters, &key);
1017 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1018 const char *path_packidx, struct got_object_id *id)
1020 struct got_packidx_bloom_filter *bf;
1022 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1024 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1026 /* No bloom filter means this pack index must be searched. */
1030 static const struct got_error *
1031 add_packidx_bloom_filter(struct got_repository *repo,
1032 struct got_packidx *packidx, const char *path_packidx)
1034 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1035 struct got_packidx_bloom_filter *bf;
1039 * Don't use bloom filters for very large pack index files.
1040 * Large pack files will contain a relatively large fraction
1041 * of our objects so we will likely need to visit them anyway.
1042 * The more objects a pack file contains the higher the probability
1043 * of a false-positive match from the bloom filter. And reading
1044 * all object IDs from a large pack index file can be expensive.
1046 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1049 /* Do we already have a filter for this pack index? */
1050 if (get_packidx_bloom_filter(repo, path_packidx,
1051 strlen(path_packidx)) != NULL)
1054 bf = calloc(1, sizeof(*bf));
1056 return got_error_from_errno("calloc");
1057 bf->bloom = calloc(1, sizeof(*bf->bloom));
1058 if (bf->bloom == NULL) {
1060 return got_error_from_errno("calloc");
1063 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1064 if (len >= sizeof(bf->path)) {
1067 return got_error(GOT_ERR_NO_SPACE);
1071 /* Minimum size supported by our bloom filter is 1000 entries. */
1072 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1073 for (i = 0; i < nobjects; i++) {
1074 struct got_packidx_object_id *id;
1075 id = &packidx->hdr.sorted_ids[i];
1076 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1079 RB_INSERT(got_packidx_bloom_filter_tree,
1080 &repo->packidx_bloom_filters, bf);
1084 const struct got_error *
1085 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1086 struct got_repository *repo, struct got_object_id *id)
1088 const struct got_error *err;
1089 DIR *packdir = NULL;
1090 struct dirent *dent;
1095 /* Search pack index cache. */
1096 for (i = 0; i < repo->pack_cache_size; i++) {
1097 if (repo->packidx_cache[i] == NULL)
1099 if (!got_repo_check_packidx_bloom_filter(repo,
1100 repo->packidx_cache[i]->path_packidx, id))
1101 continue; /* object will not be found in this index */
1102 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1104 *packidx = repo->packidx_cache[i];
1106 * Move this cache entry to the front. Repeatedly
1107 * searching a wrong pack index can be expensive.
1110 memmove(&repo->packidx_cache[1],
1111 &repo->packidx_cache[0],
1112 i * sizeof(repo->packidx_cache[0]));
1113 repo->packidx_cache[0] = *packidx;
1118 /* No luck. Search the filesystem. */
1120 packdir_fd = openat(got_repo_get_fd(repo),
1121 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1122 if (packdir_fd == -1) {
1123 if (errno == ENOENT)
1124 err = got_error_no_obj(id);
1126 err = got_error_from_errno_fmt("openat: %s/%s",
1127 got_repo_get_path_git_dir(repo),
1128 GOT_OBJECTS_PACK_DIR);
1132 packdir = fdopendir(packdir_fd);
1133 if (packdir == NULL) {
1134 err = got_error_from_errno("fdopendir");
1138 while ((dent = readdir(packdir)) != NULL) {
1141 if (!got_repo_is_packidx_filename(dent->d_name,
1142 strlen(dent->d_name)))
1145 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1146 dent->d_name) == -1) {
1147 err = got_error_from_errno("asprintf");
1151 if (!got_repo_check_packidx_bloom_filter(repo,
1152 path_packidx, id)) {
1154 continue; /* object will not be found in this index */
1157 for (i = 0; i < repo->pack_cache_size; i++) {
1158 if (repo->packidx_cache[i] == NULL)
1160 if (strcmp(repo->packidx_cache[i]->path_packidx,
1161 path_packidx) == 0) {
1168 continue; /* already searched */
1171 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1178 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1184 err = cache_packidx(repo, *packidx, path_packidx);
1189 *idx = got_packidx_get_object_idx(*packidx, id);
1191 err = NULL; /* found the object */
1196 err = got_error_no_obj(id);
1198 if (packdir && closedir(packdir) != 0 && err == NULL)
1199 err = got_error_from_errno("closedir");
1203 const struct got_error *
1204 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1205 struct got_repository *repo)
1207 const struct got_error *err = NULL;
1208 DIR *packdir = NULL;
1209 struct dirent *dent;
1210 char *path_packidx = NULL;
1213 packdir_fd = openat(got_repo_get_fd(repo),
1214 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1215 if (packdir_fd == -1) {
1216 return got_error_from_errno_fmt("openat: %s/%s",
1217 got_repo_get_path_git_dir(repo),
1218 GOT_OBJECTS_PACK_DIR);
1221 packdir = fdopendir(packdir_fd);
1222 if (packdir == NULL) {
1223 err = got_error_from_errno("fdopendir");
1227 while ((dent = readdir(packdir)) != NULL) {
1228 if (!got_repo_is_packidx_filename(dent->d_name,
1229 strlen(dent->d_name)))
1232 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1233 dent->d_name) == -1) {
1234 err = got_error_from_errno("asprintf");
1235 path_packidx = NULL;
1239 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1246 if (packdir && closedir(packdir) != 0 && err == NULL)
1247 err = got_error_from_errno("closedir");
1251 const struct got_error *
1252 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1253 struct got_repository *repo)
1255 const struct got_error *err;
1260 /* Search pack index cache. */
1261 for (i = 0; i < repo->pack_cache_size; i++) {
1262 if (repo->packidx_cache[i] == NULL)
1264 if (strcmp(repo->packidx_cache[i]->path_packidx,
1265 path_packidx) == 0) {
1266 *packidx = repo->packidx_cache[i];
1270 /* No luck. Search the filesystem. */
1272 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1277 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1281 err = cache_packidx(repo, *packidx, path_packidx);
1284 got_packidx_close(*packidx);
1290 static const struct got_error *
1291 read_packfile_hdr(int fd, struct got_packidx *packidx)
1293 const struct got_error *err = NULL;
1294 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1295 struct got_packfile_hdr hdr;
1298 n = read(fd, &hdr, sizeof(hdr));
1300 return got_error_from_errno("read");
1301 if (n != sizeof(hdr))
1302 return got_error(GOT_ERR_BAD_PACKFILE);
1304 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1305 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1306 be32toh(hdr.nobjects) != totobj)
1307 err = got_error(GOT_ERR_BAD_PACKFILE);
1312 static const struct got_error *
1313 open_packfile(int *fd, struct got_repository *repo,
1314 const char *relpath, struct got_packidx *packidx)
1316 const struct got_error *err = NULL;
1318 *fd = openat(got_repo_get_fd(repo), relpath,
1319 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1321 return got_error_from_errno_fmt("openat: %s/%s",
1322 got_repo_get_path_git_dir(repo), relpath);
1325 err = read_packfile_hdr(*fd, packidx);
1335 const struct got_error *
1336 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1337 const char *path_packfile, struct got_packidx *packidx)
1339 const struct got_error *err = NULL;
1340 struct got_pack *pack = NULL;
1347 for (i = 0; i < repo->pack_cache_size; i++) {
1348 pack = &repo->packs[i];
1349 if (pack->path_packfile == NULL)
1351 if (strcmp(pack->path_packfile, path_packfile) == 0)
1352 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1355 if (i == repo->pack_cache_size) {
1356 err = got_pack_close(&repo->packs[i - 1]);
1359 memmove(&repo->packs[1], &repo->packs[0],
1360 sizeof(repo->packs) - sizeof(repo->packs[0]));
1364 pack = &repo->packs[i];
1366 pack->path_packfile = strdup(path_packfile);
1367 if (pack->path_packfile == NULL) {
1368 err = got_error_from_errno("strdup");
1372 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1376 if (fstat(pack->fd, &sb) != 0) {
1377 err = got_error_from_errno("fstat");
1380 pack->filesize = sb.st_size;
1382 pack->privsep_child = NULL;
1384 #ifndef GOT_PACK_NO_MMAP
1385 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1387 if (pack->map == MAP_FAILED) {
1388 if (errno != ENOMEM) {
1389 err = got_error_from_errno("mmap");
1392 pack->map = NULL; /* fall back to read(2) */
1398 free(pack->path_packfile);
1399 memset(pack, 0, sizeof(*pack));
1407 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1409 struct got_pack *pack = NULL;
1412 for (i = 0; i < repo->pack_cache_size; i++) {
1413 pack = &repo->packs[i];
1414 if (pack->path_packfile == NULL)
1416 if (strcmp(pack->path_packfile, path_packfile) == 0)
1423 const struct got_error *
1424 got_repo_init(const char *repo_path)
1426 const struct got_error *err = NULL;
1427 const char *dirnames[] = {
1429 GOT_OBJECTS_PACK_DIR,
1432 const char *description_str = "Unnamed repository; "
1433 "edit this file 'description' to name the repository.";
1434 const char *headref_str = "ref: refs/heads/main";
1435 const char *gitconfig_str = "[core]\n"
1436 "\trepositoryformatversion = 0\n"
1437 "\tfilemode = true\n"
1442 if (!got_path_dir_is_empty(repo_path))
1443 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1445 for (i = 0; i < nitems(dirnames); i++) {
1446 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1447 return got_error_from_errno("asprintf");
1449 err = got_path_mkdir(path);
1455 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1456 return got_error_from_errno("asprintf");
1457 err = got_path_create_file(path, description_str);
1462 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1463 return got_error_from_errno("asprintf");
1464 err = got_path_create_file(path, headref_str);
1469 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1470 return got_error_from_errno("asprintf");
1471 err = got_path_create_file(path, gitconfig_str);
1479 static const struct got_error *
1480 match_packed_object(struct got_object_id **unique_id,
1481 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1483 const struct got_error *err = NULL;
1484 DIR *packdir = NULL;
1485 struct dirent *dent;
1487 struct got_object_id_queue matched_ids;
1490 STAILQ_INIT(&matched_ids);
1492 packdir_fd = openat(got_repo_get_fd(repo),
1493 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1494 if (packdir_fd == -1) {
1495 if (errno != ENOENT) {
1496 err = got_error_from_errno2("openat",
1497 GOT_OBJECTS_PACK_DIR);
1502 packdir = fdopendir(packdir_fd);
1503 if (packdir == NULL) {
1504 err = got_error_from_errno("fdopendir");
1508 while ((dent = readdir(packdir)) != NULL) {
1509 struct got_packidx *packidx;
1510 struct got_object_qid *qid;
1512 if (!got_repo_is_packidx_filename(dent->d_name,
1513 strlen(dent->d_name)))
1516 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1517 dent->d_name) == -1) {
1518 err = got_error_from_errno("strdup");
1522 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1528 err = got_packidx_match_id_str_prefix(&matched_ids,
1529 packidx, id_str_prefix);
1531 got_packidx_close(packidx);
1534 err = got_packidx_close(packidx);
1538 STAILQ_FOREACH(qid, &matched_ids, entry) {
1539 if (obj_type != GOT_OBJ_TYPE_ANY) {
1541 err = got_object_get_type(&matched_type, repo,
1545 if (matched_type != obj_type)
1548 if (*unique_id == NULL) {
1549 *unique_id = got_object_id_dup(qid->id);
1550 if (*unique_id == NULL) {
1551 err = got_error_from_errno("malloc");
1555 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1556 continue; /* packed multiple times */
1557 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1563 got_object_id_queue_free(&matched_ids);
1564 if (packdir && closedir(packdir) != 0 && err == NULL)
1565 err = got_error_from_errno("closedir");
1573 static const struct got_error *
1574 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1575 const char *object_dir, const char *id_str_prefix, int obj_type,
1576 struct got_repository *repo)
1578 const struct got_error *err = NULL;
1581 struct dirent *dent;
1582 struct got_object_id id;
1584 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1585 err = got_error_from_errno("asprintf");
1589 dir = opendir(path);
1591 if (errno == ENOENT) {
1595 err = got_error_from_errno2("opendir", path);
1598 while ((dent = readdir(dir)) != NULL) {
1602 if (strcmp(dent->d_name, ".") == 0 ||
1603 strcmp(dent->d_name, "..") == 0)
1606 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1607 err = got_error_from_errno("asprintf");
1611 if (!got_parse_sha1_digest(id.sha1, id_str))
1615 * Directory entries do not necessarily appear in
1616 * sorted order, so we must iterate over all of them.
1618 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1624 if (*unique_id == NULL) {
1625 if (obj_type != GOT_OBJ_TYPE_ANY) {
1627 err = got_object_get_type(&matched_type, repo,
1631 if (matched_type != obj_type)
1634 *unique_id = got_object_id_dup(&id);
1635 if (*unique_id == NULL) {
1636 err = got_error_from_errno("got_object_id_dup");
1641 if (got_object_id_cmp(*unique_id, &id) == 0)
1642 continue; /* both packed and loose */
1643 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1649 if (dir && closedir(dir) != 0 && err == NULL)
1650 err = got_error_from_errno("closedir");
1659 const struct got_error *
1660 got_repo_match_object_id_prefix(struct got_object_id **id,
1661 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1663 const struct got_error *err = NULL;
1664 char *path_objects = got_repo_get_path_objects(repo);
1665 char *object_dir = NULL;
1671 for (i = 0; i < strlen(id_str_prefix); i++) {
1672 if (isxdigit((unsigned char)id_str_prefix[i]))
1674 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1677 len = strlen(id_str_prefix);
1679 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1682 object_dir = strndup(id_str_prefix, 2);
1683 if (object_dir == NULL) {
1684 err = got_error_from_errno("strdup");
1687 err = match_loose_object(id, path_objects, object_dir,
1688 id_str_prefix, obj_type, repo);
1689 } else if (len == 1) {
1691 for (i = 0; i < 0xf; i++) {
1692 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1694 err = got_error_from_errno("asprintf");
1697 err = match_packed_object(id, repo, object_dir,
1701 err = match_loose_object(id, path_objects, object_dir,
1702 id_str_prefix, obj_type, repo);
1707 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1715 } else if (*id == NULL) {
1717 case GOT_OBJ_TYPE_BLOB:
1718 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1719 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1721 case GOT_OBJ_TYPE_TREE:
1722 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1723 GOT_OBJ_LABEL_TREE, id_str_prefix);
1725 case GOT_OBJ_TYPE_COMMIT:
1726 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1727 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1729 case GOT_OBJ_TYPE_TAG:
1730 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1731 GOT_OBJ_LABEL_TAG, id_str_prefix);
1734 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1742 const struct got_error *
1743 got_repo_match_object_id(struct got_object_id **id, char **label,
1744 const char *id_str, int obj_type, struct got_reflist_head *refs,
1745 struct got_repository *repo)
1747 const struct got_error *err;
1748 struct got_tag_object *tag;
1749 struct got_reference *ref = NULL;
1756 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1759 *id = got_object_id_dup(
1760 got_object_tag_get_object_id(tag));
1762 err = got_error_from_errno("got_object_id_dup");
1763 else if (label && asprintf(label, "refs/tags/%s",
1764 got_object_tag_get_name(tag)) == -1) {
1765 err = got_error_from_errno("asprintf");
1769 got_object_tag_close(tag);
1771 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1772 err->code != GOT_ERR_NO_OBJ)
1776 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1778 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1780 err = got_ref_open(&ref, repo, id_str, 0);
1784 *label = strdup(got_ref_get_name(ref));
1785 if (*label == NULL) {
1786 err = got_error_from_errno("strdup");
1790 err = got_ref_resolve(id, repo, ref);
1792 err = got_object_id_str(label, *id);
1793 if (*label == NULL) {
1794 err = got_error_from_errno("strdup");
1804 const struct got_error *
1805 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1806 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1808 const struct got_error *err = NULL;
1809 struct got_reflist_entry *re;
1810 struct got_object_id *tag_id;
1811 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1815 TAILQ_FOREACH(re, refs, entry) {
1816 const char *refname;
1817 refname = got_ref_get_name(re->ref);
1818 if (got_ref_is_symbolic(re->ref))
1820 if (strncmp(refname, "refs/tags/", 10) != 0)
1822 if (!name_is_absolute)
1823 refname += strlen("refs/tags/");
1824 if (strcmp(refname, name) != 0)
1826 err = got_ref_resolve(&tag_id, repo, re->ref);
1829 err = got_object_open_as_tag(tag, repo, tag_id);
1833 if (obj_type == GOT_OBJ_TYPE_ANY ||
1834 got_object_tag_get_object_type(*tag) == obj_type)
1836 got_object_tag_close(*tag);
1840 if (err == NULL && *tag == NULL)
1841 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1842 GOT_OBJ_LABEL_TAG, name);
1846 static const struct got_error *
1847 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1848 const char *name, mode_t mode, struct got_object_id *blob_id)
1850 const struct got_error *err = NULL;
1854 *new_te = calloc(1, sizeof(**new_te));
1855 if (*new_te == NULL)
1856 return got_error_from_errno("calloc");
1858 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1859 sizeof((*new_te)->name)) {
1860 err = got_error(GOT_ERR_NO_SPACE);
1864 if (S_ISLNK(mode)) {
1865 (*new_te)->mode = S_IFLNK;
1867 (*new_te)->mode = S_IFREG;
1868 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1870 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1872 if (err && *new_te) {
1879 static const struct got_error *
1880 import_file(struct got_tree_entry **new_te, struct dirent *de,
1881 const char *path, struct got_repository *repo)
1883 const struct got_error *err;
1884 struct got_object_id *blob_id = NULL;
1888 if (asprintf(&filepath, "%s%s%s", path,
1889 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1890 return got_error_from_errno("asprintf");
1892 if (lstat(filepath, &sb) != 0) {
1893 err = got_error_from_errno2("lstat", path);
1897 err = got_object_blob_create(&blob_id, filepath, repo);
1901 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1910 static const struct got_error *
1911 insert_tree_entry(struct got_tree_entry *new_te,
1912 struct got_pathlist_head *paths)
1914 const struct got_error *err = NULL;
1915 struct got_pathlist_entry *new_pe;
1917 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1921 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1925 static const struct got_error *write_tree(struct got_object_id **,
1926 const char *, struct got_pathlist_head *, struct got_repository *,
1927 got_repo_import_cb progress_cb, void *progress_arg);
1929 static const struct got_error *
1930 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1931 const char *path, struct got_pathlist_head *ignores,
1932 struct got_repository *repo,
1933 got_repo_import_cb progress_cb, void *progress_arg)
1935 const struct got_error *err;
1936 struct got_object_id *id = NULL;
1939 if (asprintf(&subdirpath, "%s%s%s", path,
1940 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1941 return got_error_from_errno("asprintf");
1943 (*new_te) = calloc(1, sizeof(**new_te));
1944 if (*new_te == NULL)
1945 return got_error_from_errno("calloc");
1946 (*new_te)->mode = S_IFDIR;
1947 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1948 sizeof((*new_te)->name)) {
1949 err = got_error(GOT_ERR_NO_SPACE);
1952 err = write_tree(&id, subdirpath, ignores, repo,
1953 progress_cb, progress_arg);
1956 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1968 static const struct got_error *
1969 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1970 struct got_pathlist_head *ignores, struct got_repository *repo,
1971 got_repo_import_cb progress_cb, void *progress_arg)
1973 const struct got_error *err = NULL;
1977 struct got_tree_entry *new_te = NULL;
1978 struct got_pathlist_head paths;
1979 struct got_pathlist_entry *pe;
1981 *new_tree_id = NULL;
1985 dir = opendir(path_dir);
1987 err = got_error_from_errno2("opendir", path_dir);
1992 while ((de = readdir(dir)) != NULL) {
1996 if (strcmp(de->d_name, ".") == 0 ||
1997 strcmp(de->d_name, "..") == 0)
2000 TAILQ_FOREACH(pe, ignores, entry) {
2001 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2009 err = got_path_dirent_type(&type, path_dir, de);
2013 if (type == DT_DIR) {
2014 err = import_subdir(&new_te, de, path_dir,
2015 ignores, repo, progress_cb, progress_arg);
2017 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2022 } else if (type == DT_REG || type == DT_LNK) {
2023 err = import_file(&new_te, de, path_dir, repo);
2029 err = insert_tree_entry(new_te, &paths);
2035 if (TAILQ_EMPTY(&paths)) {
2036 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2037 "cannot create tree without any entries");
2041 TAILQ_FOREACH(pe, &paths, entry) {
2042 struct got_tree_entry *te = pe->data;
2044 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2046 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2047 err = got_error_from_errno("asprintf");
2050 err = (*progress_cb)(progress_arg, path);
2056 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2060 got_pathlist_free(&paths);
2064 const struct got_error *
2065 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2066 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2067 struct got_repository *repo, got_repo_import_cb progress_cb,
2070 const struct got_error *err;
2071 struct got_object_id *new_tree_id;
2073 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2074 progress_cb, progress_arg);
2078 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2079 author, time(NULL), author, time(NULL), logmsg, repo);
2084 const struct got_error *
2085 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2086 struct got_repository *repo)
2088 const struct got_error *err = NULL;
2089 char *path_objects = NULL, *path = NULL;
2091 struct got_object_id id;
2097 path_objects = got_repo_get_path_objects(repo);
2098 if (path_objects == NULL)
2099 return got_error_from_errno("got_repo_get_path_objects");
2101 for (i = 0; i <= 0xff; i++) {
2102 struct dirent *dent;
2104 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2105 err = got_error_from_errno("asprintf");
2109 dir = opendir(path);
2111 if (errno == ENOENT) {
2115 err = got_error_from_errno2("opendir", path);
2119 while ((dent = readdir(dir)) != NULL) {
2124 if (strcmp(dent->d_name, ".") == 0 ||
2125 strcmp(dent->d_name, "..") == 0)
2128 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2129 err = got_error_from_errno("asprintf");
2133 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2139 err = got_object_open_loose_fd(&fd, &id, repo);
2143 if (fstat(fd, &sb) == -1) {
2144 err = got_error_from_errno("fstat");
2149 (*ondisk_size) += sb.st_size;
2151 if (close(fd) == -1) {
2152 err = got_error_from_errno("close");
2157 if (closedir(dir) != 0) {
2158 err = got_error_from_errno("closedir");
2167 if (dir && closedir(dir) != 0 && err == NULL)
2168 err = got_error_from_errno("closedir");
2179 const struct got_error *
2180 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2181 off_t *total_packsize, struct got_repository *repo)
2183 const struct got_error *err = NULL;
2184 DIR *packdir = NULL;
2185 struct dirent *dent;
2186 struct got_packidx *packidx = NULL;
2188 char *path_packfile;
2194 *total_packsize = 0;
2196 packdir_fd = openat(got_repo_get_fd(repo),
2197 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2198 if (packdir_fd == -1) {
2199 return got_error_from_errno_fmt("openat: %s/%s",
2200 got_repo_get_path_git_dir(repo),
2201 GOT_OBJECTS_PACK_DIR);
2204 packdir = fdopendir(packdir_fd);
2205 if (packdir == NULL) {
2206 err = got_error_from_errno("fdopendir");
2210 while ((dent = readdir(packdir)) != NULL) {
2211 if (!got_repo_is_packidx_filename(dent->d_name,
2212 strlen(dent->d_name)))
2215 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2216 dent->d_name) == -1) {
2217 err = got_error_from_errno("asprintf");
2221 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2227 if (fstat(packidx->fd, &sb) == -1)
2229 *total_packsize += sb.st_size;
2231 err = got_packidx_get_packfile_path(&path_packfile,
2232 packidx->path_packidx);
2236 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2238 free(path_packfile);
2241 free(path_packfile);
2242 *total_packsize += sb.st_size;
2244 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2248 got_packidx_close(packidx);
2253 got_packidx_close(packidx);
2254 if (packdir && closedir(packdir) != 0 && err == NULL)
2255 err = got_error_from_errno("closedir");
2259 *total_packsize = 0;
2264 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2265 got_packidx_bloom_filter_cmp);