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 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
72 got_packidx_bloom_filter_cmp);
75 got_repo_get_path(struct got_repository *repo)
81 got_repo_get_path_git_dir(struct got_repository *repo)
83 return repo->path_git_dir;
87 got_repo_get_fd(struct got_repository *repo)
89 return repo->gitdir_fd;
93 got_repo_get_gitconfig_author_name(struct got_repository *repo)
95 return repo->gitconfig_author_name;
99 got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 return repo->gitconfig_author_email;
105 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 return repo->global_gitconfig_author_name;
111 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 return repo->global_gitconfig_author_email;
117 got_repo_get_gitconfig_owner(struct got_repository *repo)
119 return repo->gitconfig_owner;
123 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
124 struct got_repository *repo)
126 *extensions = repo->extensions;
127 *nextensions = repo->nextensions;
131 got_repo_is_bare(struct got_repository *repo)
133 return (strcmp(repo->path, repo->path_git_dir) == 0);
137 get_path_git_child(struct got_repository *repo, const char *basename)
141 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
149 got_repo_get_path_objects(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_DIR);
155 got_repo_get_path_objects_pack(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
161 got_repo_get_path_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_REFS_DIR);
167 got_repo_get_path_packed_refs(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
173 get_path_head(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_HEAD_FILE);
179 got_repo_get_path_gitconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GITCONFIG);
185 got_repo_get_path_gotconfig(struct got_repository *repo)
187 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
190 const struct got_gotconfig *
191 got_repo_get_gotconfig(struct got_repository *repo)
193 return repo->gotconfig;
197 got_repo_get_gitconfig_remotes(int *nremotes,
198 const struct got_remote_repo **remotes, struct got_repository *repo)
200 *nremotes = repo->ngitconfig_remotes;
201 *remotes = repo->gitconfig_remotes;
205 is_git_repo(struct got_repository *repo)
207 const char *path_git = got_repo_get_path_git_dir(repo);
208 char *path_objects = got_repo_get_path_objects(repo);
209 char *path_refs = got_repo_get_path_refs(repo);
210 char *path_head = get_path_head(repo);
213 struct got_reference *head_ref;
215 if (lstat(path_git, &sb) == -1)
217 if (!S_ISDIR(sb.st_mode))
220 if (lstat(path_objects, &sb) == -1)
222 if (!S_ISDIR(sb.st_mode))
225 if (lstat(path_refs, &sb) == -1)
227 if (!S_ISDIR(sb.st_mode))
230 if (lstat(path_head, &sb) == -1)
232 if (!S_ISREG(sb.st_mode))
235 /* Check if the HEAD reference can be opened. */
236 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
238 got_ref_close(head_ref);
249 const struct got_error *
250 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
251 struct got_object *obj)
253 #ifndef GOT_NO_OBJ_CACHE
254 const struct got_error *err = NULL;
255 err = got_object_cache_add(&repo->objcache, id, obj);
257 if (err->code == GOT_ERR_OBJ_EXISTS ||
258 err->code == GOT_ERR_OBJ_TOO_LARGE)
268 got_repo_get_cached_object(struct got_repository *repo,
269 struct got_object_id *id)
271 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
274 const struct got_error *
275 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
276 struct got_tree_object *tree)
278 #ifndef GOT_NO_OBJ_CACHE
279 const struct got_error *err = NULL;
280 err = got_object_cache_add(&repo->treecache, id, tree);
282 if (err->code == GOT_ERR_OBJ_EXISTS ||
283 err->code == GOT_ERR_OBJ_TOO_LARGE)
292 struct got_tree_object *
293 got_repo_get_cached_tree(struct got_repository *repo,
294 struct got_object_id *id)
296 return (struct got_tree_object *)got_object_cache_get(
297 &repo->treecache, id);
300 const struct got_error *
301 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
302 struct got_commit_object *commit)
304 #ifndef GOT_NO_OBJ_CACHE
305 const struct got_error *err = NULL;
306 err = got_object_cache_add(&repo->commitcache, id, commit);
308 if (err->code == GOT_ERR_OBJ_EXISTS ||
309 err->code == GOT_ERR_OBJ_TOO_LARGE)
318 struct got_commit_object *
319 got_repo_get_cached_commit(struct got_repository *repo,
320 struct got_object_id *id)
322 return (struct got_commit_object *)got_object_cache_get(
323 &repo->commitcache, id);
326 const struct got_error *
327 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
328 struct got_tag_object *tag)
330 #ifndef GOT_NO_OBJ_CACHE
331 const struct got_error *err = NULL;
332 err = got_object_cache_add(&repo->tagcache, id, tag);
334 if (err->code == GOT_ERR_OBJ_EXISTS ||
335 err->code == GOT_ERR_OBJ_TOO_LARGE)
344 struct got_tag_object *
345 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
347 return (struct got_tag_object *)got_object_cache_get(
348 &repo->tagcache, id);
351 const struct got_error *
352 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
353 struct got_raw_object *raw)
355 #ifndef GOT_NO_OBJ_CACHE
356 const struct got_error *err = NULL;
357 err = got_object_cache_add(&repo->rawcache, id, raw);
359 if (err->code == GOT_ERR_OBJ_EXISTS ||
360 err->code == GOT_ERR_OBJ_TOO_LARGE)
370 struct got_raw_object *
371 got_repo_get_cached_raw_object(struct got_repository *repo,
372 struct got_object_id *id)
374 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
378 static const struct got_error *
379 open_repo(struct got_repository *repo, const char *path)
381 const struct got_error *err = NULL;
383 repo->gitdir_fd = -1;
385 /* bare git repository? */
386 repo->path_git_dir = strdup(path);
387 if (repo->path_git_dir == NULL)
388 return got_error_from_errno("strdup");
389 if (is_git_repo(repo)) {
390 repo->path = strdup(repo->path_git_dir);
391 if (repo->path == NULL) {
392 err = got_error_from_errno("strdup");
395 repo->gitdir_fd = open(repo->path_git_dir,
396 O_DIRECTORY | O_CLOEXEC);
397 if (repo->gitdir_fd == -1) {
398 err = got_error_from_errno2("open",
405 /* git repository with working tree? */
406 free(repo->path_git_dir);
407 repo->path_git_dir = NULL;
408 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
409 err = got_error_from_errno("asprintf");
412 if (is_git_repo(repo)) {
413 repo->path = strdup(path);
414 if (repo->path == NULL) {
415 err = got_error_from_errno("strdup");
418 repo->gitdir_fd = open(repo->path_git_dir,
419 O_DIRECTORY | O_CLOEXEC);
420 if (repo->gitdir_fd == -1) {
421 err = got_error_from_errno2("open",
428 err = got_error(GOT_ERR_NOT_GIT_REPO);
433 free(repo->path_git_dir);
434 repo->path_git_dir = NULL;
435 if (repo->gitdir_fd != -1)
436 close(repo->gitdir_fd);
437 repo->gitdir_fd = -1;
443 static const struct got_error *
444 parse_gitconfig_file(int *gitconfig_repository_format_version,
445 char **gitconfig_author_name, char **gitconfig_author_email,
446 struct got_remote_repo **remotes, int *nremotes,
447 char **gitconfig_owner, char ***extensions, int *nextensions,
448 const char *gitconfig_path)
450 const struct got_error *err = NULL, *child_err = NULL;
452 int imsg_fds[2] = { -1, -1 };
454 struct imsgbuf *ibuf;
456 *gitconfig_repository_format_version = 0;
461 *gitconfig_author_name = NULL;
462 *gitconfig_author_email = NULL;
468 *gitconfig_owner = NULL;
470 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
474 return got_error_from_errno2("open", gitconfig_path);
477 ibuf = calloc(1, sizeof(*ibuf));
479 err = got_error_from_errno("calloc");
483 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
484 err = got_error_from_errno("socketpair");
490 err = got_error_from_errno("fork");
492 } else if (pid == 0) {
493 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
498 if (close(imsg_fds[1]) == -1) {
499 err = got_error_from_errno("close");
503 imsg_init(ibuf, imsg_fds[0]);
505 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
510 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
514 err = got_privsep_recv_gitconfig_int(
515 gitconfig_repository_format_version, ibuf);
519 if (extensions && nextensions) {
520 err = got_privsep_send_gitconfig_repository_extensions_req(
524 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
527 if (*nextensions > 0) {
529 *extensions = calloc(*nextensions, sizeof(char *));
530 if (*extensions == NULL) {
531 err = got_error_from_errno("calloc");
534 for (i = 0; i < *nextensions; i++) {
536 err = got_privsep_recv_gitconfig_str(&ext,
540 (*extensions)[i] = ext;
545 err = got_privsep_send_gitconfig_author_name_req(ibuf);
549 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
553 err = got_privsep_send_gitconfig_author_email_req(ibuf);
557 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
561 if (remotes && nremotes) {
562 err = got_privsep_send_gitconfig_remotes_req(ibuf);
566 err = got_privsep_recv_gitconfig_remotes(remotes,
572 if (gitconfig_owner) {
573 err = got_privsep_send_gitconfig_owner_req(ibuf);
576 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
581 err = got_privsep_send_stop(imsg_fds[0]);
582 child_err = got_privsep_wait_for_child(pid);
583 if (child_err && err == NULL)
586 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
587 err = got_error_from_errno("close");
588 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
589 err = got_error_from_errno("close");
590 if (fd != -1 && close(fd) == -1 && err == NULL)
591 err = got_error_from_errno2("close", gitconfig_path);
596 static const struct got_error *
597 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
599 const struct got_error *err = NULL;
600 char *repo_gitconfig_path = NULL;
602 if (global_gitconfig_path) {
603 /* Read settings from ~/.gitconfig. */
604 int dummy_repo_version;
605 err = parse_gitconfig_file(&dummy_repo_version,
606 &repo->global_gitconfig_author_name,
607 &repo->global_gitconfig_author_email,
608 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
613 /* Read repository's .git/config file. */
614 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
615 if (repo_gitconfig_path == NULL)
616 return got_error_from_errno("got_repo_get_path_gitconfig");
618 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
619 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
620 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
621 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
622 repo_gitconfig_path);
626 free(repo_gitconfig_path);
630 static const struct got_error *
631 read_gotconfig(struct got_repository *repo)
633 const struct got_error *err = NULL;
634 char *gotconfig_path;
636 gotconfig_path = got_repo_get_path_gotconfig(repo);
637 if (gotconfig_path == NULL)
638 return got_error_from_errno("got_repo_get_path_gotconfig");
640 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
641 free(gotconfig_path);
645 /* Supported repository format extensions. */
646 static const char *const repo_extensions[] = {
647 "noop", /* Got supports repository format version 1. */
648 "preciousObjects", /* Supported by gotadmin cleanup. */
649 "worktreeConfig", /* Got does not care about Git work trees. */
652 const struct got_error *
653 got_repo_open(struct got_repository **repop, const char *path,
654 const char *global_gitconfig_path)
656 struct got_repository *repo = NULL;
657 const struct got_error *err = NULL;
658 char *repo_path = NULL;
664 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
665 return got_error_from_errno("getrlimit");
667 repo = calloc(1, sizeof(*repo));
669 return got_error_from_errno("calloc");
671 RB_INIT(&repo->packidx_bloom_filters);
672 TAILQ_INIT(&repo->packidx_paths);
674 for (i = 0; i < nitems(repo->privsep_children); i++) {
675 memset(&repo->privsep_children[i], 0,
676 sizeof(repo->privsep_children[0]));
677 repo->privsep_children[i].imsg_fd = -1;
680 err = got_object_cache_init(&repo->objcache,
681 GOT_OBJECT_CACHE_TYPE_OBJ);
684 err = got_object_cache_init(&repo->treecache,
685 GOT_OBJECT_CACHE_TYPE_TREE);
688 err = got_object_cache_init(&repo->commitcache,
689 GOT_OBJECT_CACHE_TYPE_COMMIT);
692 err = got_object_cache_init(&repo->tagcache,
693 GOT_OBJECT_CACHE_TYPE_TAG);
696 err = got_object_cache_init(&repo->rawcache,
697 GOT_OBJECT_CACHE_TYPE_RAW);
701 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
702 if (repo->pack_cache_size > rl.rlim_cur / 8)
703 repo->pack_cache_size = rl.rlim_cur / 8;
704 for (i = 0; i < nitems(repo->packs); i++) {
705 if (i < repo->pack_cache_size) {
706 repo->packs[i].basefd = got_opentempfd();
707 if (repo->packs[i].basefd == -1)
708 return got_error_from_errno("got_opentempfd");
709 repo->packs[i].accumfd = got_opentempfd();
710 if (repo->packs[i].accumfd == -1)
711 return got_error_from_errno("got_opentempfd");
713 repo->packs[i].basefd = -1;
714 repo->packs[i].accumfd = -1;
718 repo_path = realpath(path, NULL);
719 if (repo_path == NULL) {
720 err = got_error_from_errno2("realpath", path);
727 err = open_repo(repo, repo_path);
730 if (err->code != GOT_ERR_NOT_GIT_REPO)
732 if (repo_path[0] == '/' && repo_path[1] == '\0') {
733 err = got_error(GOT_ERR_NOT_GIT_REPO);
736 err = got_path_dirname(&parent_path, repo_path);
740 repo_path = parent_path;
743 err = read_gotconfig(repo);
747 err = read_gitconfig(repo, global_gitconfig_path);
750 if (repo->gitconfig_repository_format_version != 0)
751 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
752 for (i = 0; i < repo->nextensions; i++) {
753 char *ext = repo->extensions[i];
754 int j, supported = 0;
755 for (j = 0; j < nitems(repo_extensions); j++) {
756 if (strcmp(ext, repo_extensions[j]) == 0) {
762 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
767 err = got_repo_list_packidx(&repo->packidx_paths, repo);
770 got_repo_close(repo);
777 const struct got_error *
778 got_repo_close(struct got_repository *repo)
780 const struct got_error *err = NULL, *child_err;
781 struct got_packidx_bloom_filter *bf;
782 struct got_pathlist_entry *pe;
785 for (i = 0; i < repo->pack_cache_size; i++) {
786 if (repo->packidx_cache[i] == NULL)
788 got_packidx_close(repo->packidx_cache[i]);
791 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
792 &repo->packidx_bloom_filters))) {
793 RB_REMOVE(got_packidx_bloom_filter_tree,
794 &repo->packidx_bloom_filters, bf);
799 for (i = 0; i < repo->pack_cache_size; i++) {
800 if (repo->packs[i].path_packfile == NULL)
802 got_pack_close(&repo->packs[i]);
803 if (repo->packs[i].basefd != -1) {
804 if (close(repo->packs[i].basefd) == -1 && err == NULL)
805 err = got_error_from_errno("close");
806 repo->packs[i].basefd = -1;
808 if (repo->packs[i].accumfd != -1) {
809 if (close(repo->packs[i].accumfd) == -1 && err == NULL)
810 err = got_error_from_errno("close");
811 repo->packs[i].accumfd = -1;
816 free(repo->path_git_dir);
818 got_object_cache_close(&repo->objcache);
819 got_object_cache_close(&repo->treecache);
820 got_object_cache_close(&repo->commitcache);
821 got_object_cache_close(&repo->tagcache);
822 got_object_cache_close(&repo->rawcache);
824 for (i = 0; i < nitems(repo->privsep_children); i++) {
825 if (repo->privsep_children[i].imsg_fd == -1)
827 imsg_clear(repo->privsep_children[i].ibuf);
828 free(repo->privsep_children[i].ibuf);
829 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
830 child_err = got_privsep_wait_for_child(
831 repo->privsep_children[i].pid);
832 if (child_err && err == NULL)
834 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
836 err = got_error_from_errno("close");
839 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
841 err = got_error_from_errno("close");
844 got_gotconfig_free(repo->gotconfig);
845 free(repo->gitconfig_author_name);
846 free(repo->gitconfig_author_email);
847 for (i = 0; i < repo->ngitconfig_remotes; i++)
848 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
849 free(repo->gitconfig_remotes);
850 for (i = 0; i < repo->nextensions; i++)
851 free(repo->extensions[i]);
852 free(repo->extensions);
854 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
855 free((void *)pe->path);
856 got_pathlist_free(&repo->packidx_paths);
863 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
869 free(repo->fetch_url);
870 repo->fetch_url = NULL;
871 free(repo->send_url);
872 repo->send_url = NULL;
873 for (i = 0; i < repo->nfetch_branches; i++)
874 free(repo->fetch_branches[i]);
875 free(repo->fetch_branches);
876 repo->fetch_branches = NULL;
877 repo->nfetch_branches = 0;
878 for (i = 0; i < repo->nsend_branches; i++)
879 free(repo->send_branches[i]);
880 free(repo->send_branches);
881 repo->send_branches = NULL;
882 repo->nsend_branches = 0;
885 const struct got_error *
886 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
887 const char *input_path)
889 const struct got_error *err = NULL;
890 const char *repo_abspath = NULL;
892 char *canonpath, *path = NULL;
894 *in_repo_path = NULL;
896 canonpath = strdup(input_path);
897 if (canonpath == NULL) {
898 err = got_error_from_errno("strdup");
901 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
905 repo_abspath = got_repo_get_path(repo);
907 if (canonpath[0] == '\0') {
908 path = strdup(canonpath);
910 err = got_error_from_errno("strdup");
914 path = realpath(canonpath, NULL);
916 if (errno != ENOENT) {
917 err = got_error_from_errno2("realpath",
922 * Path is not on disk.
923 * Assume it is already relative to repository root.
925 path = strdup(canonpath);
927 err = got_error_from_errno("strdup");
932 repolen = strlen(repo_abspath);
936 if (strcmp(path, repo_abspath) == 0) {
940 err = got_error_from_errno("strdup");
943 } else if (len > repolen &&
944 got_path_is_child(path, repo_abspath, repolen)) {
945 /* Matched an on-disk path inside repository. */
946 if (got_repo_is_bare(repo)) {
948 * Matched an on-disk path inside repository
949 * database. Treat input as repository-relative.
956 /* Strip common prefix with repository path. */
957 err = got_path_skip_common_ancestor(&child,
966 * Matched unrelated on-disk path.
967 * Treat input as repository-relative.
975 /* Make in-repository path absolute */
976 if (path[0] != '/') {
978 if (asprintf(&abspath, "/%s", path) == -1) {
979 err = got_error_from_errno("asprintf");
991 *in_repo_path = path;
995 static const struct got_error *
996 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
997 const char *path_packidx)
999 const struct got_error *err = NULL;
1002 for (i = 0; i < repo->pack_cache_size; i++) {
1003 if (repo->packidx_cache[i] == NULL)
1005 if (strcmp(repo->packidx_cache[i]->path_packidx,
1006 path_packidx) == 0) {
1007 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1010 if (i == repo->pack_cache_size) {
1011 i = repo->pack_cache_size - 1;
1012 err = got_packidx_close(repo->packidx_cache[i]);
1017 repo->packidx_cache[i] = packidx;
1023 got_repo_is_packidx_filename(const char *name, size_t len)
1025 if (len != GOT_PACKIDX_NAMELEN)
1028 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1031 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1032 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1038 static struct got_packidx_bloom_filter *
1039 get_packidx_bloom_filter(struct got_repository *repo,
1040 const char *path, size_t path_len)
1042 struct got_packidx_bloom_filter key;
1044 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1045 return NULL; /* XXX */
1046 key.path_len = path_len;
1048 return RB_FIND(got_packidx_bloom_filter_tree,
1049 &repo->packidx_bloom_filters, &key);
1053 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1054 const char *path_packidx, struct got_object_id *id)
1056 struct got_packidx_bloom_filter *bf;
1058 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1060 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1062 /* No bloom filter means this pack index must be searched. */
1066 static const struct got_error *
1067 add_packidx_bloom_filter(struct got_repository *repo,
1068 struct got_packidx *packidx, const char *path_packidx)
1070 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1071 struct got_packidx_bloom_filter *bf;
1075 * Don't use bloom filters for very large pack index files.
1076 * Large pack files will contain a relatively large fraction
1077 * of our objects so we will likely need to visit them anyway.
1078 * The more objects a pack file contains the higher the probability
1079 * of a false-positive match from the bloom filter. And reading
1080 * all object IDs from a large pack index file can be expensive.
1082 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1085 /* Do we already have a filter for this pack index? */
1086 if (get_packidx_bloom_filter(repo, path_packidx,
1087 strlen(path_packidx)) != NULL)
1090 bf = calloc(1, sizeof(*bf));
1092 return got_error_from_errno("calloc");
1093 bf->bloom = calloc(1, sizeof(*bf->bloom));
1094 if (bf->bloom == NULL) {
1096 return got_error_from_errno("calloc");
1099 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1100 if (len >= sizeof(bf->path)) {
1103 return got_error(GOT_ERR_NO_SPACE);
1107 /* Minimum size supported by our bloom filter is 1000 entries. */
1108 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1109 for (i = 0; i < nobjects; i++) {
1110 struct got_packidx_object_id *id;
1111 id = &packidx->hdr.sorted_ids[i];
1112 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1115 RB_INSERT(got_packidx_bloom_filter_tree,
1116 &repo->packidx_bloom_filters, bf);
1120 const struct got_error *
1121 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1122 struct got_repository *repo, struct got_object_id *id)
1124 const struct got_error *err;
1125 struct got_pathlist_entry *pe;
1128 /* Search pack index cache. */
1129 for (i = 0; i < repo->pack_cache_size; i++) {
1130 if (repo->packidx_cache[i] == NULL)
1132 if (!got_repo_check_packidx_bloom_filter(repo,
1133 repo->packidx_cache[i]->path_packidx, id))
1134 continue; /* object will not be found in this index */
1135 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1137 *packidx = repo->packidx_cache[i];
1139 * Move this cache entry to the front. Repeatedly
1140 * searching a wrong pack index can be expensive.
1143 memmove(&repo->packidx_cache[1],
1144 &repo->packidx_cache[0],
1145 i * sizeof(repo->packidx_cache[0]));
1146 repo->packidx_cache[0] = *packidx;
1151 /* No luck. Search the filesystem. */
1153 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1154 const char *path_packidx = pe->path;
1157 if (!got_repo_check_packidx_bloom_filter(repo,
1159 continue; /* object will not be found in this index */
1161 for (i = 0; i < repo->pack_cache_size; i++) {
1162 if (repo->packidx_cache[i] == NULL)
1164 if (strcmp(repo->packidx_cache[i]->path_packidx,
1165 path_packidx) == 0) {
1171 continue; /* already searched */
1173 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1178 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1182 err = cache_packidx(repo, *packidx, path_packidx);
1186 *idx = got_packidx_get_object_idx(*packidx, id);
1188 err = NULL; /* found the object */
1193 err = got_error_no_obj(id);
1198 const struct got_error *
1199 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1200 struct got_repository *repo)
1202 const struct got_error *err = NULL;
1203 DIR *packdir = NULL;
1204 struct dirent *dent;
1205 char *path_packidx = NULL;
1208 packdir_fd = openat(got_repo_get_fd(repo),
1209 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1210 if (packdir_fd == -1) {
1211 return got_error_from_errno_fmt("openat: %s/%s",
1212 got_repo_get_path_git_dir(repo),
1213 GOT_OBJECTS_PACK_DIR);
1216 packdir = fdopendir(packdir_fd);
1217 if (packdir == NULL) {
1218 err = got_error_from_errno("fdopendir");
1222 while ((dent = readdir(packdir)) != NULL) {
1223 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1226 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1227 dent->d_name) == -1) {
1228 err = got_error_from_errno("asprintf");
1229 path_packidx = NULL;
1233 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1240 if (packdir && closedir(packdir) != 0 && err == NULL)
1241 err = got_error_from_errno("closedir");
1245 const struct got_error *
1246 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1247 struct got_repository *repo)
1249 const struct got_error *err;
1254 /* Search pack index cache. */
1255 for (i = 0; i < repo->pack_cache_size; i++) {
1256 if (repo->packidx_cache[i] == NULL)
1258 if (strcmp(repo->packidx_cache[i]->path_packidx,
1259 path_packidx) == 0) {
1260 *packidx = repo->packidx_cache[i];
1264 /* No luck. Search the filesystem. */
1266 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1271 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1275 err = cache_packidx(repo, *packidx, path_packidx);
1278 got_packidx_close(*packidx);
1284 static const struct got_error *
1285 read_packfile_hdr(int fd, struct got_packidx *packidx)
1287 const struct got_error *err = NULL;
1288 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1289 struct got_packfile_hdr hdr;
1292 n = read(fd, &hdr, sizeof(hdr));
1294 return got_error_from_errno("read");
1295 if (n != sizeof(hdr))
1296 return got_error(GOT_ERR_BAD_PACKFILE);
1298 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1299 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1300 be32toh(hdr.nobjects) != totobj)
1301 err = got_error(GOT_ERR_BAD_PACKFILE);
1306 static const struct got_error *
1307 open_packfile(int *fd, struct got_repository *repo,
1308 const char *relpath, struct got_packidx *packidx)
1310 const struct got_error *err = NULL;
1312 *fd = openat(got_repo_get_fd(repo), relpath,
1313 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1315 return got_error_from_errno_fmt("openat: %s/%s",
1316 got_repo_get_path_git_dir(repo), relpath);
1319 err = read_packfile_hdr(*fd, packidx);
1329 const struct got_error *
1330 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1331 const char *path_packfile, struct got_packidx *packidx)
1333 const struct got_error *err = NULL;
1334 struct got_pack *pack = NULL;
1341 for (i = 0; i < repo->pack_cache_size; i++) {
1342 pack = &repo->packs[i];
1343 if (pack->path_packfile == NULL)
1345 if (strcmp(pack->path_packfile, path_packfile) == 0)
1346 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1349 if (i == repo->pack_cache_size) {
1350 err = got_pack_close(&repo->packs[i - 1]);
1353 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1354 return got_error_from_errno("ftruncate");
1355 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1356 return got_error_from_errno("ftruncate");
1357 memmove(&repo->packs[1], &repo->packs[0],
1358 sizeof(repo->packs) - sizeof(repo->packs[0]));
1362 pack = &repo->packs[i];
1364 pack->path_packfile = strdup(path_packfile);
1365 if (pack->path_packfile == NULL) {
1366 err = got_error_from_errno("strdup");
1370 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1374 if (fstat(pack->fd, &sb) != 0) {
1375 err = got_error_from_errno("fstat");
1378 pack->filesize = sb.st_size;
1380 pack->privsep_child = NULL;
1382 #ifndef GOT_PACK_NO_MMAP
1383 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1385 if (pack->map == MAP_FAILED) {
1386 if (errno != ENOMEM) {
1387 err = got_error_from_errno("mmap");
1390 pack->map = NULL; /* fall back to read(2) */
1396 free(pack->path_packfile);
1397 memset(pack, 0, sizeof(*pack));
1405 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1407 struct got_pack *pack = NULL;
1410 for (i = 0; i < repo->pack_cache_size; i++) {
1411 pack = &repo->packs[i];
1412 if (pack->path_packfile == NULL)
1414 if (strcmp(pack->path_packfile, path_packfile) == 0)
1421 const struct got_error *
1422 got_repo_init(const char *repo_path)
1424 const struct got_error *err = NULL;
1425 const char *dirnames[] = {
1427 GOT_OBJECTS_PACK_DIR,
1430 const char *description_str = "Unnamed repository; "
1431 "edit this file 'description' to name the repository.";
1432 const char *headref_str = "ref: refs/heads/main";
1433 const char *gitconfig_str = "[core]\n"
1434 "\trepositoryformatversion = 0\n"
1435 "\tfilemode = true\n"
1440 if (!got_path_dir_is_empty(repo_path))
1441 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1443 for (i = 0; i < nitems(dirnames); i++) {
1444 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1445 return got_error_from_errno("asprintf");
1447 err = got_path_mkdir(path);
1453 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1454 return got_error_from_errno("asprintf");
1455 err = got_path_create_file(path, description_str);
1460 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1461 return got_error_from_errno("asprintf");
1462 err = got_path_create_file(path, headref_str);
1467 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1468 return got_error_from_errno("asprintf");
1469 err = got_path_create_file(path, gitconfig_str);
1477 static const struct got_error *
1478 match_packed_object(struct got_object_id **unique_id,
1479 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1481 const struct got_error *err = NULL;
1482 struct got_object_id_queue matched_ids;
1483 struct got_pathlist_entry *pe;
1485 STAILQ_INIT(&matched_ids);
1487 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1488 const char *path_packidx = pe->path;
1489 struct got_packidx *packidx;
1490 struct got_object_qid *qid;
1492 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1497 err = got_packidx_match_id_str_prefix(&matched_ids,
1498 packidx, id_str_prefix);
1500 got_packidx_close(packidx);
1503 err = got_packidx_close(packidx);
1507 STAILQ_FOREACH(qid, &matched_ids, entry) {
1508 if (obj_type != GOT_OBJ_TYPE_ANY) {
1510 err = got_object_get_type(&matched_type, repo,
1514 if (matched_type != obj_type)
1517 if (*unique_id == NULL) {
1518 *unique_id = got_object_id_dup(&qid->id);
1519 if (*unique_id == NULL) {
1520 err = got_error_from_errno("malloc");
1524 if (got_object_id_cmp(*unique_id,
1526 continue; /* packed multiple times */
1527 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1533 got_object_id_queue_free(&matched_ids);
1541 static const struct got_error *
1542 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1543 const char *object_dir, const char *id_str_prefix, int obj_type,
1544 struct got_repository *repo)
1546 const struct got_error *err = NULL;
1549 struct dirent *dent;
1550 struct got_object_id id;
1552 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1553 err = got_error_from_errno("asprintf");
1557 dir = opendir(path);
1559 if (errno == ENOENT) {
1563 err = got_error_from_errno2("opendir", path);
1566 while ((dent = readdir(dir)) != NULL) {
1570 if (strcmp(dent->d_name, ".") == 0 ||
1571 strcmp(dent->d_name, "..") == 0)
1574 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1575 err = got_error_from_errno("asprintf");
1579 if (!got_parse_sha1_digest(id.sha1, id_str))
1583 * Directory entries do not necessarily appear in
1584 * sorted order, so we must iterate over all of them.
1586 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1592 if (*unique_id == NULL) {
1593 if (obj_type != GOT_OBJ_TYPE_ANY) {
1595 err = got_object_get_type(&matched_type, repo,
1599 if (matched_type != obj_type)
1602 *unique_id = got_object_id_dup(&id);
1603 if (*unique_id == NULL) {
1604 err = got_error_from_errno("got_object_id_dup");
1609 if (got_object_id_cmp(*unique_id, &id) == 0)
1610 continue; /* both packed and loose */
1611 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1617 if (dir && closedir(dir) != 0 && err == NULL)
1618 err = got_error_from_errno("closedir");
1627 const struct got_error *
1628 got_repo_match_object_id_prefix(struct got_object_id **id,
1629 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1631 const struct got_error *err = NULL;
1632 char *path_objects = got_repo_get_path_objects(repo);
1633 char *object_dir = NULL;
1639 len = strlen(id_str_prefix);
1640 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1641 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1643 for (i = 0; i < len; i++) {
1644 if (isxdigit((unsigned char)id_str_prefix[i]))
1646 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1650 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1653 object_dir = strndup(id_str_prefix, 2);
1654 if (object_dir == NULL) {
1655 err = got_error_from_errno("strdup");
1658 err = match_loose_object(id, path_objects, object_dir,
1659 id_str_prefix, obj_type, repo);
1660 } else if (len == 1) {
1662 for (i = 0; i < 0xf; i++) {
1663 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1665 err = got_error_from_errno("asprintf");
1668 err = match_packed_object(id, repo, object_dir,
1672 err = match_loose_object(id, path_objects, object_dir,
1673 id_str_prefix, obj_type, repo);
1678 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1686 } else if (*id == NULL) {
1688 case GOT_OBJ_TYPE_BLOB:
1689 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1690 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1692 case GOT_OBJ_TYPE_TREE:
1693 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1694 GOT_OBJ_LABEL_TREE, id_str_prefix);
1696 case GOT_OBJ_TYPE_COMMIT:
1697 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1698 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1700 case GOT_OBJ_TYPE_TAG:
1701 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1702 GOT_OBJ_LABEL_TAG, id_str_prefix);
1705 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1713 const struct got_error *
1714 got_repo_match_object_id(struct got_object_id **id, char **label,
1715 const char *id_str, int obj_type, struct got_reflist_head *refs,
1716 struct got_repository *repo)
1718 const struct got_error *err;
1719 struct got_tag_object *tag;
1720 struct got_reference *ref = NULL;
1727 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1730 *id = got_object_id_dup(
1731 got_object_tag_get_object_id(tag));
1733 err = got_error_from_errno("got_object_id_dup");
1734 else if (label && asprintf(label, "refs/tags/%s",
1735 got_object_tag_get_name(tag)) == -1) {
1736 err = got_error_from_errno("asprintf");
1740 got_object_tag_close(tag);
1742 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1743 err->code != GOT_ERR_NO_OBJ)
1747 err = got_ref_open(&ref, repo, id_str, 0);
1749 err = got_ref_resolve(id, repo, ref);
1753 *label = strdup(got_ref_get_name(ref));
1754 if (*label == NULL) {
1755 err = got_error_from_errno("strdup");
1760 if (err->code != GOT_ERR_NOT_REF &&
1761 err->code != GOT_ERR_BAD_REF_NAME)
1763 err = got_repo_match_object_id_prefix(id, id_str,
1766 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1767 err = got_error_not_ref(id_str);
1771 err = got_object_id_str(label, *id);
1772 if (*label == NULL) {
1773 err = got_error_from_errno("strdup");
1784 const struct got_error *
1785 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1786 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1788 const struct got_error *err = NULL;
1789 struct got_reflist_entry *re;
1790 struct got_object_id *tag_id;
1791 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1795 TAILQ_FOREACH(re, refs, entry) {
1796 const char *refname;
1797 refname = got_ref_get_name(re->ref);
1798 if (got_ref_is_symbolic(re->ref))
1800 if (strncmp(refname, "refs/tags/", 10) != 0)
1802 if (!name_is_absolute)
1803 refname += strlen("refs/tags/");
1804 if (strcmp(refname, name) != 0)
1806 err = got_ref_resolve(&tag_id, repo, re->ref);
1809 err = got_object_open_as_tag(tag, repo, tag_id);
1813 if (obj_type == GOT_OBJ_TYPE_ANY ||
1814 got_object_tag_get_object_type(*tag) == obj_type)
1816 got_object_tag_close(*tag);
1820 if (err == NULL && *tag == NULL)
1821 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1822 GOT_OBJ_LABEL_TAG, name);
1826 static const struct got_error *
1827 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1828 const char *name, mode_t mode, struct got_object_id *blob_id)
1830 const struct got_error *err = NULL;
1834 *new_te = calloc(1, sizeof(**new_te));
1835 if (*new_te == NULL)
1836 return got_error_from_errno("calloc");
1838 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1839 sizeof((*new_te)->name)) {
1840 err = got_error(GOT_ERR_NO_SPACE);
1844 if (S_ISLNK(mode)) {
1845 (*new_te)->mode = S_IFLNK;
1847 (*new_te)->mode = S_IFREG;
1848 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1850 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1852 if (err && *new_te) {
1859 static const struct got_error *
1860 import_file(struct got_tree_entry **new_te, struct dirent *de,
1861 const char *path, struct got_repository *repo)
1863 const struct got_error *err;
1864 struct got_object_id *blob_id = NULL;
1868 if (asprintf(&filepath, "%s%s%s", path,
1869 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1870 return got_error_from_errno("asprintf");
1872 if (lstat(filepath, &sb) != 0) {
1873 err = got_error_from_errno2("lstat", path);
1877 err = got_object_blob_create(&blob_id, filepath, repo);
1881 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1890 static const struct got_error *
1891 insert_tree_entry(struct got_tree_entry *new_te,
1892 struct got_pathlist_head *paths)
1894 const struct got_error *err = NULL;
1895 struct got_pathlist_entry *new_pe;
1897 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1901 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1905 static const struct got_error *write_tree(struct got_object_id **,
1906 const char *, struct got_pathlist_head *, struct got_repository *,
1907 got_repo_import_cb progress_cb, void *progress_arg);
1909 static const struct got_error *
1910 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1911 const char *path, struct got_pathlist_head *ignores,
1912 struct got_repository *repo,
1913 got_repo_import_cb progress_cb, void *progress_arg)
1915 const struct got_error *err;
1916 struct got_object_id *id = NULL;
1919 if (asprintf(&subdirpath, "%s%s%s", path,
1920 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1921 return got_error_from_errno("asprintf");
1923 (*new_te) = calloc(1, sizeof(**new_te));
1924 if (*new_te == NULL)
1925 return got_error_from_errno("calloc");
1926 (*new_te)->mode = S_IFDIR;
1927 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1928 sizeof((*new_te)->name)) {
1929 err = got_error(GOT_ERR_NO_SPACE);
1932 err = write_tree(&id, subdirpath, ignores, repo,
1933 progress_cb, progress_arg);
1936 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1948 static const struct got_error *
1949 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1950 struct got_pathlist_head *ignores, struct got_repository *repo,
1951 got_repo_import_cb progress_cb, void *progress_arg)
1953 const struct got_error *err = NULL;
1957 struct got_tree_entry *new_te = NULL;
1958 struct got_pathlist_head paths;
1959 struct got_pathlist_entry *pe;
1961 *new_tree_id = NULL;
1965 dir = opendir(path_dir);
1967 err = got_error_from_errno2("opendir", path_dir);
1972 while ((de = readdir(dir)) != NULL) {
1976 if (strcmp(de->d_name, ".") == 0 ||
1977 strcmp(de->d_name, "..") == 0)
1980 TAILQ_FOREACH(pe, ignores, entry) {
1981 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1989 err = got_path_dirent_type(&type, path_dir, de);
1993 if (type == DT_DIR) {
1994 err = import_subdir(&new_te, de, path_dir,
1995 ignores, repo, progress_cb, progress_arg);
1997 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2002 } else if (type == DT_REG || type == DT_LNK) {
2003 err = import_file(&new_te, de, path_dir, repo);
2009 err = insert_tree_entry(new_te, &paths);
2015 if (TAILQ_EMPTY(&paths)) {
2016 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2017 "cannot create tree without any entries");
2021 TAILQ_FOREACH(pe, &paths, entry) {
2022 struct got_tree_entry *te = pe->data;
2024 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2026 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2027 err = got_error_from_errno("asprintf");
2030 err = (*progress_cb)(progress_arg, path);
2036 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2040 got_pathlist_free(&paths);
2044 const struct got_error *
2045 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2046 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2047 struct got_repository *repo, got_repo_import_cb progress_cb,
2050 const struct got_error *err;
2051 struct got_object_id *new_tree_id;
2053 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2054 progress_cb, progress_arg);
2058 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2059 author, time(NULL), author, time(NULL), logmsg, repo);
2064 const struct got_error *
2065 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2066 struct got_repository *repo)
2068 const struct got_error *err = NULL;
2069 char *path_objects = NULL, *path = NULL;
2071 struct got_object_id id;
2077 path_objects = got_repo_get_path_objects(repo);
2078 if (path_objects == NULL)
2079 return got_error_from_errno("got_repo_get_path_objects");
2081 for (i = 0; i <= 0xff; i++) {
2082 struct dirent *dent;
2084 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2085 err = got_error_from_errno("asprintf");
2089 dir = opendir(path);
2091 if (errno == ENOENT) {
2095 err = got_error_from_errno2("opendir", path);
2099 while ((dent = readdir(dir)) != NULL) {
2104 if (strcmp(dent->d_name, ".") == 0 ||
2105 strcmp(dent->d_name, "..") == 0)
2108 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2109 err = got_error_from_errno("asprintf");
2113 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2119 err = got_object_open_loose_fd(&fd, &id, repo);
2123 if (fstat(fd, &sb) == -1) {
2124 err = got_error_from_errno("fstat");
2129 (*ondisk_size) += sb.st_size;
2131 if (close(fd) == -1) {
2132 err = got_error_from_errno("close");
2137 if (closedir(dir) != 0) {
2138 err = got_error_from_errno("closedir");
2147 if (dir && closedir(dir) != 0 && err == NULL)
2148 err = got_error_from_errno("closedir");
2159 const struct got_error *
2160 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2161 off_t *total_packsize, struct got_repository *repo)
2163 const struct got_error *err = NULL;
2164 DIR *packdir = NULL;
2165 struct dirent *dent;
2166 struct got_packidx *packidx = NULL;
2168 char *path_packfile;
2174 *total_packsize = 0;
2176 packdir_fd = openat(got_repo_get_fd(repo),
2177 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2178 if (packdir_fd == -1) {
2179 return got_error_from_errno_fmt("openat: %s/%s",
2180 got_repo_get_path_git_dir(repo),
2181 GOT_OBJECTS_PACK_DIR);
2184 packdir = fdopendir(packdir_fd);
2185 if (packdir == NULL) {
2186 err = got_error_from_errno("fdopendir");
2190 while ((dent = readdir(packdir)) != NULL) {
2191 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2194 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2195 dent->d_name) == -1) {
2196 err = got_error_from_errno("asprintf");
2200 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2206 if (fstat(packidx->fd, &sb) == -1)
2208 *total_packsize += sb.st_size;
2210 err = got_packidx_get_packfile_path(&path_packfile,
2211 packidx->path_packidx);
2215 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2217 free(path_packfile);
2220 free(path_packfile);
2221 *total_packsize += sb.st_size;
2223 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2227 got_packidx_close(packidx);
2232 got_packidx_close(packidx);
2233 if (packdir && closedir(packdir) != 0 && err == NULL)
2234 err = got_error_from_errno("closedir");
2238 *total_packsize = 0;
2243 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2244 got_packidx_bloom_filter_cmp);