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>
18 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <sys/resource.h>
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
77 got_repo_get_path(struct got_repository *repo)
83 got_repo_get_path_git_dir(struct got_repository *repo)
85 return repo->path_git_dir;
89 got_repo_get_fd(struct got_repository *repo)
91 return repo->gitdir_fd;
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
97 return repo->gitconfig_author_name;
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
139 get_path_git_child(struct got_repository *repo, const char *basename)
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_objects, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_refs, &sb) == -1)
229 if (!S_ISDIR(sb.st_mode))
232 if (lstat(path_head, &sb) == -1)
234 if (!S_ISREG(sb.st_mode))
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
240 got_ref_close(head_ref);
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
261 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
262 pack_fds_tmp[i] = got_opentempfd();
263 if (pack_fds_tmp[i] == -1) {
264 err = got_repo_pack_fds_close(pack_fds_tmp);
268 return got_error_from_errno("got_opentempfd");
271 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
275 const struct got_error *
276 got_repo_pack_fds_close(int *pack_fds)
278 const struct got_error *err = NULL;
281 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
282 if (pack_fds[i] == -1)
284 if (close(pack_fds[i]) == -1) {
285 err = got_error_from_errno("close");
294 const struct got_error *
295 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
296 struct got_object *obj)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->objcache, id, obj);
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
313 got_repo_get_cached_object(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
319 const struct got_error *
320 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
321 struct got_tree_object *tree)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->treecache, id, tree);
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
337 struct got_tree_object *
338 got_repo_get_cached_tree(struct got_repository *repo,
339 struct got_object_id *id)
341 return (struct got_tree_object *)got_object_cache_get(
342 &repo->treecache, id);
345 const struct got_error *
346 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
347 struct got_commit_object *commit)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->commitcache, id, commit);
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
363 struct got_commit_object *
364 got_repo_get_cached_commit(struct got_repository *repo,
365 struct got_object_id *id)
367 return (struct got_commit_object *)got_object_cache_get(
368 &repo->commitcache, id);
371 const struct got_error *
372 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
373 struct got_tag_object *tag)
375 #ifndef GOT_NO_OBJ_CACHE
376 const struct got_error *err = NULL;
377 err = got_object_cache_add(&repo->tagcache, id, tag);
379 if (err->code == GOT_ERR_OBJ_EXISTS ||
380 err->code == GOT_ERR_OBJ_TOO_LARGE)
389 struct got_tag_object *
390 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
392 return (struct got_tag_object *)got_object_cache_get(
393 &repo->tagcache, id);
396 const struct got_error *
397 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
398 struct got_raw_object *raw)
400 #ifndef GOT_NO_OBJ_CACHE
401 const struct got_error *err = NULL;
402 err = got_object_cache_add(&repo->rawcache, id, raw);
404 if (err->code == GOT_ERR_OBJ_EXISTS ||
405 err->code == GOT_ERR_OBJ_TOO_LARGE)
415 struct got_raw_object *
416 got_repo_get_cached_raw_object(struct got_repository *repo,
417 struct got_object_id *id)
419 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
423 static const struct got_error *
424 open_repo(struct got_repository *repo, const char *path)
426 const struct got_error *err = NULL;
428 repo->gitdir_fd = -1;
430 /* bare git repository? */
431 repo->path_git_dir = strdup(path);
432 if (repo->path_git_dir == NULL)
433 return got_error_from_errno("strdup");
434 if (is_git_repo(repo)) {
435 repo->path = strdup(repo->path_git_dir);
436 if (repo->path == NULL) {
437 err = got_error_from_errno("strdup");
440 repo->gitdir_fd = open(repo->path_git_dir,
441 O_DIRECTORY | O_CLOEXEC);
442 if (repo->gitdir_fd == -1) {
443 err = got_error_from_errno2("open",
450 /* git repository with working tree? */
451 free(repo->path_git_dir);
452 repo->path_git_dir = NULL;
453 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
454 err = got_error_from_errno("asprintf");
457 if (is_git_repo(repo)) {
458 repo->path = strdup(path);
459 if (repo->path == NULL) {
460 err = got_error_from_errno("strdup");
463 repo->gitdir_fd = open(repo->path_git_dir,
464 O_DIRECTORY | O_CLOEXEC);
465 if (repo->gitdir_fd == -1) {
466 err = got_error_from_errno2("open",
473 err = got_error(GOT_ERR_NOT_GIT_REPO);
478 free(repo->path_git_dir);
479 repo->path_git_dir = NULL;
480 if (repo->gitdir_fd != -1)
481 close(repo->gitdir_fd);
482 repo->gitdir_fd = -1;
488 static const struct got_error *
489 parse_gitconfig_file(int *gitconfig_repository_format_version,
490 char **gitconfig_author_name, char **gitconfig_author_email,
491 struct got_remote_repo **remotes, int *nremotes,
492 char **gitconfig_owner, char ***extensions, int *nextensions,
493 const char *gitconfig_path)
495 const struct got_error *err = NULL, *child_err = NULL;
497 int imsg_fds[2] = { -1, -1 };
499 struct imsgbuf *ibuf;
501 *gitconfig_repository_format_version = 0;
506 *gitconfig_author_name = NULL;
507 *gitconfig_author_email = NULL;
513 *gitconfig_owner = NULL;
515 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
519 return got_error_from_errno2("open", gitconfig_path);
522 ibuf = calloc(1, sizeof(*ibuf));
524 err = got_error_from_errno("calloc");
528 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
529 err = got_error_from_errno("socketpair");
535 err = got_error_from_errno("fork");
537 } else if (pid == 0) {
538 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
543 if (close(imsg_fds[1]) == -1) {
544 err = got_error_from_errno("close");
548 imsg_init(ibuf, imsg_fds[0]);
550 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
555 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
559 err = got_privsep_recv_gitconfig_int(
560 gitconfig_repository_format_version, ibuf);
564 if (extensions && nextensions) {
565 err = got_privsep_send_gitconfig_repository_extensions_req(
569 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
572 if (*nextensions > 0) {
574 *extensions = calloc(*nextensions, sizeof(char *));
575 if (*extensions == NULL) {
576 err = got_error_from_errno("calloc");
579 for (i = 0; i < *nextensions; i++) {
581 err = got_privsep_recv_gitconfig_str(&ext,
585 (*extensions)[i] = ext;
590 err = got_privsep_send_gitconfig_author_name_req(ibuf);
594 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
598 err = got_privsep_send_gitconfig_author_email_req(ibuf);
602 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
606 if (remotes && nremotes) {
607 err = got_privsep_send_gitconfig_remotes_req(ibuf);
611 err = got_privsep_recv_gitconfig_remotes(remotes,
617 if (gitconfig_owner) {
618 err = got_privsep_send_gitconfig_owner_req(ibuf);
621 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
626 err = got_privsep_send_stop(imsg_fds[0]);
627 child_err = got_privsep_wait_for_child(pid);
628 if (child_err && err == NULL)
631 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
632 err = got_error_from_errno("close");
633 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
634 err = got_error_from_errno("close");
635 if (fd != -1 && close(fd) == -1 && err == NULL)
636 err = got_error_from_errno2("close", gitconfig_path);
641 static const struct got_error *
642 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
644 const struct got_error *err = NULL;
645 char *repo_gitconfig_path = NULL;
647 if (global_gitconfig_path) {
648 /* Read settings from ~/.gitconfig. */
649 int dummy_repo_version;
650 err = parse_gitconfig_file(&dummy_repo_version,
651 &repo->global_gitconfig_author_name,
652 &repo->global_gitconfig_author_email,
653 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
658 /* Read repository's .git/config file. */
659 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
660 if (repo_gitconfig_path == NULL)
661 return got_error_from_errno("got_repo_get_path_gitconfig");
663 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
664 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
665 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
666 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
667 repo_gitconfig_path);
671 free(repo_gitconfig_path);
675 static const struct got_error *
676 read_gotconfig(struct got_repository *repo)
678 const struct got_error *err = NULL;
679 char *gotconfig_path;
681 gotconfig_path = got_repo_get_path_gotconfig(repo);
682 if (gotconfig_path == NULL)
683 return got_error_from_errno("got_repo_get_path_gotconfig");
685 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
686 free(gotconfig_path);
690 /* Supported repository format extensions. */
691 static const char *const repo_extensions[] = {
692 "noop", /* Got supports repository format version 1. */
693 "preciousObjects", /* Supported by gotadmin cleanup. */
694 "worktreeConfig", /* Got does not care about Git work trees. */
697 const struct got_error *
698 got_repo_open(struct got_repository **repop, const char *path,
699 const char *global_gitconfig_path, int *pack_fds)
701 struct got_repository *repo = NULL;
702 const struct got_error *err = NULL;
703 char *repo_path = NULL;
709 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
710 return got_error_from_errno("getrlimit");
712 repo = calloc(1, sizeof(*repo));
714 return got_error_from_errno("calloc");
716 RB_INIT(&repo->packidx_bloom_filters);
717 TAILQ_INIT(&repo->packidx_paths);
719 for (i = 0; i < nitems(repo->privsep_children); i++) {
720 memset(&repo->privsep_children[i], 0,
721 sizeof(repo->privsep_children[0]));
722 repo->privsep_children[i].imsg_fd = -1;
725 err = got_object_cache_init(&repo->objcache,
726 GOT_OBJECT_CACHE_TYPE_OBJ);
729 err = got_object_cache_init(&repo->treecache,
730 GOT_OBJECT_CACHE_TYPE_TREE);
733 err = got_object_cache_init(&repo->commitcache,
734 GOT_OBJECT_CACHE_TYPE_COMMIT);
737 err = got_object_cache_init(&repo->tagcache,
738 GOT_OBJECT_CACHE_TYPE_TAG);
741 err = got_object_cache_init(&repo->rawcache,
742 GOT_OBJECT_CACHE_TYPE_RAW);
746 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
747 if (repo->pack_cache_size > rl.rlim_cur / 8)
748 repo->pack_cache_size = rl.rlim_cur / 8;
749 for (i = 0; i < nitems(repo->packs); i++) {
750 if (i < repo->pack_cache_size) {
751 repo->packs[i].basefd = pack_fds[j++];
752 repo->packs[i].accumfd = pack_fds[j++];
754 repo->packs[i].basefd = -1;
755 repo->packs[i].accumfd = -1;
759 repo_path = realpath(path, NULL);
760 if (repo_path == NULL) {
761 err = got_error_from_errno2("realpath", path);
768 err = open_repo(repo, repo_path);
771 if (err->code != GOT_ERR_NOT_GIT_REPO)
773 if (repo_path[0] == '/' && repo_path[1] == '\0') {
774 err = got_error(GOT_ERR_NOT_GIT_REPO);
777 err = got_path_dirname(&parent_path, repo_path);
781 repo_path = parent_path;
784 err = read_gotconfig(repo);
788 err = read_gitconfig(repo, global_gitconfig_path);
791 if (repo->gitconfig_repository_format_version != 0)
792 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
793 for (i = 0; i < repo->nextensions; i++) {
794 char *ext = repo->extensions[i];
795 int j, supported = 0;
796 for (j = 0; j < nitems(repo_extensions); j++) {
797 if (strcmp(ext, repo_extensions[j]) == 0) {
803 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
808 err = got_repo_list_packidx(&repo->packidx_paths, repo);
811 got_repo_close(repo);
818 const struct got_error *
819 got_repo_close(struct got_repository *repo)
821 const struct got_error *err = NULL, *child_err;
822 struct got_packidx_bloom_filter *bf;
823 struct got_pathlist_entry *pe;
826 for (i = 0; i < repo->pack_cache_size; i++) {
827 if (repo->packidx_cache[i] == NULL)
829 got_packidx_close(repo->packidx_cache[i]);
832 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
833 &repo->packidx_bloom_filters))) {
834 RB_REMOVE(got_packidx_bloom_filter_tree,
835 &repo->packidx_bloom_filters, bf);
840 for (i = 0; i < repo->pack_cache_size; i++)
841 if (repo->packs[i].path_packfile)
842 if (repo->packs[i].path_packfile)
843 got_pack_close(&repo->packs[i]);
846 free(repo->path_git_dir);
848 got_object_cache_close(&repo->objcache);
849 got_object_cache_close(&repo->treecache);
850 got_object_cache_close(&repo->commitcache);
851 got_object_cache_close(&repo->tagcache);
852 got_object_cache_close(&repo->rawcache);
854 for (i = 0; i < nitems(repo->privsep_children); i++) {
855 if (repo->privsep_children[i].imsg_fd == -1)
857 imsg_clear(repo->privsep_children[i].ibuf);
858 free(repo->privsep_children[i].ibuf);
859 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
860 child_err = got_privsep_wait_for_child(
861 repo->privsep_children[i].pid);
862 if (child_err && err == NULL)
864 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
866 err = got_error_from_errno("close");
869 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
871 err = got_error_from_errno("close");
874 got_gotconfig_free(repo->gotconfig);
875 free(repo->gitconfig_author_name);
876 free(repo->gitconfig_author_email);
877 for (i = 0; i < repo->ngitconfig_remotes; i++)
878 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
879 free(repo->gitconfig_remotes);
880 for (i = 0; i < repo->nextensions; i++)
881 free(repo->extensions[i]);
882 free(repo->extensions);
884 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
885 free((void *)pe->path);
886 got_pathlist_free(&repo->packidx_paths);
893 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
899 free(repo->fetch_url);
900 repo->fetch_url = NULL;
901 free(repo->send_url);
902 repo->send_url = NULL;
903 for (i = 0; i < repo->nfetch_branches; i++)
904 free(repo->fetch_branches[i]);
905 free(repo->fetch_branches);
906 repo->fetch_branches = NULL;
907 repo->nfetch_branches = 0;
908 for (i = 0; i < repo->nsend_branches; i++)
909 free(repo->send_branches[i]);
910 free(repo->send_branches);
911 repo->send_branches = NULL;
912 repo->nsend_branches = 0;
915 const struct got_error *
916 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
917 const char *input_path)
919 const struct got_error *err = NULL;
920 const char *repo_abspath = NULL;
922 char *canonpath, *path = NULL;
924 *in_repo_path = NULL;
926 canonpath = strdup(input_path);
927 if (canonpath == NULL) {
928 err = got_error_from_errno("strdup");
931 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
935 repo_abspath = got_repo_get_path(repo);
937 if (canonpath[0] == '\0') {
938 path = strdup(canonpath);
940 err = got_error_from_errno("strdup");
944 path = realpath(canonpath, NULL);
946 if (errno != ENOENT) {
947 err = got_error_from_errno2("realpath",
952 * Path is not on disk.
953 * Assume it is already relative to repository root.
955 path = strdup(canonpath);
957 err = got_error_from_errno("strdup");
962 repolen = strlen(repo_abspath);
966 if (strcmp(path, repo_abspath) == 0) {
970 err = got_error_from_errno("strdup");
973 } else if (len > repolen &&
974 got_path_is_child(path, repo_abspath, repolen)) {
975 /* Matched an on-disk path inside repository. */
976 if (got_repo_is_bare(repo)) {
978 * Matched an on-disk path inside repository
979 * database. Treat input as repository-relative.
986 /* Strip common prefix with repository path. */
987 err = got_path_skip_common_ancestor(&child,
996 * Matched unrelated on-disk path.
997 * Treat input as repository-relative.
1005 /* Make in-repository path absolute */
1006 if (path[0] != '/') {
1008 if (asprintf(&abspath, "/%s", path) == -1) {
1009 err = got_error_from_errno("asprintf");
1021 *in_repo_path = path;
1025 static const struct got_error *
1026 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1027 const char *path_packidx)
1029 const struct got_error *err = NULL;
1032 for (i = 0; i < repo->pack_cache_size; i++) {
1033 if (repo->packidx_cache[i] == NULL)
1035 if (strcmp(repo->packidx_cache[i]->path_packidx,
1036 path_packidx) == 0) {
1037 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1040 if (i == repo->pack_cache_size) {
1041 i = repo->pack_cache_size - 1;
1042 err = got_packidx_close(repo->packidx_cache[i]);
1047 repo->packidx_cache[i] = packidx;
1053 got_repo_is_packidx_filename(const char *name, size_t len)
1055 if (len != GOT_PACKIDX_NAMELEN)
1058 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1061 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1062 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1068 static struct got_packidx_bloom_filter *
1069 get_packidx_bloom_filter(struct got_repository *repo,
1070 const char *path, size_t path_len)
1072 struct got_packidx_bloom_filter key;
1074 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1075 return NULL; /* XXX */
1076 key.path_len = path_len;
1078 return RB_FIND(got_packidx_bloom_filter_tree,
1079 &repo->packidx_bloom_filters, &key);
1083 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1084 const char *path_packidx, struct got_object_id *id)
1086 struct got_packidx_bloom_filter *bf;
1088 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1090 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1092 /* No bloom filter means this pack index must be searched. */
1096 static const struct got_error *
1097 add_packidx_bloom_filter(struct got_repository *repo,
1098 struct got_packidx *packidx, const char *path_packidx)
1100 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1101 struct got_packidx_bloom_filter *bf;
1105 * Don't use bloom filters for very large pack index files.
1106 * Large pack files will contain a relatively large fraction
1107 * of our objects so we will likely need to visit them anyway.
1108 * The more objects a pack file contains the higher the probability
1109 * of a false-positive match from the bloom filter. And reading
1110 * all object IDs from a large pack index file can be expensive.
1112 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1115 /* Do we already have a filter for this pack index? */
1116 if (get_packidx_bloom_filter(repo, path_packidx,
1117 strlen(path_packidx)) != NULL)
1120 bf = calloc(1, sizeof(*bf));
1122 return got_error_from_errno("calloc");
1123 bf->bloom = calloc(1, sizeof(*bf->bloom));
1124 if (bf->bloom == NULL) {
1126 return got_error_from_errno("calloc");
1129 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1130 if (len >= sizeof(bf->path)) {
1133 return got_error(GOT_ERR_NO_SPACE);
1137 /* Minimum size supported by our bloom filter is 1000 entries. */
1138 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1139 for (i = 0; i < nobjects; i++) {
1140 struct got_packidx_object_id *id;
1141 id = &packidx->hdr.sorted_ids[i];
1142 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1145 RB_INSERT(got_packidx_bloom_filter_tree,
1146 &repo->packidx_bloom_filters, bf);
1150 const struct got_error *
1151 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1152 struct got_repository *repo, struct got_object_id *id)
1154 const struct got_error *err;
1155 struct got_pathlist_entry *pe;
1158 /* Search pack index cache. */
1159 for (i = 0; i < repo->pack_cache_size; i++) {
1160 if (repo->packidx_cache[i] == NULL)
1162 if (!got_repo_check_packidx_bloom_filter(repo,
1163 repo->packidx_cache[i]->path_packidx, id))
1164 continue; /* object will not be found in this index */
1165 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1167 *packidx = repo->packidx_cache[i];
1169 * Move this cache entry to the front. Repeatedly
1170 * searching a wrong pack index can be expensive.
1173 memmove(&repo->packidx_cache[1],
1174 &repo->packidx_cache[0],
1175 i * sizeof(repo->packidx_cache[0]));
1176 repo->packidx_cache[0] = *packidx;
1181 /* No luck. Search the filesystem. */
1183 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1184 const char *path_packidx = pe->path;
1187 if (!got_repo_check_packidx_bloom_filter(repo,
1189 continue; /* object will not be found in this index */
1191 for (i = 0; i < repo->pack_cache_size; i++) {
1192 if (repo->packidx_cache[i] == NULL)
1194 if (strcmp(repo->packidx_cache[i]->path_packidx,
1195 path_packidx) == 0) {
1201 continue; /* already searched */
1203 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1208 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1212 err = cache_packidx(repo, *packidx, path_packidx);
1216 *idx = got_packidx_get_object_idx(*packidx, id);
1218 err = NULL; /* found the object */
1223 err = got_error_no_obj(id);
1228 const struct got_error *
1229 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1230 struct got_repository *repo)
1232 const struct got_error *err = NULL;
1233 DIR *packdir = NULL;
1234 struct dirent *dent;
1235 char *path_packidx = NULL;
1238 packdir_fd = openat(got_repo_get_fd(repo),
1239 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1240 if (packdir_fd == -1) {
1241 return got_error_from_errno_fmt("openat: %s/%s",
1242 got_repo_get_path_git_dir(repo),
1243 GOT_OBJECTS_PACK_DIR);
1246 packdir = fdopendir(packdir_fd);
1247 if (packdir == NULL) {
1248 err = got_error_from_errno("fdopendir");
1252 while ((dent = readdir(packdir)) != NULL) {
1253 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1256 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1257 dent->d_name) == -1) {
1258 err = got_error_from_errno("asprintf");
1259 path_packidx = NULL;
1263 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1270 if (packdir && closedir(packdir) != 0 && err == NULL)
1271 err = got_error_from_errno("closedir");
1275 const struct got_error *
1276 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1277 struct got_repository *repo)
1279 const struct got_error *err;
1284 /* Search pack index cache. */
1285 for (i = 0; i < repo->pack_cache_size; i++) {
1286 if (repo->packidx_cache[i] == NULL)
1288 if (strcmp(repo->packidx_cache[i]->path_packidx,
1289 path_packidx) == 0) {
1290 *packidx = repo->packidx_cache[i];
1294 /* No luck. Search the filesystem. */
1296 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1301 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1305 err = cache_packidx(repo, *packidx, path_packidx);
1308 got_packidx_close(*packidx);
1314 static const struct got_error *
1315 read_packfile_hdr(int fd, struct got_packidx *packidx)
1317 const struct got_error *err = NULL;
1318 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1319 struct got_packfile_hdr hdr;
1322 n = read(fd, &hdr, sizeof(hdr));
1324 return got_error_from_errno("read");
1325 if (n != sizeof(hdr))
1326 return got_error(GOT_ERR_BAD_PACKFILE);
1328 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1329 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1330 be32toh(hdr.nobjects) != totobj)
1331 err = got_error(GOT_ERR_BAD_PACKFILE);
1336 static const struct got_error *
1337 open_packfile(int *fd, struct got_repository *repo,
1338 const char *relpath, struct got_packidx *packidx)
1340 const struct got_error *err = NULL;
1342 *fd = openat(got_repo_get_fd(repo), relpath,
1343 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1345 return got_error_from_errno_fmt("openat: %s/%s",
1346 got_repo_get_path_git_dir(repo), relpath);
1349 err = read_packfile_hdr(*fd, packidx);
1359 const struct got_error *
1360 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1361 const char *path_packfile, struct got_packidx *packidx)
1363 const struct got_error *err = NULL;
1364 struct got_pack *pack = NULL;
1371 for (i = 0; i < repo->pack_cache_size; i++) {
1372 pack = &repo->packs[i];
1373 if (pack->path_packfile == NULL)
1375 if (strcmp(pack->path_packfile, path_packfile) == 0)
1376 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1379 if (i == repo->pack_cache_size) {
1380 struct got_pack tmp;
1381 err = got_pack_close(&repo->packs[i - 1]);
1384 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1385 return got_error_from_errno("ftruncate");
1386 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1387 return got_error_from_errno("ftruncate");
1388 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1389 memcpy(&repo->packs[i - 1], &repo->packs[0],
1390 sizeof(repo->packs[i - 1]));
1391 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1395 pack = &repo->packs[i];
1397 pack->path_packfile = strdup(path_packfile);
1398 if (pack->path_packfile == NULL) {
1399 err = got_error_from_errno("strdup");
1403 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1407 if (fstat(pack->fd, &sb) != 0) {
1408 err = got_error_from_errno("fstat");
1411 pack->filesize = sb.st_size;
1413 pack->privsep_child = NULL;
1415 #ifndef GOT_PACK_NO_MMAP
1416 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1418 if (pack->map == MAP_FAILED) {
1419 if (errno != ENOMEM) {
1420 err = got_error_from_errno("mmap");
1423 pack->map = NULL; /* fall back to read(2) */
1429 free(pack->path_packfile);
1430 memset(pack, 0, sizeof(*pack));
1438 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1440 struct got_pack *pack = NULL;
1443 for (i = 0; i < repo->pack_cache_size; i++) {
1444 pack = &repo->packs[i];
1445 if (pack->path_packfile == NULL)
1447 if (strcmp(pack->path_packfile, path_packfile) == 0)
1454 const struct got_error *
1455 got_repo_init(const char *repo_path)
1457 const struct got_error *err = NULL;
1458 const char *dirnames[] = {
1460 GOT_OBJECTS_PACK_DIR,
1463 const char *description_str = "Unnamed repository; "
1464 "edit this file 'description' to name the repository.";
1465 const char *headref_str = "ref: refs/heads/main";
1466 const char *gitconfig_str = "[core]\n"
1467 "\trepositoryformatversion = 0\n"
1468 "\tfilemode = true\n"
1473 if (!got_path_dir_is_empty(repo_path))
1474 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1476 for (i = 0; i < nitems(dirnames); i++) {
1477 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1478 return got_error_from_errno("asprintf");
1480 err = got_path_mkdir(path);
1486 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1487 return got_error_from_errno("asprintf");
1488 err = got_path_create_file(path, description_str);
1493 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1494 return got_error_from_errno("asprintf");
1495 err = got_path_create_file(path, headref_str);
1500 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1501 return got_error_from_errno("asprintf");
1502 err = got_path_create_file(path, gitconfig_str);
1510 static const struct got_error *
1511 match_packed_object(struct got_object_id **unique_id,
1512 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1514 const struct got_error *err = NULL;
1515 struct got_object_id_queue matched_ids;
1516 struct got_pathlist_entry *pe;
1518 STAILQ_INIT(&matched_ids);
1520 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1521 const char *path_packidx = pe->path;
1522 struct got_packidx *packidx;
1523 struct got_object_qid *qid;
1525 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1530 err = got_packidx_match_id_str_prefix(&matched_ids,
1531 packidx, id_str_prefix);
1533 got_packidx_close(packidx);
1536 err = got_packidx_close(packidx);
1540 STAILQ_FOREACH(qid, &matched_ids, entry) {
1541 if (obj_type != GOT_OBJ_TYPE_ANY) {
1543 err = got_object_get_type(&matched_type, repo,
1547 if (matched_type != obj_type)
1550 if (*unique_id == NULL) {
1551 *unique_id = got_object_id_dup(&qid->id);
1552 if (*unique_id == NULL) {
1553 err = got_error_from_errno("malloc");
1557 if (got_object_id_cmp(*unique_id,
1559 continue; /* packed multiple times */
1560 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1566 got_object_id_queue_free(&matched_ids);
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 len = strlen(id_str_prefix);
1673 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1674 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1676 for (i = 0; i < len; i++) {
1677 if (isxdigit((unsigned char)id_str_prefix[i]))
1679 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1683 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1686 object_dir = strndup(id_str_prefix, 2);
1687 if (object_dir == NULL) {
1688 err = got_error_from_errno("strdup");
1691 err = match_loose_object(id, path_objects, object_dir,
1692 id_str_prefix, obj_type, repo);
1693 } else if (len == 1) {
1695 for (i = 0; i < 0xf; i++) {
1696 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1698 err = got_error_from_errno("asprintf");
1701 err = match_packed_object(id, repo, object_dir,
1705 err = match_loose_object(id, path_objects, object_dir,
1706 id_str_prefix, obj_type, repo);
1711 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1719 } else if (*id == NULL) {
1721 case GOT_OBJ_TYPE_BLOB:
1722 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1723 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1725 case GOT_OBJ_TYPE_TREE:
1726 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1727 GOT_OBJ_LABEL_TREE, id_str_prefix);
1729 case GOT_OBJ_TYPE_COMMIT:
1730 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1731 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1733 case GOT_OBJ_TYPE_TAG:
1734 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1735 GOT_OBJ_LABEL_TAG, id_str_prefix);
1738 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1746 const struct got_error *
1747 got_repo_match_object_id(struct got_object_id **id, char **label,
1748 const char *id_str, int obj_type, struct got_reflist_head *refs,
1749 struct got_repository *repo)
1751 const struct got_error *err;
1752 struct got_tag_object *tag;
1753 struct got_reference *ref = NULL;
1760 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1763 *id = got_object_id_dup(
1764 got_object_tag_get_object_id(tag));
1766 err = got_error_from_errno("got_object_id_dup");
1767 else if (label && asprintf(label, "refs/tags/%s",
1768 got_object_tag_get_name(tag)) == -1) {
1769 err = got_error_from_errno("asprintf");
1773 got_object_tag_close(tag);
1775 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1776 err->code != GOT_ERR_NO_OBJ)
1780 err = got_ref_open(&ref, repo, id_str, 0);
1782 err = got_ref_resolve(id, repo, ref);
1786 *label = strdup(got_ref_get_name(ref));
1787 if (*label == NULL) {
1788 err = got_error_from_errno("strdup");
1793 if (err->code != GOT_ERR_NOT_REF &&
1794 err->code != GOT_ERR_BAD_REF_NAME)
1796 err = got_repo_match_object_id_prefix(id, id_str,
1799 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1800 err = got_error_not_ref(id_str);
1804 err = got_object_id_str(label, *id);
1805 if (*label == NULL) {
1806 err = got_error_from_errno("strdup");
1817 const struct got_error *
1818 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1819 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1821 const struct got_error *err = NULL;
1822 struct got_reflist_entry *re;
1823 struct got_object_id *tag_id;
1824 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1828 TAILQ_FOREACH(re, refs, entry) {
1829 const char *refname;
1830 refname = got_ref_get_name(re->ref);
1831 if (got_ref_is_symbolic(re->ref))
1833 if (strncmp(refname, "refs/tags/", 10) != 0)
1835 if (!name_is_absolute)
1836 refname += strlen("refs/tags/");
1837 if (strcmp(refname, name) != 0)
1839 err = got_ref_resolve(&tag_id, repo, re->ref);
1842 err = got_object_open_as_tag(tag, repo, tag_id);
1846 if (obj_type == GOT_OBJ_TYPE_ANY ||
1847 got_object_tag_get_object_type(*tag) == obj_type)
1849 got_object_tag_close(*tag);
1853 if (err == NULL && *tag == NULL)
1854 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1855 GOT_OBJ_LABEL_TAG, name);
1859 static const struct got_error *
1860 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1861 const char *name, mode_t mode, struct got_object_id *blob_id)
1863 const struct got_error *err = NULL;
1867 *new_te = calloc(1, sizeof(**new_te));
1868 if (*new_te == NULL)
1869 return got_error_from_errno("calloc");
1871 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1872 sizeof((*new_te)->name)) {
1873 err = got_error(GOT_ERR_NO_SPACE);
1877 if (S_ISLNK(mode)) {
1878 (*new_te)->mode = S_IFLNK;
1880 (*new_te)->mode = S_IFREG;
1881 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1883 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1885 if (err && *new_te) {
1892 static const struct got_error *
1893 import_file(struct got_tree_entry **new_te, struct dirent *de,
1894 const char *path, struct got_repository *repo)
1896 const struct got_error *err;
1897 struct got_object_id *blob_id = NULL;
1901 if (asprintf(&filepath, "%s%s%s", path,
1902 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1903 return got_error_from_errno("asprintf");
1905 if (lstat(filepath, &sb) != 0) {
1906 err = got_error_from_errno2("lstat", path);
1910 err = got_object_blob_create(&blob_id, filepath, repo);
1914 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1923 static const struct got_error *
1924 insert_tree_entry(struct got_tree_entry *new_te,
1925 struct got_pathlist_head *paths)
1927 const struct got_error *err = NULL;
1928 struct got_pathlist_entry *new_pe;
1930 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1934 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1938 static const struct got_error *write_tree(struct got_object_id **,
1939 const char *, struct got_pathlist_head *, struct got_repository *,
1940 got_repo_import_cb progress_cb, void *progress_arg);
1942 static const struct got_error *
1943 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1944 const char *path, struct got_pathlist_head *ignores,
1945 struct got_repository *repo,
1946 got_repo_import_cb progress_cb, void *progress_arg)
1948 const struct got_error *err;
1949 struct got_object_id *id = NULL;
1952 if (asprintf(&subdirpath, "%s%s%s", path,
1953 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1954 return got_error_from_errno("asprintf");
1956 (*new_te) = calloc(1, sizeof(**new_te));
1957 if (*new_te == NULL)
1958 return got_error_from_errno("calloc");
1959 (*new_te)->mode = S_IFDIR;
1960 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1961 sizeof((*new_te)->name)) {
1962 err = got_error(GOT_ERR_NO_SPACE);
1965 err = write_tree(&id, subdirpath, ignores, repo,
1966 progress_cb, progress_arg);
1969 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1981 static const struct got_error *
1982 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1983 struct got_pathlist_head *ignores, struct got_repository *repo,
1984 got_repo_import_cb progress_cb, void *progress_arg)
1986 const struct got_error *err = NULL;
1990 struct got_tree_entry *new_te = NULL;
1991 struct got_pathlist_head paths;
1992 struct got_pathlist_entry *pe;
1994 *new_tree_id = NULL;
1998 dir = opendir(path_dir);
2000 err = got_error_from_errno2("opendir", path_dir);
2005 while ((de = readdir(dir)) != NULL) {
2009 if (strcmp(de->d_name, ".") == 0 ||
2010 strcmp(de->d_name, "..") == 0)
2013 TAILQ_FOREACH(pe, ignores, entry) {
2014 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2022 err = got_path_dirent_type(&type, path_dir, de);
2026 if (type == DT_DIR) {
2027 err = import_subdir(&new_te, de, path_dir,
2028 ignores, repo, progress_cb, progress_arg);
2030 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2035 } else if (type == DT_REG || type == DT_LNK) {
2036 err = import_file(&new_te, de, path_dir, repo);
2042 err = insert_tree_entry(new_te, &paths);
2048 if (TAILQ_EMPTY(&paths)) {
2049 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2050 "cannot create tree without any entries");
2054 TAILQ_FOREACH(pe, &paths, entry) {
2055 struct got_tree_entry *te = pe->data;
2057 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2059 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2060 err = got_error_from_errno("asprintf");
2063 err = (*progress_cb)(progress_arg, path);
2069 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2073 got_pathlist_free(&paths);
2077 const struct got_error *
2078 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2079 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2080 struct got_repository *repo, got_repo_import_cb progress_cb,
2083 const struct got_error *err;
2084 struct got_object_id *new_tree_id;
2086 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2087 progress_cb, progress_arg);
2091 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2092 author, time(NULL), author, time(NULL), logmsg, repo);
2097 const struct got_error *
2098 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2099 struct got_repository *repo)
2101 const struct got_error *err = NULL;
2102 char *path_objects = NULL, *path = NULL;
2104 struct got_object_id id;
2110 path_objects = got_repo_get_path_objects(repo);
2111 if (path_objects == NULL)
2112 return got_error_from_errno("got_repo_get_path_objects");
2114 for (i = 0; i <= 0xff; i++) {
2115 struct dirent *dent;
2117 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2118 err = got_error_from_errno("asprintf");
2122 dir = opendir(path);
2124 if (errno == ENOENT) {
2128 err = got_error_from_errno2("opendir", path);
2132 while ((dent = readdir(dir)) != NULL) {
2137 if (strcmp(dent->d_name, ".") == 0 ||
2138 strcmp(dent->d_name, "..") == 0)
2141 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2142 err = got_error_from_errno("asprintf");
2146 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2152 err = got_object_open_loose_fd(&fd, &id, repo);
2156 if (fstat(fd, &sb) == -1) {
2157 err = got_error_from_errno("fstat");
2162 (*ondisk_size) += sb.st_size;
2164 if (close(fd) == -1) {
2165 err = got_error_from_errno("close");
2170 if (closedir(dir) != 0) {
2171 err = got_error_from_errno("closedir");
2180 if (dir && closedir(dir) != 0 && err == NULL)
2181 err = got_error_from_errno("closedir");
2192 const struct got_error *
2193 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2194 off_t *total_packsize, struct got_repository *repo)
2196 const struct got_error *err = NULL;
2197 DIR *packdir = NULL;
2198 struct dirent *dent;
2199 struct got_packidx *packidx = NULL;
2201 char *path_packfile;
2207 *total_packsize = 0;
2209 packdir_fd = openat(got_repo_get_fd(repo),
2210 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2211 if (packdir_fd == -1) {
2212 return got_error_from_errno_fmt("openat: %s/%s",
2213 got_repo_get_path_git_dir(repo),
2214 GOT_OBJECTS_PACK_DIR);
2217 packdir = fdopendir(packdir_fd);
2218 if (packdir == NULL) {
2219 err = got_error_from_errno("fdopendir");
2223 while ((dent = readdir(packdir)) != NULL) {
2224 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2227 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2228 dent->d_name) == -1) {
2229 err = got_error_from_errno("asprintf");
2233 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2239 if (fstat(packidx->fd, &sb) == -1)
2241 *total_packsize += sb.st_size;
2243 err = got_packidx_get_packfile_path(&path_packfile,
2244 packidx->path_packidx);
2248 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2250 free(path_packfile);
2253 free(path_packfile);
2254 *total_packsize += sb.st_size;
2256 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2260 got_packidx_close(packidx);
2265 got_packidx_close(packidx);
2266 if (packdir && closedir(packdir) != 0 && err == NULL)
2267 err = got_error_from_errno("closedir");
2271 *total_packsize = 0;
2276 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2277 got_packidx_bloom_filter_cmp);