2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
66 got_packidx_bloom_filter_cmp);
69 got_repo_get_path(struct got_repository *repo)
75 got_repo_get_path_git_dir(struct got_repository *repo)
77 return repo->path_git_dir;
81 got_repo_get_fd(struct got_repository *repo)
83 return repo->gitdir_fd;
87 got_repo_get_gitconfig_author_name(struct got_repository *repo)
89 return repo->gitconfig_author_name;
93 got_repo_get_gitconfig_author_email(struct got_repository *repo)
95 return repo->gitconfig_author_email;
99 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
101 return repo->global_gitconfig_author_name;
105 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
107 return repo->global_gitconfig_author_email;
111 got_repo_get_gitconfig_owner(struct got_repository *repo)
113 return repo->gitconfig_owner;
117 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
118 struct got_repository *repo)
120 *extensions = repo->extensions;
121 *nextensions = repo->nextensions;
125 got_repo_is_bare(struct got_repository *repo)
127 return (strcmp(repo->path, repo->path_git_dir) == 0);
131 get_path_git_child(struct got_repository *repo, const char *basename)
135 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
143 got_repo_get_path_objects(struct got_repository *repo)
145 return get_path_git_child(repo, GOT_OBJECTS_DIR);
149 got_repo_get_path_objects_pack(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
155 got_repo_get_path_refs(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_REFS_DIR);
161 got_repo_get_path_packed_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
167 get_path_head(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_HEAD_FILE);
173 got_repo_get_path_gitconfig(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_GITCONFIG);
179 got_repo_get_path_gotconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
184 const struct got_gotconfig *
185 got_repo_get_gotconfig(struct got_repository *repo)
187 return repo->gotconfig;
191 got_repo_get_gitconfig_remotes(int *nremotes,
192 const struct got_remote_repo **remotes, struct got_repository *repo)
194 *nremotes = repo->ngitconfig_remotes;
195 *remotes = repo->gitconfig_remotes;
199 is_git_repo(struct got_repository *repo)
201 const char *path_git = got_repo_get_path_git_dir(repo);
202 char *path_objects = got_repo_get_path_objects(repo);
203 char *path_refs = got_repo_get_path_refs(repo);
204 char *path_head = get_path_head(repo);
207 struct got_reference *head_ref;
209 if (lstat(path_git, &sb) == -1)
211 if (!S_ISDIR(sb.st_mode))
214 if (lstat(path_objects, &sb) == -1)
216 if (!S_ISDIR(sb.st_mode))
219 if (lstat(path_refs, &sb) == -1)
221 if (!S_ISDIR(sb.st_mode))
224 if (lstat(path_head, &sb) == -1)
226 if (!S_ISREG(sb.st_mode))
229 /* Check if the HEAD reference can be opened. */
230 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
232 got_ref_close(head_ref);
243 const struct got_error *
244 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
245 struct got_object *obj)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->objcache, id, obj);
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
262 got_repo_get_cached_object(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
268 const struct got_error *
269 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
270 struct got_tree_object *tree)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->treecache, id, tree);
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
286 struct got_tree_object *
287 got_repo_get_cached_tree(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_tree_object *)got_object_cache_get(
291 &repo->treecache, id);
294 const struct got_error *
295 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
296 struct got_commit_object *commit)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->commitcache, id, commit);
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
312 struct got_commit_object *
313 got_repo_get_cached_commit(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_commit_object *)got_object_cache_get(
317 &repo->commitcache, id);
320 const struct got_error *
321 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
322 struct got_tag_object *tag)
324 #ifndef GOT_NO_OBJ_CACHE
325 const struct got_error *err = NULL;
326 err = got_object_cache_add(&repo->tagcache, id, tag);
328 if (err->code == GOT_ERR_OBJ_EXISTS ||
329 err->code == GOT_ERR_OBJ_TOO_LARGE)
338 struct got_tag_object *
339 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
341 return (struct got_tag_object *)got_object_cache_get(
342 &repo->tagcache, id);
345 const struct got_error *
346 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
347 struct got_raw_object *raw)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->rawcache, id, raw);
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
364 struct got_raw_object *
365 got_repo_get_cached_raw_object(struct got_repository *repo,
366 struct got_object_id *id)
368 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
372 static const struct got_error *
373 open_repo(struct got_repository *repo, const char *path)
375 const struct got_error *err = NULL;
377 repo->gitdir_fd = -1;
379 /* bare git repository? */
380 repo->path_git_dir = strdup(path);
381 if (repo->path_git_dir == NULL)
382 return got_error_from_errno("strdup");
383 if (is_git_repo(repo)) {
384 repo->path = strdup(repo->path_git_dir);
385 if (repo->path == NULL) {
386 err = got_error_from_errno("strdup");
389 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
390 if (repo->gitdir_fd == -1) {
391 err = got_error_from_errno2("open",
398 /* git repository with working tree? */
399 free(repo->path_git_dir);
400 repo->path_git_dir = NULL;
401 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
402 err = got_error_from_errno("asprintf");
405 if (is_git_repo(repo)) {
406 repo->path = strdup(path);
407 if (repo->path == NULL) {
408 err = got_error_from_errno("strdup");
411 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
412 if (repo->gitdir_fd == -1) {
413 err = got_error_from_errno2("open",
420 err = got_error(GOT_ERR_NOT_GIT_REPO);
425 free(repo->path_git_dir);
426 repo->path_git_dir = NULL;
427 if (repo->gitdir_fd != -1)
428 close(repo->gitdir_fd);
429 repo->gitdir_fd = -1;
435 static const struct got_error *
436 parse_gitconfig_file(int *gitconfig_repository_format_version,
437 char **gitconfig_author_name, char **gitconfig_author_email,
438 struct got_remote_repo **remotes, int *nremotes,
439 char **gitconfig_owner, char ***extensions, int *nextensions,
440 const char *gitconfig_path)
442 const struct got_error *err = NULL, *child_err = NULL;
444 int imsg_fds[2] = { -1, -1 };
446 struct imsgbuf *ibuf;
448 *gitconfig_repository_format_version = 0;
453 *gitconfig_author_name = NULL;
454 *gitconfig_author_email = NULL;
460 *gitconfig_owner = NULL;
462 fd = open(gitconfig_path, O_RDONLY);
466 return got_error_from_errno2("open", gitconfig_path);
469 ibuf = calloc(1, sizeof(*ibuf));
471 err = got_error_from_errno("calloc");
475 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
476 err = got_error_from_errno("socketpair");
482 err = got_error_from_errno("fork");
484 } else if (pid == 0) {
485 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
490 if (close(imsg_fds[1]) == -1) {
491 err = got_error_from_errno("close");
495 imsg_init(ibuf, imsg_fds[0]);
497 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
502 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
506 err = got_privsep_recv_gitconfig_int(
507 gitconfig_repository_format_version, ibuf);
511 if (extensions && nextensions) {
512 err = got_privsep_send_gitconfig_repository_extensions_req(
516 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
519 if (*nextensions > 0) {
521 *extensions = calloc(*nextensions, sizeof(char *));
522 if (*extensions == NULL) {
523 err = got_error_from_errno("calloc");
526 for (i = 0; i < *nextensions; i++) {
528 err = got_privsep_recv_gitconfig_str(&ext,
532 (*extensions)[i] = ext;
537 err = got_privsep_send_gitconfig_author_name_req(ibuf);
541 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
545 err = got_privsep_send_gitconfig_author_email_req(ibuf);
549 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
553 if (remotes && nremotes) {
554 err = got_privsep_send_gitconfig_remotes_req(ibuf);
558 err = got_privsep_recv_gitconfig_remotes(remotes,
564 if (gitconfig_owner) {
565 err = got_privsep_send_gitconfig_owner_req(ibuf);
568 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
574 err = got_privsep_send_stop(imsg_fds[0]);
575 child_err = got_privsep_wait_for_child(pid);
576 if (child_err && err == NULL)
579 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
580 err = got_error_from_errno("close");
581 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (fd != -1 && close(fd) == -1 && err == NULL)
584 err = got_error_from_errno2("close", gitconfig_path);
589 static const struct got_error *
590 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
592 const struct got_error *err = NULL;
593 char *repo_gitconfig_path = NULL;
595 if (global_gitconfig_path) {
596 /* Read settings from ~/.gitconfig. */
597 int dummy_repo_version;
598 err = parse_gitconfig_file(&dummy_repo_version,
599 &repo->global_gitconfig_author_name,
600 &repo->global_gitconfig_author_email,
601 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
606 /* Read repository's .git/config file. */
607 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
608 if (repo_gitconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gitconfig");
611 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
612 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
613 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
614 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
615 repo_gitconfig_path);
619 free(repo_gitconfig_path);
623 static const struct got_error *
624 read_gotconfig(struct got_repository *repo)
626 const struct got_error *err = NULL;
627 char *gotconfig_path;
629 gotconfig_path = got_repo_get_path_gotconfig(repo);
630 if (gotconfig_path == NULL)
631 return got_error_from_errno("got_repo_get_path_gotconfig");
633 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
634 free(gotconfig_path);
638 /* Supported repository format extensions. */
639 static const char *repo_extensions[] = {
640 "noop", /* Got supports repository format version 1. */
641 "preciousObjects", /* Supported by gotadmin cleanup. */
642 "worktreeConfig", /* Got does not care about Git work trees. */
645 const struct got_error *
646 got_repo_open(struct got_repository **repop, const char *path,
647 const char *global_gitconfig_path)
649 struct got_repository *repo = NULL;
650 const struct got_error *err = NULL;
651 char *repo_path = NULL;
657 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
658 return got_error_from_errno("getrlimit");
660 repo = calloc(1, sizeof(*repo));
662 err = got_error_from_errno("calloc");
666 RB_INIT(&repo->packidx_bloom_filters);
668 for (i = 0; i < nitems(repo->privsep_children); i++) {
669 memset(&repo->privsep_children[i], 0,
670 sizeof(repo->privsep_children[0]));
671 repo->privsep_children[i].imsg_fd = -1;
674 err = got_object_cache_init(&repo->objcache,
675 GOT_OBJECT_CACHE_TYPE_OBJ);
678 err = got_object_cache_init(&repo->treecache,
679 GOT_OBJECT_CACHE_TYPE_TREE);
682 err = got_object_cache_init(&repo->commitcache,
683 GOT_OBJECT_CACHE_TYPE_COMMIT);
686 err = got_object_cache_init(&repo->tagcache,
687 GOT_OBJECT_CACHE_TYPE_TAG);
690 err = got_object_cache_init(&repo->rawcache,
691 GOT_OBJECT_CACHE_TYPE_RAW);
695 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
696 if (repo->pack_cache_size > rl.rlim_cur / 8)
697 repo->pack_cache_size = rl.rlim_cur / 8;
699 repo_path = realpath(path, NULL);
700 if (repo_path == NULL) {
701 err = got_error_from_errno2("realpath", path);
708 err = open_repo(repo, repo_path);
711 if (err->code != GOT_ERR_NOT_GIT_REPO)
713 if (repo_path[0] == '/' && repo_path[1] == '\0') {
714 err = got_error(GOT_ERR_NOT_GIT_REPO);
717 err = got_path_dirname(&parent_path, repo_path);
721 repo_path = parent_path;
724 err = read_gotconfig(repo);
728 err = read_gitconfig(repo, global_gitconfig_path);
731 if (repo->gitconfig_repository_format_version != 0)
732 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
733 for (i = 0; i < repo->nextensions; i++) {
734 char *ext = repo->extensions[i];
735 int j, supported = 0;
736 for (j = 0; j < nitems(repo_extensions); j++) {
737 if (strcmp(ext, repo_extensions[j]) == 0) {
743 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
749 got_repo_close(repo);
756 const struct got_error *
757 got_repo_close(struct got_repository *repo)
759 const struct got_error *err = NULL, *child_err;
760 struct got_packidx_bloom_filter *bf;
763 for (i = 0; i < repo->pack_cache_size; i++) {
764 if (repo->packidx_cache[i] == NULL)
766 got_packidx_close(repo->packidx_cache[i]);
769 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
770 &repo->packidx_bloom_filters))) {
771 RB_REMOVE(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters, bf);
777 for (i = 0; i < repo->pack_cache_size; i++) {
778 if (repo->packs[i].path_packfile == NULL)
780 got_pack_close(&repo->packs[i]);
784 free(repo->path_git_dir);
786 got_object_cache_close(&repo->objcache);
787 got_object_cache_close(&repo->treecache);
788 got_object_cache_close(&repo->commitcache);
789 got_object_cache_close(&repo->tagcache);
790 got_object_cache_close(&repo->rawcache);
792 for (i = 0; i < nitems(repo->privsep_children); i++) {
793 if (repo->privsep_children[i].imsg_fd == -1)
795 imsg_clear(repo->privsep_children[i].ibuf);
796 free(repo->privsep_children[i].ibuf);
797 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
798 child_err = got_privsep_wait_for_child(
799 repo->privsep_children[i].pid);
800 if (child_err && err == NULL)
802 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
804 err = got_error_from_errno("close");
807 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
809 err = got_error_from_errno("close");
812 got_gotconfig_free(repo->gotconfig);
813 free(repo->gitconfig_author_name);
814 free(repo->gitconfig_author_email);
815 for (i = 0; i < repo->ngitconfig_remotes; i++)
816 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
817 free(repo->gitconfig_remotes);
818 for (i = 0; i < repo->nextensions; i++)
819 free(repo->extensions[i]);
820 free(repo->extensions);
827 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
833 free(repo->fetch_url);
834 repo->fetch_url = NULL;
835 free(repo->send_url);
836 repo->send_url = NULL;
837 for (i = 0; i < repo->nfetch_branches; i++)
838 free(repo->fetch_branches[i]);
839 free(repo->fetch_branches);
840 repo->fetch_branches = NULL;
841 repo->nfetch_branches = 0;
842 for (i = 0; i < repo->nsend_branches; i++)
843 free(repo->send_branches[i]);
844 free(repo->send_branches);
845 repo->send_branches = NULL;
846 repo->nsend_branches = 0;
849 const struct got_error *
850 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
851 const char *input_path)
853 const struct got_error *err = NULL;
854 const char *repo_abspath = NULL;
856 char *canonpath, *path = NULL;
858 *in_repo_path = NULL;
860 canonpath = strdup(input_path);
861 if (canonpath == NULL) {
862 err = got_error_from_errno("strdup");
865 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
869 repo_abspath = got_repo_get_path(repo);
871 if (canonpath[0] == '\0') {
872 path = strdup(canonpath);
874 err = got_error_from_errno("strdup");
878 path = realpath(canonpath, NULL);
880 if (errno != ENOENT) {
881 err = got_error_from_errno2("realpath",
886 * Path is not on disk.
887 * Assume it is already relative to repository root.
889 path = strdup(canonpath);
891 err = got_error_from_errno("strdup");
896 repolen = strlen(repo_abspath);
900 if (strcmp(path, repo_abspath) == 0) {
904 err = got_error_from_errno("strdup");
907 } else if (len > repolen &&
908 got_path_is_child(path, repo_abspath, repolen)) {
909 /* Matched an on-disk path inside repository. */
910 if (got_repo_is_bare(repo)) {
912 * Matched an on-disk path inside repository
913 * database. Treat input as repository-relative.
920 /* Strip common prefix with repository path. */
921 err = got_path_skip_common_ancestor(&child,
930 * Matched unrelated on-disk path.
931 * Treat input as repository-relative.
939 /* Make in-repository path absolute */
940 if (path[0] != '/') {
942 if (asprintf(&abspath, "/%s", path) == -1) {
943 err = got_error_from_errno("asprintf");
955 *in_repo_path = path;
959 static const struct got_error *
960 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
961 const char *path_packidx)
963 const struct got_error *err = NULL;
966 for (i = 0; i < repo->pack_cache_size; i++) {
967 if (repo->packidx_cache[i] == NULL)
969 if (strcmp(repo->packidx_cache[i]->path_packidx,
970 path_packidx) == 0) {
971 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
974 if (i == repo->pack_cache_size) {
975 i = repo->pack_cache_size - 1;
976 err = got_packidx_close(repo->packidx_cache[i]);
981 repo->packidx_cache[i] = packidx;
987 got_repo_is_packidx_filename(const char *name, size_t len)
989 if (len != GOT_PACKIDX_NAMELEN)
992 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
995 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
996 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1002 static struct got_packidx_bloom_filter *
1003 get_packidx_bloom_filter(struct got_repository *repo,
1004 const char *path, size_t path_len)
1006 struct got_packidx_bloom_filter key;
1008 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1009 return NULL; /* XXX */
1010 key.path_len = path_len;
1012 return RB_FIND(got_packidx_bloom_filter_tree,
1013 &repo->packidx_bloom_filters, &key);
1017 check_packidx_bloom_filter(struct got_repository *repo,
1018 const char *path_packidx, struct got_object_id *id)
1020 struct got_packidx_bloom_filter *bf;
1022 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1024 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1026 /* No bloom filter means this pack index must be searched. */
1030 static const struct got_error *
1031 add_packidx_bloom_filter(struct got_repository *repo,
1032 struct got_packidx *packidx, const char *path_packidx)
1034 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1035 struct got_packidx_bloom_filter *bf;
1039 * Don't use bloom filters for very large pack index files.
1040 * Large pack files will contain a relatively large fraction
1041 * of our objects so we will likely need to visit them anyway.
1042 * The more objects a pack file contains the higher the probability
1043 * of a false-positive match from the bloom filter. And reading
1044 * all object IDs from a large pack index file can be expensive.
1046 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1049 /* Do we already have a filter for this pack index? */
1050 if (get_packidx_bloom_filter(repo, path_packidx,
1051 strlen(path_packidx)) != NULL)
1054 bf = calloc(1, sizeof(*bf));
1056 return got_error_from_errno("calloc");
1057 bf->bloom = calloc(1, sizeof(*bf->bloom));
1058 if (bf->bloom == NULL) {
1060 return got_error_from_errno("calloc");
1063 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1064 if (len >= sizeof(bf->path)) {
1067 return got_error(GOT_ERR_NO_SPACE);
1071 /* Minimum size supported by our bloom filter is 1000 entries. */
1072 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1073 for (i = 0; i < nobjects; i++) {
1074 struct got_packidx_object_id *id;
1075 id = &packidx->hdr.sorted_ids[i];
1076 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1079 RB_INSERT(got_packidx_bloom_filter_tree,
1080 &repo->packidx_bloom_filters, bf);
1084 const struct got_error *
1085 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1086 struct got_repository *repo, struct got_object_id *id)
1088 const struct got_error *err;
1089 DIR *packdir = NULL;
1090 struct dirent *dent;
1095 /* Search pack index cache. */
1096 for (i = 0; i < repo->pack_cache_size; i++) {
1097 if (repo->packidx_cache[i] == NULL)
1099 if (!check_packidx_bloom_filter(repo,
1100 repo->packidx_cache[i]->path_packidx, id))
1101 continue; /* object will not be found in this index */
1102 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1104 *packidx = repo->packidx_cache[i];
1106 * Move this cache entry to the front. Repeatedly
1107 * searching a wrong pack index can be expensive.
1110 memmove(&repo->packidx_cache[1],
1111 &repo->packidx_cache[0],
1112 i * sizeof(repo->packidx_cache[0]));
1113 repo->packidx_cache[0] = *packidx;
1118 /* No luck. Search the filesystem. */
1120 packdir_fd = openat(got_repo_get_fd(repo),
1121 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1122 if (packdir_fd == -1) {
1123 if (errno == ENOENT)
1124 err = got_error_no_obj(id);
1126 err = got_error_from_errno_fmt("openat: %s/%s",
1127 got_repo_get_path_git_dir(repo),
1128 GOT_OBJECTS_PACK_DIR);
1132 packdir = fdopendir(packdir_fd);
1133 if (packdir == NULL) {
1134 err = got_error_from_errno("fdopendir");
1138 while ((dent = readdir(packdir)) != NULL) {
1141 if (!got_repo_is_packidx_filename(dent->d_name,
1142 strlen(dent->d_name)))
1145 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1146 dent->d_name) == -1) {
1147 err = got_error_from_errno("asprintf");
1151 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1153 continue; /* object will not be found in this index */
1156 for (i = 0; i < repo->pack_cache_size; i++) {
1157 if (repo->packidx_cache[i] == NULL)
1159 if (strcmp(repo->packidx_cache[i]->path_packidx,
1160 path_packidx) == 0) {
1167 continue; /* already searched */
1170 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1177 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1183 err = cache_packidx(repo, *packidx, path_packidx);
1188 *idx = got_packidx_get_object_idx(*packidx, id);
1190 err = NULL; /* found the object */
1195 err = got_error_no_obj(id);
1197 if (packdir && closedir(packdir) != 0 && err == NULL)
1198 err = got_error_from_errno("closedir");
1202 static const struct got_error *
1203 read_packfile_hdr(int fd, struct got_packidx *packidx)
1205 const struct got_error *err = NULL;
1206 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1207 struct got_packfile_hdr hdr;
1210 n = read(fd, &hdr, sizeof(hdr));
1212 return got_error_from_errno("read");
1213 if (n != sizeof(hdr))
1214 return got_error(GOT_ERR_BAD_PACKFILE);
1216 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1217 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1218 be32toh(hdr.nobjects) != totobj)
1219 err = got_error(GOT_ERR_BAD_PACKFILE);
1224 static const struct got_error *
1225 open_packfile(int *fd, struct got_repository *repo,
1226 const char *relpath, struct got_packidx *packidx)
1228 const struct got_error *err = NULL;
1230 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1232 return got_error_from_errno_fmt("openat: %s/%s",
1233 got_repo_get_path_git_dir(repo), relpath);
1236 err = read_packfile_hdr(*fd, packidx);
1246 const struct got_error *
1247 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1248 const char *path_packfile, struct got_packidx *packidx)
1250 const struct got_error *err = NULL;
1251 struct got_pack *pack = NULL;
1258 for (i = 0; i < repo->pack_cache_size; i++) {
1259 pack = &repo->packs[i];
1260 if (pack->path_packfile == NULL)
1262 if (strcmp(pack->path_packfile, path_packfile) == 0)
1263 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1266 if (i == repo->pack_cache_size) {
1267 err = got_pack_close(&repo->packs[i - 1]);
1270 memmove(&repo->packs[1], &repo->packs[0],
1271 sizeof(repo->packs) - sizeof(repo->packs[0]));
1275 pack = &repo->packs[i];
1277 pack->path_packfile = strdup(path_packfile);
1278 if (pack->path_packfile == NULL) {
1279 err = got_error_from_errno("strdup");
1283 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1287 if (fstat(pack->fd, &sb) != 0) {
1288 err = got_error_from_errno("fstat");
1291 pack->filesize = sb.st_size;
1293 pack->privsep_child = NULL;
1295 #ifndef GOT_PACK_NO_MMAP
1296 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1298 if (pack->map == MAP_FAILED) {
1299 if (errno != ENOMEM) {
1300 err = got_error_from_errno("mmap");
1303 pack->map = NULL; /* fall back to read(2) */
1309 free(pack->path_packfile);
1310 memset(pack, 0, sizeof(*pack));
1318 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1320 struct got_pack *pack = NULL;
1323 for (i = 0; i < repo->pack_cache_size; i++) {
1324 pack = &repo->packs[i];
1325 if (pack->path_packfile == NULL)
1327 if (strcmp(pack->path_packfile, path_packfile) == 0)
1334 const struct got_error *
1335 got_repo_init(const char *repo_path)
1337 const struct got_error *err = NULL;
1338 const char *dirnames[] = {
1340 GOT_OBJECTS_PACK_DIR,
1343 const char *description_str = "Unnamed repository; "
1344 "edit this file 'description' to name the repository.";
1345 const char *headref_str = "ref: refs/heads/main";
1346 const char *gitconfig_str = "[core]\n"
1347 "\trepositoryformatversion = 0\n"
1348 "\tfilemode = true\n"
1353 if (!got_path_dir_is_empty(repo_path))
1354 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1356 for (i = 0; i < nitems(dirnames); i++) {
1357 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1358 return got_error_from_errno("asprintf");
1360 err = got_path_mkdir(path);
1366 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1367 return got_error_from_errno("asprintf");
1368 err = got_path_create_file(path, description_str);
1373 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1374 return got_error_from_errno("asprintf");
1375 err = got_path_create_file(path, headref_str);
1380 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1381 return got_error_from_errno("asprintf");
1382 err = got_path_create_file(path, gitconfig_str);
1390 static const struct got_error *
1391 match_packed_object(struct got_object_id **unique_id,
1392 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1394 const struct got_error *err = NULL;
1395 DIR *packdir = NULL;
1396 struct dirent *dent;
1398 struct got_object_id_queue matched_ids;
1401 STAILQ_INIT(&matched_ids);
1403 packdir_fd = openat(got_repo_get_fd(repo),
1404 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1405 if (packdir_fd == -1) {
1406 if (errno != ENOENT)
1407 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1411 packdir = fdopendir(packdir_fd);
1412 if (packdir == NULL) {
1413 err = got_error_from_errno("fdopendir");
1417 while ((dent = readdir(packdir)) != NULL) {
1418 struct got_packidx *packidx;
1419 struct got_object_qid *qid;
1421 if (!got_repo_is_packidx_filename(dent->d_name,
1422 strlen(dent->d_name)))
1425 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1426 dent->d_name) == -1) {
1427 err = got_error_from_errno("strdup");
1431 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1437 err = got_packidx_match_id_str_prefix(&matched_ids,
1438 packidx, id_str_prefix);
1440 got_packidx_close(packidx);
1443 err = got_packidx_close(packidx);
1447 STAILQ_FOREACH(qid, &matched_ids, entry) {
1448 if (obj_type != GOT_OBJ_TYPE_ANY) {
1450 err = got_object_get_type(&matched_type, repo,
1454 if (matched_type != obj_type)
1457 if (*unique_id == NULL) {
1458 *unique_id = got_object_id_dup(qid->id);
1459 if (*unique_id == NULL) {
1460 err = got_error_from_errno("malloc");
1464 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1465 continue; /* packed multiple times */
1466 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1472 got_object_id_queue_free(&matched_ids);
1473 if (packdir && closedir(packdir) != 0 && err == NULL)
1474 err = got_error_from_errno("closedir");
1482 static const struct got_error *
1483 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1484 const char *object_dir, const char *id_str_prefix, int obj_type,
1485 struct got_repository *repo)
1487 const struct got_error *err = NULL;
1490 struct dirent *dent;
1491 struct got_object_id id;
1493 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1494 err = got_error_from_errno("asprintf");
1498 dir = opendir(path);
1500 if (errno == ENOENT) {
1504 err = got_error_from_errno2("opendir", path);
1507 while ((dent = readdir(dir)) != NULL) {
1511 if (strcmp(dent->d_name, ".") == 0 ||
1512 strcmp(dent->d_name, "..") == 0)
1515 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1516 err = got_error_from_errno("asprintf");
1520 if (!got_parse_sha1_digest(id.sha1, id_str))
1524 * Directory entries do not necessarily appear in
1525 * sorted order, so we must iterate over all of them.
1527 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1533 if (*unique_id == NULL) {
1534 if (obj_type != GOT_OBJ_TYPE_ANY) {
1536 err = got_object_get_type(&matched_type, repo,
1540 if (matched_type != obj_type)
1543 *unique_id = got_object_id_dup(&id);
1544 if (*unique_id == NULL) {
1545 err = got_error_from_errno("got_object_id_dup");
1550 if (got_object_id_cmp(*unique_id, &id) == 0)
1551 continue; /* both packed and loose */
1552 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1558 if (dir && closedir(dir) != 0 && err == NULL)
1559 err = got_error_from_errno("closedir");
1568 const struct got_error *
1569 got_repo_match_object_id_prefix(struct got_object_id **id,
1570 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1572 const struct got_error *err = NULL;
1573 char *path_objects = got_repo_get_path_objects(repo);
1574 char *object_dir = NULL;
1580 for (i = 0; i < strlen(id_str_prefix); i++) {
1581 if (isxdigit((unsigned char)id_str_prefix[i]))
1583 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1586 len = strlen(id_str_prefix);
1588 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1591 object_dir = strndup(id_str_prefix, 2);
1592 if (object_dir == NULL) {
1593 err = got_error_from_errno("strdup");
1596 err = match_loose_object(id, path_objects, object_dir,
1597 id_str_prefix, obj_type, repo);
1598 } else if (len == 1) {
1600 for (i = 0; i < 0xf; i++) {
1601 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1603 err = got_error_from_errno("asprintf");
1606 err = match_packed_object(id, repo, object_dir,
1610 err = match_loose_object(id, path_objects, object_dir,
1611 id_str_prefix, obj_type, repo);
1616 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1624 } else if (*id == NULL) {
1626 case GOT_OBJ_TYPE_BLOB:
1627 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1628 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1630 case GOT_OBJ_TYPE_TREE:
1631 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1632 GOT_OBJ_LABEL_TREE, id_str_prefix);
1634 case GOT_OBJ_TYPE_COMMIT:
1635 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1636 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1638 case GOT_OBJ_TYPE_TAG:
1639 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1640 GOT_OBJ_LABEL_TAG, id_str_prefix);
1643 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1651 const struct got_error *
1652 got_repo_match_object_id(struct got_object_id **id, char **label,
1653 const char *id_str, int obj_type, struct got_reflist_head *refs,
1654 struct got_repository *repo)
1656 const struct got_error *err;
1657 struct got_tag_object *tag;
1658 struct got_reference *ref = NULL;
1665 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1668 *id = got_object_id_dup(
1669 got_object_tag_get_object_id(tag));
1671 err = got_error_from_errno("got_object_id_dup");
1672 else if (label && asprintf(label, "refs/tags/%s",
1673 got_object_tag_get_name(tag)) == -1) {
1674 err = got_error_from_errno("asprintf");
1678 got_object_tag_close(tag);
1680 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1681 err->code != GOT_ERR_NO_OBJ)
1685 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1687 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1689 err = got_ref_open(&ref, repo, id_str, 0);
1693 *label = strdup(got_ref_get_name(ref));
1694 if (*label == NULL) {
1695 err = got_error_from_errno("strdup");
1699 err = got_ref_resolve(id, repo, ref);
1701 err = got_object_id_str(label, *id);
1702 if (*label == NULL) {
1703 err = got_error_from_errno("strdup");
1713 const struct got_error *
1714 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1715 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1717 const struct got_error *err = NULL;
1718 struct got_reflist_entry *re;
1719 struct got_object_id *tag_id;
1720 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1724 TAILQ_FOREACH(re, refs, entry) {
1725 const char *refname;
1726 refname = got_ref_get_name(re->ref);
1727 if (got_ref_is_symbolic(re->ref))
1729 if (strncmp(refname, "refs/tags/", 10) != 0)
1731 if (!name_is_absolute)
1732 refname += strlen("refs/tags/");
1733 if (strcmp(refname, name) != 0)
1735 err = got_ref_resolve(&tag_id, repo, re->ref);
1738 err = got_object_open_as_tag(tag, repo, tag_id);
1742 if (obj_type == GOT_OBJ_TYPE_ANY ||
1743 got_object_tag_get_object_type(*tag) == obj_type)
1745 got_object_tag_close(*tag);
1749 if (err == NULL && *tag == NULL)
1750 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1751 GOT_OBJ_LABEL_TAG, name);
1755 static const struct got_error *
1756 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1757 const char *name, mode_t mode, struct got_object_id *blob_id)
1759 const struct got_error *err = NULL;
1763 *new_te = calloc(1, sizeof(**new_te));
1764 if (*new_te == NULL)
1765 return got_error_from_errno("calloc");
1767 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1768 sizeof((*new_te)->name)) {
1769 err = got_error(GOT_ERR_NO_SPACE);
1773 if (S_ISLNK(mode)) {
1774 (*new_te)->mode = S_IFLNK;
1776 (*new_te)->mode = S_IFREG;
1777 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1779 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1781 if (err && *new_te) {
1788 static const struct got_error *
1789 import_file(struct got_tree_entry **new_te, struct dirent *de,
1790 const char *path, struct got_repository *repo)
1792 const struct got_error *err;
1793 struct got_object_id *blob_id = NULL;
1797 if (asprintf(&filepath, "%s%s%s", path,
1798 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1799 return got_error_from_errno("asprintf");
1801 if (lstat(filepath, &sb) != 0) {
1802 err = got_error_from_errno2("lstat", path);
1806 err = got_object_blob_create(&blob_id, filepath, repo);
1810 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1819 static const struct got_error *
1820 insert_tree_entry(struct got_tree_entry *new_te,
1821 struct got_pathlist_head *paths)
1823 const struct got_error *err = NULL;
1824 struct got_pathlist_entry *new_pe;
1826 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1830 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1834 static const struct got_error *write_tree(struct got_object_id **,
1835 const char *, struct got_pathlist_head *, struct got_repository *,
1836 got_repo_import_cb progress_cb, void *progress_arg);
1838 static const struct got_error *
1839 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1840 const char *path, struct got_pathlist_head *ignores,
1841 struct got_repository *repo,
1842 got_repo_import_cb progress_cb, void *progress_arg)
1844 const struct got_error *err;
1845 struct got_object_id *id = NULL;
1848 if (asprintf(&subdirpath, "%s%s%s", path,
1849 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1850 return got_error_from_errno("asprintf");
1852 (*new_te) = calloc(1, sizeof(**new_te));
1853 if (*new_te == NULL)
1854 return got_error_from_errno("calloc");
1855 (*new_te)->mode = S_IFDIR;
1856 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1857 sizeof((*new_te)->name)) {
1858 err = got_error(GOT_ERR_NO_SPACE);
1861 err = write_tree(&id, subdirpath, ignores, repo,
1862 progress_cb, progress_arg);
1865 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1877 static const struct got_error *
1878 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1879 struct got_pathlist_head *ignores, struct got_repository *repo,
1880 got_repo_import_cb progress_cb, void *progress_arg)
1882 const struct got_error *err = NULL;
1886 struct got_tree_entry *new_te = NULL;
1887 struct got_pathlist_head paths;
1888 struct got_pathlist_entry *pe;
1890 *new_tree_id = NULL;
1894 dir = opendir(path_dir);
1896 err = got_error_from_errno2("opendir", path_dir);
1901 while ((de = readdir(dir)) != NULL) {
1905 if (strcmp(de->d_name, ".") == 0 ||
1906 strcmp(de->d_name, "..") == 0)
1909 TAILQ_FOREACH(pe, ignores, entry) {
1910 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1918 err = got_path_dirent_type(&type, path_dir, de);
1922 if (type == DT_DIR) {
1923 err = import_subdir(&new_te, de, path_dir,
1924 ignores, repo, progress_cb, progress_arg);
1926 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1931 } else if (type == DT_REG || type == DT_LNK) {
1932 err = import_file(&new_te, de, path_dir, repo);
1938 err = insert_tree_entry(new_te, &paths);
1944 if (TAILQ_EMPTY(&paths)) {
1945 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1946 "cannot create tree without any entries");
1950 TAILQ_FOREACH(pe, &paths, entry) {
1951 struct got_tree_entry *te = pe->data;
1953 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1955 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1956 err = got_error_from_errno("asprintf");
1959 err = (*progress_cb)(progress_arg, path);
1965 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1969 got_pathlist_free(&paths);
1973 const struct got_error *
1974 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1975 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1976 struct got_repository *repo, got_repo_import_cb progress_cb,
1979 const struct got_error *err;
1980 struct got_object_id *new_tree_id;
1982 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1983 progress_cb, progress_arg);
1987 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1988 author, time(NULL), author, time(NULL), logmsg, repo);
1993 const struct got_error *
1994 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1995 struct got_repository *repo)
1997 const struct got_error *err = NULL;
1998 char *path_objects = NULL, *path = NULL;
2000 struct got_object_id id;
2006 path_objects = got_repo_get_path_objects(repo);
2007 if (path_objects == NULL)
2008 return got_error_from_errno("got_repo_get_path_objects");
2010 for (i = 0; i <= 0xff; i++) {
2011 struct dirent *dent;
2013 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2014 err = got_error_from_errno("asprintf");
2018 dir = opendir(path);
2020 if (errno == ENOENT) {
2024 err = got_error_from_errno2("opendir", path);
2028 while ((dent = readdir(dir)) != NULL) {
2033 if (strcmp(dent->d_name, ".") == 0 ||
2034 strcmp(dent->d_name, "..") == 0)
2037 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2038 err = got_error_from_errno("asprintf");
2042 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2048 err = got_object_open_loose_fd(&fd, &id, repo);
2052 if (fstat(fd, &sb) == -1) {
2053 err = got_error_from_errno("fstat");
2058 (*ondisk_size) += sb.st_size;
2060 if (close(fd) == -1) {
2061 err = got_error_from_errno("close");
2066 if (closedir(dir) != 0) {
2067 err = got_error_from_errno("closedir");
2076 if (dir && closedir(dir) != 0 && err == NULL)
2077 err = got_error_from_errno("closedir");
2088 const struct got_error *
2089 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2090 off_t *total_packsize, struct got_repository *repo)
2092 const struct got_error *err = NULL;
2093 DIR *packdir = NULL;
2094 struct dirent *dent;
2095 struct got_packidx *packidx = NULL;
2097 char *path_packfile;
2103 *total_packsize = 0;
2105 packdir_fd = openat(got_repo_get_fd(repo),
2106 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2107 if (packdir_fd == -1) {
2108 return got_error_from_errno_fmt("openat: %s/%s",
2109 got_repo_get_path_git_dir(repo),
2110 GOT_OBJECTS_PACK_DIR);
2113 packdir = fdopendir(packdir_fd);
2114 if (packdir == NULL) {
2115 err = got_error_from_errno("fdopendir");
2119 while ((dent = readdir(packdir)) != NULL) {
2120 if (!got_repo_is_packidx_filename(dent->d_name,
2121 strlen(dent->d_name)))
2124 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2125 dent->d_name) == -1) {
2126 err = got_error_from_errno("asprintf");
2130 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2136 if (fstat(packidx->fd, &sb) == -1)
2138 *total_packsize += sb.st_size;
2140 err = got_packidx_get_packfile_path(&path_packfile,
2141 packidx->path_packidx);
2145 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2147 free(path_packfile);
2150 free(path_packfile);
2151 *total_packsize += sb.st_size;
2153 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2157 got_packidx_close(packidx);
2162 got_packidx_close(packidx);
2163 if (packdir && closedir(packdir) != 0 && err == NULL)
2164 err = got_error_from_errno("closedir");
2168 *total_packsize = 0;
2173 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2174 got_packidx_bloom_filter_cmp);