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)
801 got_pack_close(&repo->packs[i]);
802 if (repo->packs[i].basefd != -1) {
803 if (close(repo->packs[i].basefd) == -1 && err == NULL)
804 err = got_error_from_errno("close");
805 repo->packs[i].basefd = -1;
807 if (repo->packs[i].accumfd != -1) {
808 if (close(repo->packs[i].accumfd) == -1 && err == NULL)
809 err = got_error_from_errno("close");
810 repo->packs[i].accumfd = -1;
815 free(repo->path_git_dir);
817 got_object_cache_close(&repo->objcache);
818 got_object_cache_close(&repo->treecache);
819 got_object_cache_close(&repo->commitcache);
820 got_object_cache_close(&repo->tagcache);
821 got_object_cache_close(&repo->rawcache);
823 for (i = 0; i < nitems(repo->privsep_children); i++) {
824 if (repo->privsep_children[i].imsg_fd == -1)
826 imsg_clear(repo->privsep_children[i].ibuf);
827 free(repo->privsep_children[i].ibuf);
828 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
829 child_err = got_privsep_wait_for_child(
830 repo->privsep_children[i].pid);
831 if (child_err && err == NULL)
833 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
835 err = got_error_from_errno("close");
838 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
840 err = got_error_from_errno("close");
843 got_gotconfig_free(repo->gotconfig);
844 free(repo->gitconfig_author_name);
845 free(repo->gitconfig_author_email);
846 for (i = 0; i < repo->ngitconfig_remotes; i++)
847 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
848 free(repo->gitconfig_remotes);
849 for (i = 0; i < repo->nextensions; i++)
850 free(repo->extensions[i]);
851 free(repo->extensions);
853 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
854 free((void *)pe->path);
855 got_pathlist_free(&repo->packidx_paths);
862 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
868 free(repo->fetch_url);
869 repo->fetch_url = NULL;
870 free(repo->send_url);
871 repo->send_url = NULL;
872 for (i = 0; i < repo->nfetch_branches; i++)
873 free(repo->fetch_branches[i]);
874 free(repo->fetch_branches);
875 repo->fetch_branches = NULL;
876 repo->nfetch_branches = 0;
877 for (i = 0; i < repo->nsend_branches; i++)
878 free(repo->send_branches[i]);
879 free(repo->send_branches);
880 repo->send_branches = NULL;
881 repo->nsend_branches = 0;
884 const struct got_error *
885 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
886 const char *input_path)
888 const struct got_error *err = NULL;
889 const char *repo_abspath = NULL;
891 char *canonpath, *path = NULL;
893 *in_repo_path = NULL;
895 canonpath = strdup(input_path);
896 if (canonpath == NULL) {
897 err = got_error_from_errno("strdup");
900 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
904 repo_abspath = got_repo_get_path(repo);
906 if (canonpath[0] == '\0') {
907 path = strdup(canonpath);
909 err = got_error_from_errno("strdup");
913 path = realpath(canonpath, NULL);
915 if (errno != ENOENT) {
916 err = got_error_from_errno2("realpath",
921 * Path is not on disk.
922 * Assume it is already relative to repository root.
924 path = strdup(canonpath);
926 err = got_error_from_errno("strdup");
931 repolen = strlen(repo_abspath);
935 if (strcmp(path, repo_abspath) == 0) {
939 err = got_error_from_errno("strdup");
942 } else if (len > repolen &&
943 got_path_is_child(path, repo_abspath, repolen)) {
944 /* Matched an on-disk path inside repository. */
945 if (got_repo_is_bare(repo)) {
947 * Matched an on-disk path inside repository
948 * database. Treat input as repository-relative.
955 /* Strip common prefix with repository path. */
956 err = got_path_skip_common_ancestor(&child,
965 * Matched unrelated on-disk path.
966 * Treat input as repository-relative.
974 /* Make in-repository path absolute */
975 if (path[0] != '/') {
977 if (asprintf(&abspath, "/%s", path) == -1) {
978 err = got_error_from_errno("asprintf");
990 *in_repo_path = path;
994 static const struct got_error *
995 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
996 const char *path_packidx)
998 const struct got_error *err = NULL;
1001 for (i = 0; i < repo->pack_cache_size; i++) {
1002 if (repo->packidx_cache[i] == NULL)
1004 if (strcmp(repo->packidx_cache[i]->path_packidx,
1005 path_packidx) == 0) {
1006 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1009 if (i == repo->pack_cache_size) {
1010 i = repo->pack_cache_size - 1;
1011 err = got_packidx_close(repo->packidx_cache[i]);
1016 repo->packidx_cache[i] = packidx;
1022 got_repo_is_packidx_filename(const char *name, size_t len)
1024 if (len != GOT_PACKIDX_NAMELEN)
1027 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1030 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1031 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1037 static struct got_packidx_bloom_filter *
1038 get_packidx_bloom_filter(struct got_repository *repo,
1039 const char *path, size_t path_len)
1041 struct got_packidx_bloom_filter key;
1043 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1044 return NULL; /* XXX */
1045 key.path_len = path_len;
1047 return RB_FIND(got_packidx_bloom_filter_tree,
1048 &repo->packidx_bloom_filters, &key);
1052 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1053 const char *path_packidx, struct got_object_id *id)
1055 struct got_packidx_bloom_filter *bf;
1057 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1059 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1061 /* No bloom filter means this pack index must be searched. */
1065 static const struct got_error *
1066 add_packidx_bloom_filter(struct got_repository *repo,
1067 struct got_packidx *packidx, const char *path_packidx)
1069 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1070 struct got_packidx_bloom_filter *bf;
1074 * Don't use bloom filters for very large pack index files.
1075 * Large pack files will contain a relatively large fraction
1076 * of our objects so we will likely need to visit them anyway.
1077 * The more objects a pack file contains the higher the probability
1078 * of a false-positive match from the bloom filter. And reading
1079 * all object IDs from a large pack index file can be expensive.
1081 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1084 /* Do we already have a filter for this pack index? */
1085 if (get_packidx_bloom_filter(repo, path_packidx,
1086 strlen(path_packidx)) != NULL)
1089 bf = calloc(1, sizeof(*bf));
1091 return got_error_from_errno("calloc");
1092 bf->bloom = calloc(1, sizeof(*bf->bloom));
1093 if (bf->bloom == NULL) {
1095 return got_error_from_errno("calloc");
1098 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1099 if (len >= sizeof(bf->path)) {
1102 return got_error(GOT_ERR_NO_SPACE);
1106 /* Minimum size supported by our bloom filter is 1000 entries. */
1107 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1108 for (i = 0; i < nobjects; i++) {
1109 struct got_packidx_object_id *id;
1110 id = &packidx->hdr.sorted_ids[i];
1111 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1114 RB_INSERT(got_packidx_bloom_filter_tree,
1115 &repo->packidx_bloom_filters, bf);
1119 const struct got_error *
1120 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1121 struct got_repository *repo, struct got_object_id *id)
1123 const struct got_error *err;
1124 struct got_pathlist_entry *pe;
1127 /* Search pack index cache. */
1128 for (i = 0; i < repo->pack_cache_size; i++) {
1129 if (repo->packidx_cache[i] == NULL)
1131 if (!got_repo_check_packidx_bloom_filter(repo,
1132 repo->packidx_cache[i]->path_packidx, id))
1133 continue; /* object will not be found in this index */
1134 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1136 *packidx = repo->packidx_cache[i];
1138 * Move this cache entry to the front. Repeatedly
1139 * searching a wrong pack index can be expensive.
1142 memmove(&repo->packidx_cache[1],
1143 &repo->packidx_cache[0],
1144 i * sizeof(repo->packidx_cache[0]));
1145 repo->packidx_cache[0] = *packidx;
1150 /* No luck. Search the filesystem. */
1152 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1153 const char *path_packidx = pe->path;
1156 if (!got_repo_check_packidx_bloom_filter(repo,
1158 continue; /* object will not be found in this index */
1160 for (i = 0; i < repo->pack_cache_size; i++) {
1161 if (repo->packidx_cache[i] == NULL)
1163 if (strcmp(repo->packidx_cache[i]->path_packidx,
1164 path_packidx) == 0) {
1170 continue; /* already searched */
1172 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1177 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1181 err = cache_packidx(repo, *packidx, path_packidx);
1185 *idx = got_packidx_get_object_idx(*packidx, id);
1187 err = NULL; /* found the object */
1192 err = got_error_no_obj(id);
1197 const struct got_error *
1198 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1199 struct got_repository *repo)
1201 const struct got_error *err = NULL;
1202 DIR *packdir = NULL;
1203 struct dirent *dent;
1204 char *path_packidx = NULL;
1207 packdir_fd = openat(got_repo_get_fd(repo),
1208 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1209 if (packdir_fd == -1) {
1210 return got_error_from_errno_fmt("openat: %s/%s",
1211 got_repo_get_path_git_dir(repo),
1212 GOT_OBJECTS_PACK_DIR);
1215 packdir = fdopendir(packdir_fd);
1216 if (packdir == NULL) {
1217 err = got_error_from_errno("fdopendir");
1221 while ((dent = readdir(packdir)) != NULL) {
1222 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1225 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1226 dent->d_name) == -1) {
1227 err = got_error_from_errno("asprintf");
1228 path_packidx = NULL;
1232 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1239 if (packdir && closedir(packdir) != 0 && err == NULL)
1240 err = got_error_from_errno("closedir");
1244 const struct got_error *
1245 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1246 struct got_repository *repo)
1248 const struct got_error *err;
1253 /* Search pack index cache. */
1254 for (i = 0; i < repo->pack_cache_size; i++) {
1255 if (repo->packidx_cache[i] == NULL)
1257 if (strcmp(repo->packidx_cache[i]->path_packidx,
1258 path_packidx) == 0) {
1259 *packidx = repo->packidx_cache[i];
1263 /* No luck. Search the filesystem. */
1265 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1270 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1274 err = cache_packidx(repo, *packidx, path_packidx);
1277 got_packidx_close(*packidx);
1283 static const struct got_error *
1284 read_packfile_hdr(int fd, struct got_packidx *packidx)
1286 const struct got_error *err = NULL;
1287 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1288 struct got_packfile_hdr hdr;
1291 n = read(fd, &hdr, sizeof(hdr));
1293 return got_error_from_errno("read");
1294 if (n != sizeof(hdr))
1295 return got_error(GOT_ERR_BAD_PACKFILE);
1297 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1298 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1299 be32toh(hdr.nobjects) != totobj)
1300 err = got_error(GOT_ERR_BAD_PACKFILE);
1305 static const struct got_error *
1306 open_packfile(int *fd, struct got_repository *repo,
1307 const char *relpath, struct got_packidx *packidx)
1309 const struct got_error *err = NULL;
1311 *fd = openat(got_repo_get_fd(repo), relpath,
1312 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1314 return got_error_from_errno_fmt("openat: %s/%s",
1315 got_repo_get_path_git_dir(repo), relpath);
1318 err = read_packfile_hdr(*fd, packidx);
1328 const struct got_error *
1329 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1330 const char *path_packfile, struct got_packidx *packidx)
1332 const struct got_error *err = NULL;
1333 struct got_pack *pack = NULL;
1340 for (i = 0; i < repo->pack_cache_size; i++) {
1341 pack = &repo->packs[i];
1342 if (pack->path_packfile == NULL)
1344 if (strcmp(pack->path_packfile, path_packfile) == 0)
1345 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1348 if (i == repo->pack_cache_size) {
1349 struct got_pack tmp;
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 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1358 memcpy(&repo->packs[i - 1], &repo->packs[0],
1359 sizeof(repo->packs[i - 1]));
1360 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1364 pack = &repo->packs[i];
1366 pack->path_packfile = strdup(path_packfile);
1367 if (pack->path_packfile == NULL) {
1368 err = got_error_from_errno("strdup");
1372 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1376 if (fstat(pack->fd, &sb) != 0) {
1377 err = got_error_from_errno("fstat");
1380 pack->filesize = sb.st_size;
1382 pack->privsep_child = NULL;
1384 #ifndef GOT_PACK_NO_MMAP
1385 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1387 if (pack->map == MAP_FAILED) {
1388 if (errno != ENOMEM) {
1389 err = got_error_from_errno("mmap");
1392 pack->map = NULL; /* fall back to read(2) */
1398 free(pack->path_packfile);
1399 memset(pack, 0, sizeof(*pack));
1407 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1409 struct got_pack *pack = NULL;
1412 for (i = 0; i < repo->pack_cache_size; i++) {
1413 pack = &repo->packs[i];
1414 if (pack->path_packfile == NULL)
1416 if (strcmp(pack->path_packfile, path_packfile) == 0)
1423 const struct got_error *
1424 got_repo_init(const char *repo_path)
1426 const struct got_error *err = NULL;
1427 const char *dirnames[] = {
1429 GOT_OBJECTS_PACK_DIR,
1432 const char *description_str = "Unnamed repository; "
1433 "edit this file 'description' to name the repository.";
1434 const char *headref_str = "ref: refs/heads/main";
1435 const char *gitconfig_str = "[core]\n"
1436 "\trepositoryformatversion = 0\n"
1437 "\tfilemode = true\n"
1442 if (!got_path_dir_is_empty(repo_path))
1443 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1445 for (i = 0; i < nitems(dirnames); i++) {
1446 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1447 return got_error_from_errno("asprintf");
1449 err = got_path_mkdir(path);
1455 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1456 return got_error_from_errno("asprintf");
1457 err = got_path_create_file(path, description_str);
1462 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1463 return got_error_from_errno("asprintf");
1464 err = got_path_create_file(path, headref_str);
1469 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1470 return got_error_from_errno("asprintf");
1471 err = got_path_create_file(path, gitconfig_str);
1479 static const struct got_error *
1480 match_packed_object(struct got_object_id **unique_id,
1481 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1483 const struct got_error *err = NULL;
1484 struct got_object_id_queue matched_ids;
1485 struct got_pathlist_entry *pe;
1487 STAILQ_INIT(&matched_ids);
1489 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1490 const char *path_packidx = pe->path;
1491 struct got_packidx *packidx;
1492 struct got_object_qid *qid;
1494 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1499 err = got_packidx_match_id_str_prefix(&matched_ids,
1500 packidx, id_str_prefix);
1502 got_packidx_close(packidx);
1505 err = got_packidx_close(packidx);
1509 STAILQ_FOREACH(qid, &matched_ids, entry) {
1510 if (obj_type != GOT_OBJ_TYPE_ANY) {
1512 err = got_object_get_type(&matched_type, repo,
1516 if (matched_type != obj_type)
1519 if (*unique_id == NULL) {
1520 *unique_id = got_object_id_dup(&qid->id);
1521 if (*unique_id == NULL) {
1522 err = got_error_from_errno("malloc");
1526 if (got_object_id_cmp(*unique_id,
1528 continue; /* packed multiple times */
1529 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1535 got_object_id_queue_free(&matched_ids);
1543 static const struct got_error *
1544 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1545 const char *object_dir, const char *id_str_prefix, int obj_type,
1546 struct got_repository *repo)
1548 const struct got_error *err = NULL;
1551 struct dirent *dent;
1552 struct got_object_id id;
1554 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1555 err = got_error_from_errno("asprintf");
1559 dir = opendir(path);
1561 if (errno == ENOENT) {
1565 err = got_error_from_errno2("opendir", path);
1568 while ((dent = readdir(dir)) != NULL) {
1572 if (strcmp(dent->d_name, ".") == 0 ||
1573 strcmp(dent->d_name, "..") == 0)
1576 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1577 err = got_error_from_errno("asprintf");
1581 if (!got_parse_sha1_digest(id.sha1, id_str))
1585 * Directory entries do not necessarily appear in
1586 * sorted order, so we must iterate over all of them.
1588 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1594 if (*unique_id == NULL) {
1595 if (obj_type != GOT_OBJ_TYPE_ANY) {
1597 err = got_object_get_type(&matched_type, repo,
1601 if (matched_type != obj_type)
1604 *unique_id = got_object_id_dup(&id);
1605 if (*unique_id == NULL) {
1606 err = got_error_from_errno("got_object_id_dup");
1611 if (got_object_id_cmp(*unique_id, &id) == 0)
1612 continue; /* both packed and loose */
1613 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1619 if (dir && closedir(dir) != 0 && err == NULL)
1620 err = got_error_from_errno("closedir");
1629 const struct got_error *
1630 got_repo_match_object_id_prefix(struct got_object_id **id,
1631 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 char *path_objects = got_repo_get_path_objects(repo);
1635 char *object_dir = NULL;
1641 len = strlen(id_str_prefix);
1642 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1643 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1645 for (i = 0; i < len; i++) {
1646 if (isxdigit((unsigned char)id_str_prefix[i]))
1648 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1652 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1655 object_dir = strndup(id_str_prefix, 2);
1656 if (object_dir == NULL) {
1657 err = got_error_from_errno("strdup");
1660 err = match_loose_object(id, path_objects, object_dir,
1661 id_str_prefix, obj_type, repo);
1662 } else if (len == 1) {
1664 for (i = 0; i < 0xf; i++) {
1665 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1667 err = got_error_from_errno("asprintf");
1670 err = match_packed_object(id, repo, object_dir,
1674 err = match_loose_object(id, path_objects, object_dir,
1675 id_str_prefix, obj_type, repo);
1680 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1688 } else if (*id == NULL) {
1690 case GOT_OBJ_TYPE_BLOB:
1691 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1692 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1694 case GOT_OBJ_TYPE_TREE:
1695 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1696 GOT_OBJ_LABEL_TREE, id_str_prefix);
1698 case GOT_OBJ_TYPE_COMMIT:
1699 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1700 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1702 case GOT_OBJ_TYPE_TAG:
1703 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1704 GOT_OBJ_LABEL_TAG, id_str_prefix);
1707 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1715 const struct got_error *
1716 got_repo_match_object_id(struct got_object_id **id, char **label,
1717 const char *id_str, int obj_type, struct got_reflist_head *refs,
1718 struct got_repository *repo)
1720 const struct got_error *err;
1721 struct got_tag_object *tag;
1722 struct got_reference *ref = NULL;
1729 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1732 *id = got_object_id_dup(
1733 got_object_tag_get_object_id(tag));
1735 err = got_error_from_errno("got_object_id_dup");
1736 else if (label && asprintf(label, "refs/tags/%s",
1737 got_object_tag_get_name(tag)) == -1) {
1738 err = got_error_from_errno("asprintf");
1742 got_object_tag_close(tag);
1744 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1745 err->code != GOT_ERR_NO_OBJ)
1749 err = got_ref_open(&ref, repo, id_str, 0);
1751 err = got_ref_resolve(id, repo, ref);
1755 *label = strdup(got_ref_get_name(ref));
1756 if (*label == NULL) {
1757 err = got_error_from_errno("strdup");
1762 if (err->code != GOT_ERR_NOT_REF &&
1763 err->code != GOT_ERR_BAD_REF_NAME)
1765 err = got_repo_match_object_id_prefix(id, id_str,
1768 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1769 err = got_error_not_ref(id_str);
1773 err = got_object_id_str(label, *id);
1774 if (*label == NULL) {
1775 err = got_error_from_errno("strdup");
1786 const struct got_error *
1787 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1788 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1790 const struct got_error *err = NULL;
1791 struct got_reflist_entry *re;
1792 struct got_object_id *tag_id;
1793 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1797 TAILQ_FOREACH(re, refs, entry) {
1798 const char *refname;
1799 refname = got_ref_get_name(re->ref);
1800 if (got_ref_is_symbolic(re->ref))
1802 if (strncmp(refname, "refs/tags/", 10) != 0)
1804 if (!name_is_absolute)
1805 refname += strlen("refs/tags/");
1806 if (strcmp(refname, name) != 0)
1808 err = got_ref_resolve(&tag_id, repo, re->ref);
1811 err = got_object_open_as_tag(tag, repo, tag_id);
1815 if (obj_type == GOT_OBJ_TYPE_ANY ||
1816 got_object_tag_get_object_type(*tag) == obj_type)
1818 got_object_tag_close(*tag);
1822 if (err == NULL && *tag == NULL)
1823 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1824 GOT_OBJ_LABEL_TAG, name);
1828 static const struct got_error *
1829 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1830 const char *name, mode_t mode, struct got_object_id *blob_id)
1832 const struct got_error *err = NULL;
1836 *new_te = calloc(1, sizeof(**new_te));
1837 if (*new_te == NULL)
1838 return got_error_from_errno("calloc");
1840 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1841 sizeof((*new_te)->name)) {
1842 err = got_error(GOT_ERR_NO_SPACE);
1846 if (S_ISLNK(mode)) {
1847 (*new_te)->mode = S_IFLNK;
1849 (*new_te)->mode = S_IFREG;
1850 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1852 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1854 if (err && *new_te) {
1861 static const struct got_error *
1862 import_file(struct got_tree_entry **new_te, struct dirent *de,
1863 const char *path, struct got_repository *repo)
1865 const struct got_error *err;
1866 struct got_object_id *blob_id = NULL;
1870 if (asprintf(&filepath, "%s%s%s", path,
1871 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1872 return got_error_from_errno("asprintf");
1874 if (lstat(filepath, &sb) != 0) {
1875 err = got_error_from_errno2("lstat", path);
1879 err = got_object_blob_create(&blob_id, filepath, repo);
1883 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1892 static const struct got_error *
1893 insert_tree_entry(struct got_tree_entry *new_te,
1894 struct got_pathlist_head *paths)
1896 const struct got_error *err = NULL;
1897 struct got_pathlist_entry *new_pe;
1899 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1903 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1907 static const struct got_error *write_tree(struct got_object_id **,
1908 const char *, struct got_pathlist_head *, struct got_repository *,
1909 got_repo_import_cb progress_cb, void *progress_arg);
1911 static const struct got_error *
1912 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1913 const char *path, struct got_pathlist_head *ignores,
1914 struct got_repository *repo,
1915 got_repo_import_cb progress_cb, void *progress_arg)
1917 const struct got_error *err;
1918 struct got_object_id *id = NULL;
1921 if (asprintf(&subdirpath, "%s%s%s", path,
1922 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1923 return got_error_from_errno("asprintf");
1925 (*new_te) = calloc(1, sizeof(**new_te));
1926 if (*new_te == NULL)
1927 return got_error_from_errno("calloc");
1928 (*new_te)->mode = S_IFDIR;
1929 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1930 sizeof((*new_te)->name)) {
1931 err = got_error(GOT_ERR_NO_SPACE);
1934 err = write_tree(&id, subdirpath, ignores, repo,
1935 progress_cb, progress_arg);
1938 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1950 static const struct got_error *
1951 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1952 struct got_pathlist_head *ignores, struct got_repository *repo,
1953 got_repo_import_cb progress_cb, void *progress_arg)
1955 const struct got_error *err = NULL;
1959 struct got_tree_entry *new_te = NULL;
1960 struct got_pathlist_head paths;
1961 struct got_pathlist_entry *pe;
1963 *new_tree_id = NULL;
1967 dir = opendir(path_dir);
1969 err = got_error_from_errno2("opendir", path_dir);
1974 while ((de = readdir(dir)) != NULL) {
1978 if (strcmp(de->d_name, ".") == 0 ||
1979 strcmp(de->d_name, "..") == 0)
1982 TAILQ_FOREACH(pe, ignores, entry) {
1983 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1991 err = got_path_dirent_type(&type, path_dir, de);
1995 if (type == DT_DIR) {
1996 err = import_subdir(&new_te, de, path_dir,
1997 ignores, repo, progress_cb, progress_arg);
1999 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2004 } else if (type == DT_REG || type == DT_LNK) {
2005 err = import_file(&new_te, de, path_dir, repo);
2011 err = insert_tree_entry(new_te, &paths);
2017 if (TAILQ_EMPTY(&paths)) {
2018 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2019 "cannot create tree without any entries");
2023 TAILQ_FOREACH(pe, &paths, entry) {
2024 struct got_tree_entry *te = pe->data;
2026 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2028 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2029 err = got_error_from_errno("asprintf");
2032 err = (*progress_cb)(progress_arg, path);
2038 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2042 got_pathlist_free(&paths);
2046 const struct got_error *
2047 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2048 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2049 struct got_repository *repo, got_repo_import_cb progress_cb,
2052 const struct got_error *err;
2053 struct got_object_id *new_tree_id;
2055 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2056 progress_cb, progress_arg);
2060 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2061 author, time(NULL), author, time(NULL), logmsg, repo);
2066 const struct got_error *
2067 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2068 struct got_repository *repo)
2070 const struct got_error *err = NULL;
2071 char *path_objects = NULL, *path = NULL;
2073 struct got_object_id id;
2079 path_objects = got_repo_get_path_objects(repo);
2080 if (path_objects == NULL)
2081 return got_error_from_errno("got_repo_get_path_objects");
2083 for (i = 0; i <= 0xff; i++) {
2084 struct dirent *dent;
2086 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2087 err = got_error_from_errno("asprintf");
2091 dir = opendir(path);
2093 if (errno == ENOENT) {
2097 err = got_error_from_errno2("opendir", path);
2101 while ((dent = readdir(dir)) != NULL) {
2106 if (strcmp(dent->d_name, ".") == 0 ||
2107 strcmp(dent->d_name, "..") == 0)
2110 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2111 err = got_error_from_errno("asprintf");
2115 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2121 err = got_object_open_loose_fd(&fd, &id, repo);
2125 if (fstat(fd, &sb) == -1) {
2126 err = got_error_from_errno("fstat");
2131 (*ondisk_size) += sb.st_size;
2133 if (close(fd) == -1) {
2134 err = got_error_from_errno("close");
2139 if (closedir(dir) != 0) {
2140 err = got_error_from_errno("closedir");
2149 if (dir && closedir(dir) != 0 && err == NULL)
2150 err = got_error_from_errno("closedir");
2161 const struct got_error *
2162 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2163 off_t *total_packsize, struct got_repository *repo)
2165 const struct got_error *err = NULL;
2166 DIR *packdir = NULL;
2167 struct dirent *dent;
2168 struct got_packidx *packidx = NULL;
2170 char *path_packfile;
2176 *total_packsize = 0;
2178 packdir_fd = openat(got_repo_get_fd(repo),
2179 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2180 if (packdir_fd == -1) {
2181 return got_error_from_errno_fmt("openat: %s/%s",
2182 got_repo_get_path_git_dir(repo),
2183 GOT_OBJECTS_PACK_DIR);
2186 packdir = fdopendir(packdir_fd);
2187 if (packdir == NULL) {
2188 err = got_error_from_errno("fdopendir");
2192 while ((dent = readdir(packdir)) != NULL) {
2193 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2196 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2197 dent->d_name) == -1) {
2198 err = got_error_from_errno("asprintf");
2202 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2208 if (fstat(packidx->fd, &sb) == -1)
2210 *total_packsize += sb.st_size;
2212 err = got_packidx_get_packfile_path(&path_packfile,
2213 packidx->path_packidx);
2217 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2219 free(path_packfile);
2222 free(path_packfile);
2223 *total_packsize += sb.st_size;
2225 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2229 got_packidx_close(packidx);
2234 got_packidx_close(packidx);
2235 if (packdir && closedir(packdir) != 0 && err == NULL)
2236 err = got_error_from_errno("closedir");
2240 *total_packsize = 0;
2245 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2246 got_packidx_bloom_filter_cmp);