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>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
68 got_packidx_bloom_filter_cmp);
71 got_repo_get_path(struct got_repository *repo)
77 got_repo_get_path_git_dir(struct got_repository *repo)
79 return repo->path_git_dir;
83 got_repo_get_fd(struct got_repository *repo)
85 return repo->gitdir_fd;
89 got_repo_get_gitconfig_author_name(struct got_repository *repo)
91 return repo->gitconfig_author_name;
95 got_repo_get_gitconfig_author_email(struct got_repository *repo)
97 return repo->gitconfig_author_email;
101 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
103 return repo->global_gitconfig_author_name;
107 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
109 return repo->global_gitconfig_author_email;
113 got_repo_get_gitconfig_owner(struct got_repository *repo)
115 return repo->gitconfig_owner;
119 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
120 struct got_repository *repo)
122 *extensions = repo->extensions;
123 *nextensions = repo->nextensions;
127 got_repo_is_bare(struct got_repository *repo)
129 return (strcmp(repo->path, repo->path_git_dir) == 0);
133 get_path_git_child(struct got_repository *repo, const char *basename)
137 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
145 got_repo_get_path_objects(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_OBJECTS_DIR);
151 got_repo_get_path_objects_pack(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
157 got_repo_get_path_refs(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_REFS_DIR);
163 got_repo_get_path_packed_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
169 get_path_head(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_HEAD_FILE);
175 got_repo_get_path_gitconfig(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_GITCONFIG);
181 got_repo_get_path_gotconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
186 const struct got_gotconfig *
187 got_repo_get_gotconfig(struct got_repository *repo)
189 return repo->gotconfig;
193 got_repo_get_gitconfig_remotes(int *nremotes,
194 const struct got_remote_repo **remotes, struct got_repository *repo)
196 *nremotes = repo->ngitconfig_remotes;
197 *remotes = repo->gitconfig_remotes;
201 is_git_repo(struct got_repository *repo)
203 const char *path_git = got_repo_get_path_git_dir(repo);
204 char *path_objects = got_repo_get_path_objects(repo);
205 char *path_refs = got_repo_get_path_refs(repo);
206 char *path_head = get_path_head(repo);
209 struct got_reference *head_ref;
211 if (lstat(path_git, &sb) == -1)
213 if (!S_ISDIR(sb.st_mode))
216 if (lstat(path_objects, &sb) == -1)
218 if (!S_ISDIR(sb.st_mode))
221 if (lstat(path_refs, &sb) == -1)
223 if (!S_ISDIR(sb.st_mode))
226 if (lstat(path_head, &sb) == -1)
228 if (!S_ISREG(sb.st_mode))
231 /* Check if the HEAD reference can be opened. */
232 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
234 got_ref_close(head_ref);
245 const struct got_error *
246 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
247 struct got_object *obj)
249 #ifndef GOT_NO_OBJ_CACHE
250 const struct got_error *err = NULL;
251 err = got_object_cache_add(&repo->objcache, id, obj);
253 if (err->code == GOT_ERR_OBJ_EXISTS ||
254 err->code == GOT_ERR_OBJ_TOO_LARGE)
264 got_repo_get_cached_object(struct got_repository *repo,
265 struct got_object_id *id)
267 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
270 const struct got_error *
271 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
272 struct got_tree_object *tree)
274 #ifndef GOT_NO_OBJ_CACHE
275 const struct got_error *err = NULL;
276 err = got_object_cache_add(&repo->treecache, id, tree);
278 if (err->code == GOT_ERR_OBJ_EXISTS ||
279 err->code == GOT_ERR_OBJ_TOO_LARGE)
288 struct got_tree_object *
289 got_repo_get_cached_tree(struct got_repository *repo,
290 struct got_object_id *id)
292 return (struct got_tree_object *)got_object_cache_get(
293 &repo->treecache, id);
296 const struct got_error *
297 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
298 struct got_commit_object *commit)
300 #ifndef GOT_NO_OBJ_CACHE
301 const struct got_error *err = NULL;
302 err = got_object_cache_add(&repo->commitcache, id, commit);
304 if (err->code == GOT_ERR_OBJ_EXISTS ||
305 err->code == GOT_ERR_OBJ_TOO_LARGE)
314 struct got_commit_object *
315 got_repo_get_cached_commit(struct got_repository *repo,
316 struct got_object_id *id)
318 return (struct got_commit_object *)got_object_cache_get(
319 &repo->commitcache, id);
322 const struct got_error *
323 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
324 struct got_tag_object *tag)
326 #ifndef GOT_NO_OBJ_CACHE
327 const struct got_error *err = NULL;
328 err = got_object_cache_add(&repo->tagcache, id, tag);
330 if (err->code == GOT_ERR_OBJ_EXISTS ||
331 err->code == GOT_ERR_OBJ_TOO_LARGE)
340 struct got_tag_object *
341 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
343 return (struct got_tag_object *)got_object_cache_get(
344 &repo->tagcache, id);
347 const struct got_error *
348 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
349 struct got_raw_object *raw)
351 #ifndef GOT_NO_OBJ_CACHE
352 const struct got_error *err = NULL;
353 err = got_object_cache_add(&repo->rawcache, id, raw);
355 if (err->code == GOT_ERR_OBJ_EXISTS ||
356 err->code == GOT_ERR_OBJ_TOO_LARGE)
366 struct got_raw_object *
367 got_repo_get_cached_raw_object(struct got_repository *repo,
368 struct got_object_id *id)
370 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
374 static const struct got_error *
375 open_repo(struct got_repository *repo, const char *path)
377 const struct got_error *err = NULL;
379 repo->gitdir_fd = -1;
381 /* bare git repository? */
382 repo->path_git_dir = strdup(path);
383 if (repo->path_git_dir == NULL)
384 return got_error_from_errno("strdup");
385 if (is_git_repo(repo)) {
386 repo->path = strdup(repo->path_git_dir);
387 if (repo->path == NULL) {
388 err = got_error_from_errno("strdup");
391 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
392 if (repo->gitdir_fd == -1) {
393 err = got_error_from_errno2("open",
400 /* git repository with working tree? */
401 free(repo->path_git_dir);
402 repo->path_git_dir = NULL;
403 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
404 err = got_error_from_errno("asprintf");
407 if (is_git_repo(repo)) {
408 repo->path = strdup(path);
409 if (repo->path == NULL) {
410 err = got_error_from_errno("strdup");
413 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
414 if (repo->gitdir_fd == -1) {
415 err = got_error_from_errno2("open",
422 err = got_error(GOT_ERR_NOT_GIT_REPO);
427 free(repo->path_git_dir);
428 repo->path_git_dir = NULL;
429 if (repo->gitdir_fd != -1)
430 close(repo->gitdir_fd);
431 repo->gitdir_fd = -1;
437 static const struct got_error *
438 parse_gitconfig_file(int *gitconfig_repository_format_version,
439 char **gitconfig_author_name, char **gitconfig_author_email,
440 struct got_remote_repo **remotes, int *nremotes,
441 char **gitconfig_owner, char ***extensions, int *nextensions,
442 const char *gitconfig_path)
444 const struct got_error *err = NULL, *child_err = NULL;
446 int imsg_fds[2] = { -1, -1 };
448 struct imsgbuf *ibuf;
450 *gitconfig_repository_format_version = 0;
455 *gitconfig_author_name = NULL;
456 *gitconfig_author_email = NULL;
462 *gitconfig_owner = NULL;
464 fd = open(gitconfig_path, O_RDONLY);
468 return got_error_from_errno2("open", gitconfig_path);
471 ibuf = calloc(1, sizeof(*ibuf));
473 err = got_error_from_errno("calloc");
477 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
478 err = got_error_from_errno("socketpair");
484 err = got_error_from_errno("fork");
486 } else if (pid == 0) {
487 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
492 if (close(imsg_fds[1]) == -1) {
493 err = got_error_from_errno("close");
497 imsg_init(ibuf, imsg_fds[0]);
499 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
504 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
508 err = got_privsep_recv_gitconfig_int(
509 gitconfig_repository_format_version, ibuf);
513 if (extensions && nextensions) {
514 err = got_privsep_send_gitconfig_repository_extensions_req(
518 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
521 if (*nextensions > 0) {
523 *extensions = calloc(*nextensions, sizeof(char *));
524 if (*extensions == NULL) {
525 err = got_error_from_errno("calloc");
528 for (i = 0; i < *nextensions; i++) {
530 err = got_privsep_recv_gitconfig_str(&ext,
534 (*extensions)[i] = ext;
539 err = got_privsep_send_gitconfig_author_name_req(ibuf);
543 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
547 err = got_privsep_send_gitconfig_author_email_req(ibuf);
551 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
555 if (remotes && nremotes) {
556 err = got_privsep_send_gitconfig_remotes_req(ibuf);
560 err = got_privsep_recv_gitconfig_remotes(remotes,
566 if (gitconfig_owner) {
567 err = got_privsep_send_gitconfig_owner_req(ibuf);
570 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
576 err = got_privsep_send_stop(imsg_fds[0]);
577 child_err = got_privsep_wait_for_child(pid);
578 if (child_err && err == NULL)
581 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
584 err = got_error_from_errno("close");
585 if (fd != -1 && close(fd) == -1 && err == NULL)
586 err = got_error_from_errno2("close", gitconfig_path);
591 static const struct got_error *
592 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
594 const struct got_error *err = NULL;
595 char *repo_gitconfig_path = NULL;
597 if (global_gitconfig_path) {
598 /* Read settings from ~/.gitconfig. */
599 int dummy_repo_version;
600 err = parse_gitconfig_file(&dummy_repo_version,
601 &repo->global_gitconfig_author_name,
602 &repo->global_gitconfig_author_email,
603 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
608 /* Read repository's .git/config file. */
609 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
610 if (repo_gitconfig_path == NULL)
611 return got_error_from_errno("got_repo_get_path_gitconfig");
613 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
614 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
615 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
616 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
617 repo_gitconfig_path);
621 free(repo_gitconfig_path);
625 static const struct got_error *
626 read_gotconfig(struct got_repository *repo)
628 const struct got_error *err = NULL;
629 char *gotconfig_path;
631 gotconfig_path = got_repo_get_path_gotconfig(repo);
632 if (gotconfig_path == NULL)
633 return got_error_from_errno("got_repo_get_path_gotconfig");
635 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
636 free(gotconfig_path);
640 /* Supported repository format extensions. */
641 static const char *repo_extensions[] = {
642 "noop", /* Got supports repository format version 1. */
643 "preciousObjects", /* Supported by gotadmin cleanup. */
644 "worktreeConfig", /* Got does not care about Git work trees. */
647 const struct got_error *
648 got_repo_open(struct got_repository **repop, const char *path,
649 const char *global_gitconfig_path)
651 struct got_repository *repo = NULL;
652 const struct got_error *err = NULL;
653 char *repo_path = NULL;
659 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
660 return got_error_from_errno("getrlimit");
662 repo = calloc(1, sizeof(*repo));
664 err = got_error_from_errno("calloc");
668 RB_INIT(&repo->packidx_bloom_filters);
670 for (i = 0; i < nitems(repo->privsep_children); i++) {
671 memset(&repo->privsep_children[i], 0,
672 sizeof(repo->privsep_children[0]));
673 repo->privsep_children[i].imsg_fd = -1;
676 err = got_object_cache_init(&repo->objcache,
677 GOT_OBJECT_CACHE_TYPE_OBJ);
680 err = got_object_cache_init(&repo->treecache,
681 GOT_OBJECT_CACHE_TYPE_TREE);
684 err = got_object_cache_init(&repo->commitcache,
685 GOT_OBJECT_CACHE_TYPE_COMMIT);
688 err = got_object_cache_init(&repo->tagcache,
689 GOT_OBJECT_CACHE_TYPE_TAG);
692 err = got_object_cache_init(&repo->rawcache,
693 GOT_OBJECT_CACHE_TYPE_RAW);
697 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
698 if (repo->pack_cache_size > rl.rlim_cur / 8)
699 repo->pack_cache_size = rl.rlim_cur / 8;
701 repo_path = realpath(path, NULL);
702 if (repo_path == NULL) {
703 err = got_error_from_errno2("realpath", path);
710 err = open_repo(repo, repo_path);
713 if (err->code != GOT_ERR_NOT_GIT_REPO)
715 if (repo_path[0] == '/' && repo_path[1] == '\0') {
716 err = got_error(GOT_ERR_NOT_GIT_REPO);
719 err = got_path_dirname(&parent_path, repo_path);
723 repo_path = parent_path;
726 err = read_gotconfig(repo);
730 err = read_gitconfig(repo, global_gitconfig_path);
733 if (repo->gitconfig_repository_format_version != 0)
734 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
735 for (i = 0; i < repo->nextensions; i++) {
736 char *ext = repo->extensions[i];
737 int j, supported = 0;
738 for (j = 0; j < nitems(repo_extensions); j++) {
739 if (strcmp(ext, repo_extensions[j]) == 0) {
745 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
751 got_repo_close(repo);
758 const struct got_error *
759 got_repo_close(struct got_repository *repo)
761 const struct got_error *err = NULL, *child_err;
762 struct got_packidx_bloom_filter *bf;
765 for (i = 0; i < repo->pack_cache_size; i++) {
766 if (repo->packidx_cache[i] == NULL)
768 got_packidx_close(repo->packidx_cache[i]);
771 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
772 &repo->packidx_bloom_filters))) {
773 RB_REMOVE(got_packidx_bloom_filter_tree,
774 &repo->packidx_bloom_filters, bf);
779 for (i = 0; i < repo->pack_cache_size; i++) {
780 if (repo->packs[i].path_packfile == NULL)
782 got_pack_close(&repo->packs[i]);
786 free(repo->path_git_dir);
788 got_object_cache_close(&repo->objcache);
789 got_object_cache_close(&repo->treecache);
790 got_object_cache_close(&repo->commitcache);
791 got_object_cache_close(&repo->tagcache);
792 got_object_cache_close(&repo->rawcache);
794 for (i = 0; i < nitems(repo->privsep_children); i++) {
795 if (repo->privsep_children[i].imsg_fd == -1)
797 imsg_clear(repo->privsep_children[i].ibuf);
798 free(repo->privsep_children[i].ibuf);
799 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
800 child_err = got_privsep_wait_for_child(
801 repo->privsep_children[i].pid);
802 if (child_err && err == NULL)
804 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
806 err = got_error_from_errno("close");
809 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
811 err = got_error_from_errno("close");
814 got_gotconfig_free(repo->gotconfig);
815 free(repo->gitconfig_author_name);
816 free(repo->gitconfig_author_email);
817 for (i = 0; i < repo->ngitconfig_remotes; i++)
818 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
819 free(repo->gitconfig_remotes);
820 for (i = 0; i < repo->nextensions; i++)
821 free(repo->extensions[i]);
822 free(repo->extensions);
829 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
835 free(repo->fetch_url);
836 repo->fetch_url = NULL;
837 free(repo->send_url);
838 repo->send_url = NULL;
839 for (i = 0; i < repo->nfetch_branches; i++)
840 free(repo->fetch_branches[i]);
841 free(repo->fetch_branches);
842 repo->fetch_branches = NULL;
843 repo->nfetch_branches = 0;
844 for (i = 0; i < repo->nsend_branches; i++)
845 free(repo->send_branches[i]);
846 free(repo->send_branches);
847 repo->send_branches = NULL;
848 repo->nsend_branches = 0;
851 const struct got_error *
852 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
853 const char *input_path)
855 const struct got_error *err = NULL;
856 const char *repo_abspath = NULL;
858 char *canonpath, *path = NULL;
860 *in_repo_path = NULL;
862 canonpath = strdup(input_path);
863 if (canonpath == NULL) {
864 err = got_error_from_errno("strdup");
867 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
871 repo_abspath = got_repo_get_path(repo);
873 if (canonpath[0] == '\0') {
874 path = strdup(canonpath);
876 err = got_error_from_errno("strdup");
880 path = realpath(canonpath, NULL);
882 if (errno != ENOENT) {
883 err = got_error_from_errno2("realpath",
888 * Path is not on disk.
889 * Assume it is already relative to repository root.
891 path = strdup(canonpath);
893 err = got_error_from_errno("strdup");
898 repolen = strlen(repo_abspath);
902 if (strcmp(path, repo_abspath) == 0) {
906 err = got_error_from_errno("strdup");
909 } else if (len > repolen &&
910 got_path_is_child(path, repo_abspath, repolen)) {
911 /* Matched an on-disk path inside repository. */
912 if (got_repo_is_bare(repo)) {
914 * Matched an on-disk path inside repository
915 * database. Treat input as repository-relative.
922 /* Strip common prefix with repository path. */
923 err = got_path_skip_common_ancestor(&child,
932 * Matched unrelated on-disk path.
933 * Treat input as repository-relative.
941 /* Make in-repository path absolute */
942 if (path[0] != '/') {
944 if (asprintf(&abspath, "/%s", path) == -1) {
945 err = got_error_from_errno("asprintf");
957 *in_repo_path = path;
961 static const struct got_error *
962 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
963 const char *path_packidx)
965 const struct got_error *err = NULL;
968 for (i = 0; i < repo->pack_cache_size; i++) {
969 if (repo->packidx_cache[i] == NULL)
971 if (strcmp(repo->packidx_cache[i]->path_packidx,
972 path_packidx) == 0) {
973 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
976 if (i == repo->pack_cache_size) {
977 i = repo->pack_cache_size - 1;
978 err = got_packidx_close(repo->packidx_cache[i]);
983 repo->packidx_cache[i] = packidx;
989 got_repo_is_packidx_filename(const char *name, size_t len)
991 if (len != GOT_PACKIDX_NAMELEN)
994 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
997 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
998 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1004 static struct got_packidx_bloom_filter *
1005 get_packidx_bloom_filter(struct got_repository *repo,
1006 const char *path, size_t path_len)
1008 struct got_packidx_bloom_filter key;
1010 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1011 return NULL; /* XXX */
1012 key.path_len = path_len;
1014 return RB_FIND(got_packidx_bloom_filter_tree,
1015 &repo->packidx_bloom_filters, &key);
1019 check_packidx_bloom_filter(struct got_repository *repo,
1020 const char *path_packidx, struct got_object_id *id)
1022 struct got_packidx_bloom_filter *bf;
1024 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1026 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1028 /* No bloom filter means this pack index must be searched. */
1032 static const struct got_error *
1033 add_packidx_bloom_filter(struct got_repository *repo,
1034 struct got_packidx *packidx, const char *path_packidx)
1036 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1037 struct got_packidx_bloom_filter *bf;
1041 * Don't use bloom filters for very large pack index files.
1042 * Large pack files will contain a relatively large fraction
1043 * of our objects so we will likely need to visit them anyway.
1044 * The more objects a pack file contains the higher the probability
1045 * of a false-positive match from the bloom filter. And reading
1046 * all object IDs from a large pack index file can be expensive.
1048 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1051 /* Do we already have a filter for this pack index? */
1052 if (get_packidx_bloom_filter(repo, path_packidx,
1053 strlen(path_packidx)) != NULL)
1056 bf = calloc(1, sizeof(*bf));
1058 return got_error_from_errno("calloc");
1059 bf->bloom = calloc(1, sizeof(*bf->bloom));
1060 if (bf->bloom == NULL) {
1062 return got_error_from_errno("calloc");
1065 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1066 if (len >= sizeof(bf->path)) {
1069 return got_error(GOT_ERR_NO_SPACE);
1073 /* Minimum size supported by our bloom filter is 1000 entries. */
1074 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1075 for (i = 0; i < nobjects; i++) {
1076 struct got_packidx_object_id *id;
1077 id = &packidx->hdr.sorted_ids[i];
1078 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1081 RB_INSERT(got_packidx_bloom_filter_tree,
1082 &repo->packidx_bloom_filters, bf);
1086 const struct got_error *
1087 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1088 struct got_repository *repo, struct got_object_id *id)
1090 const struct got_error *err;
1091 DIR *packdir = NULL;
1092 struct dirent *dent;
1097 /* Search pack index cache. */
1098 for (i = 0; i < repo->pack_cache_size; i++) {
1099 if (repo->packidx_cache[i] == NULL)
1101 if (!check_packidx_bloom_filter(repo,
1102 repo->packidx_cache[i]->path_packidx, id))
1103 continue; /* object will not be found in this index */
1104 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1106 *packidx = repo->packidx_cache[i];
1108 * Move this cache entry to the front. Repeatedly
1109 * searching a wrong pack index can be expensive.
1112 memmove(&repo->packidx_cache[1],
1113 &repo->packidx_cache[0],
1114 i * sizeof(repo->packidx_cache[0]));
1115 repo->packidx_cache[0] = *packidx;
1120 /* No luck. Search the filesystem. */
1122 packdir_fd = openat(got_repo_get_fd(repo),
1123 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1124 if (packdir_fd == -1) {
1125 if (errno == ENOENT)
1126 err = got_error_no_obj(id);
1128 err = got_error_from_errno_fmt("openat: %s/%s",
1129 got_repo_get_path_git_dir(repo),
1130 GOT_OBJECTS_PACK_DIR);
1134 packdir = fdopendir(packdir_fd);
1135 if (packdir == NULL) {
1136 err = got_error_from_errno("fdopendir");
1140 while ((dent = readdir(packdir)) != NULL) {
1143 if (!got_repo_is_packidx_filename(dent->d_name,
1144 strlen(dent->d_name)))
1147 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1148 dent->d_name) == -1) {
1149 err = got_error_from_errno("asprintf");
1153 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1155 continue; /* object will not be found in this index */
1158 for (i = 0; i < repo->pack_cache_size; i++) {
1159 if (repo->packidx_cache[i] == NULL)
1161 if (strcmp(repo->packidx_cache[i]->path_packidx,
1162 path_packidx) == 0) {
1169 continue; /* already searched */
1172 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1179 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1185 err = cache_packidx(repo, *packidx, path_packidx);
1190 *idx = got_packidx_get_object_idx(*packidx, id);
1192 err = NULL; /* found the object */
1197 err = got_error_no_obj(id);
1199 if (packdir && closedir(packdir) != 0 && err == NULL)
1200 err = got_error_from_errno("closedir");
1204 static const struct got_error *
1205 read_packfile_hdr(int fd, struct got_packidx *packidx)
1207 const struct got_error *err = NULL;
1208 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1209 struct got_packfile_hdr hdr;
1212 n = read(fd, &hdr, sizeof(hdr));
1214 return got_error_from_errno("read");
1215 if (n != sizeof(hdr))
1216 return got_error(GOT_ERR_BAD_PACKFILE);
1218 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1219 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1220 be32toh(hdr.nobjects) != totobj)
1221 err = got_error(GOT_ERR_BAD_PACKFILE);
1226 static const struct got_error *
1227 open_packfile(int *fd, struct got_repository *repo,
1228 const char *relpath, struct got_packidx *packidx)
1230 const struct got_error *err = NULL;
1232 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1234 return got_error_from_errno_fmt("openat: %s/%s",
1235 got_repo_get_path_git_dir(repo), relpath);
1238 err = read_packfile_hdr(*fd, packidx);
1248 const struct got_error *
1249 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1250 const char *path_packfile, struct got_packidx *packidx)
1252 const struct got_error *err = NULL;
1253 struct got_pack *pack = NULL;
1260 for (i = 0; i < repo->pack_cache_size; i++) {
1261 pack = &repo->packs[i];
1262 if (pack->path_packfile == NULL)
1264 if (strcmp(pack->path_packfile, path_packfile) == 0)
1265 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1268 if (i == repo->pack_cache_size) {
1269 err = got_pack_close(&repo->packs[i - 1]);
1272 memmove(&repo->packs[1], &repo->packs[0],
1273 sizeof(repo->packs) - sizeof(repo->packs[0]));
1277 pack = &repo->packs[i];
1279 pack->path_packfile = strdup(path_packfile);
1280 if (pack->path_packfile == NULL) {
1281 err = got_error_from_errno("strdup");
1285 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1289 if (fstat(pack->fd, &sb) != 0) {
1290 err = got_error_from_errno("fstat");
1293 pack->filesize = sb.st_size;
1295 pack->privsep_child = NULL;
1297 #ifndef GOT_PACK_NO_MMAP
1298 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1300 if (pack->map == MAP_FAILED) {
1301 if (errno != ENOMEM) {
1302 err = got_error_from_errno("mmap");
1305 pack->map = NULL; /* fall back to read(2) */
1311 free(pack->path_packfile);
1312 memset(pack, 0, sizeof(*pack));
1320 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1322 struct got_pack *pack = NULL;
1325 for (i = 0; i < repo->pack_cache_size; i++) {
1326 pack = &repo->packs[i];
1327 if (pack->path_packfile == NULL)
1329 if (strcmp(pack->path_packfile, path_packfile) == 0)
1336 const struct got_error *
1337 got_repo_init(const char *repo_path)
1339 const struct got_error *err = NULL;
1340 const char *dirnames[] = {
1342 GOT_OBJECTS_PACK_DIR,
1345 const char *description_str = "Unnamed repository; "
1346 "edit this file 'description' to name the repository.";
1347 const char *headref_str = "ref: refs/heads/main";
1348 const char *gitconfig_str = "[core]\n"
1349 "\trepositoryformatversion = 0\n"
1350 "\tfilemode = true\n"
1355 if (!got_path_dir_is_empty(repo_path))
1356 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1358 for (i = 0; i < nitems(dirnames); i++) {
1359 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1360 return got_error_from_errno("asprintf");
1362 err = got_path_mkdir(path);
1368 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1369 return got_error_from_errno("asprintf");
1370 err = got_path_create_file(path, description_str);
1375 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1376 return got_error_from_errno("asprintf");
1377 err = got_path_create_file(path, headref_str);
1382 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1383 return got_error_from_errno("asprintf");
1384 err = got_path_create_file(path, gitconfig_str);
1392 static const struct got_error *
1393 match_packed_object(struct got_object_id **unique_id,
1394 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1396 const struct got_error *err = NULL;
1397 DIR *packdir = NULL;
1398 struct dirent *dent;
1400 struct got_object_id_queue matched_ids;
1403 STAILQ_INIT(&matched_ids);
1405 packdir_fd = openat(got_repo_get_fd(repo),
1406 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1407 if (packdir_fd == -1) {
1408 if (errno != ENOENT)
1409 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1413 packdir = fdopendir(packdir_fd);
1414 if (packdir == NULL) {
1415 err = got_error_from_errno("fdopendir");
1419 while ((dent = readdir(packdir)) != NULL) {
1420 struct got_packidx *packidx;
1421 struct got_object_qid *qid;
1423 if (!got_repo_is_packidx_filename(dent->d_name,
1424 strlen(dent->d_name)))
1427 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1428 dent->d_name) == -1) {
1429 err = got_error_from_errno("strdup");
1433 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1439 err = got_packidx_match_id_str_prefix(&matched_ids,
1440 packidx, id_str_prefix);
1442 got_packidx_close(packidx);
1445 err = got_packidx_close(packidx);
1449 STAILQ_FOREACH(qid, &matched_ids, entry) {
1450 if (obj_type != GOT_OBJ_TYPE_ANY) {
1452 err = got_object_get_type(&matched_type, repo,
1456 if (matched_type != obj_type)
1459 if (*unique_id == NULL) {
1460 *unique_id = got_object_id_dup(qid->id);
1461 if (*unique_id == NULL) {
1462 err = got_error_from_errno("malloc");
1466 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1467 continue; /* packed multiple times */
1468 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1474 got_object_id_queue_free(&matched_ids);
1475 if (packdir && closedir(packdir) != 0 && err == NULL)
1476 err = got_error_from_errno("closedir");
1484 static const struct got_error *
1485 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1486 const char *object_dir, const char *id_str_prefix, int obj_type,
1487 struct got_repository *repo)
1489 const struct got_error *err = NULL;
1492 struct dirent *dent;
1493 struct got_object_id id;
1495 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1496 err = got_error_from_errno("asprintf");
1500 dir = opendir(path);
1502 if (errno == ENOENT) {
1506 err = got_error_from_errno2("opendir", path);
1509 while ((dent = readdir(dir)) != NULL) {
1513 if (strcmp(dent->d_name, ".") == 0 ||
1514 strcmp(dent->d_name, "..") == 0)
1517 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1518 err = got_error_from_errno("asprintf");
1522 if (!got_parse_sha1_digest(id.sha1, id_str))
1526 * Directory entries do not necessarily appear in
1527 * sorted order, so we must iterate over all of them.
1529 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1535 if (*unique_id == NULL) {
1536 if (obj_type != GOT_OBJ_TYPE_ANY) {
1538 err = got_object_get_type(&matched_type, repo,
1542 if (matched_type != obj_type)
1545 *unique_id = got_object_id_dup(&id);
1546 if (*unique_id == NULL) {
1547 err = got_error_from_errno("got_object_id_dup");
1552 if (got_object_id_cmp(*unique_id, &id) == 0)
1553 continue; /* both packed and loose */
1554 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1560 if (dir && closedir(dir) != 0 && err == NULL)
1561 err = got_error_from_errno("closedir");
1570 const struct got_error *
1571 got_repo_match_object_id_prefix(struct got_object_id **id,
1572 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 char *path_objects = got_repo_get_path_objects(repo);
1576 char *object_dir = NULL;
1582 for (i = 0; i < strlen(id_str_prefix); i++) {
1583 if (isxdigit((unsigned char)id_str_prefix[i]))
1585 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1588 len = strlen(id_str_prefix);
1590 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1593 object_dir = strndup(id_str_prefix, 2);
1594 if (object_dir == NULL) {
1595 err = got_error_from_errno("strdup");
1598 err = match_loose_object(id, path_objects, object_dir,
1599 id_str_prefix, obj_type, repo);
1600 } else if (len == 1) {
1602 for (i = 0; i < 0xf; i++) {
1603 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1605 err = got_error_from_errno("asprintf");
1608 err = match_packed_object(id, repo, object_dir,
1612 err = match_loose_object(id, path_objects, object_dir,
1613 id_str_prefix, obj_type, repo);
1618 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1626 } else if (*id == NULL) {
1628 case GOT_OBJ_TYPE_BLOB:
1629 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1630 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1632 case GOT_OBJ_TYPE_TREE:
1633 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1634 GOT_OBJ_LABEL_TREE, id_str_prefix);
1636 case GOT_OBJ_TYPE_COMMIT:
1637 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1638 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1640 case GOT_OBJ_TYPE_TAG:
1641 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1642 GOT_OBJ_LABEL_TAG, id_str_prefix);
1645 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1653 const struct got_error *
1654 got_repo_match_object_id(struct got_object_id **id, char **label,
1655 const char *id_str, int obj_type, struct got_reflist_head *refs,
1656 struct got_repository *repo)
1658 const struct got_error *err;
1659 struct got_tag_object *tag;
1660 struct got_reference *ref = NULL;
1667 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1670 *id = got_object_id_dup(
1671 got_object_tag_get_object_id(tag));
1673 err = got_error_from_errno("got_object_id_dup");
1674 else if (label && asprintf(label, "refs/tags/%s",
1675 got_object_tag_get_name(tag)) == -1) {
1676 err = got_error_from_errno("asprintf");
1680 got_object_tag_close(tag);
1682 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1683 err->code != GOT_ERR_NO_OBJ)
1687 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1689 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1691 err = got_ref_open(&ref, repo, id_str, 0);
1695 *label = strdup(got_ref_get_name(ref));
1696 if (*label == NULL) {
1697 err = got_error_from_errno("strdup");
1701 err = got_ref_resolve(id, repo, ref);
1703 err = got_object_id_str(label, *id);
1704 if (*label == NULL) {
1705 err = got_error_from_errno("strdup");
1715 const struct got_error *
1716 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1717 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1719 const struct got_error *err = NULL;
1720 struct got_reflist_entry *re;
1721 struct got_object_id *tag_id;
1722 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1726 TAILQ_FOREACH(re, refs, entry) {
1727 const char *refname;
1728 refname = got_ref_get_name(re->ref);
1729 if (got_ref_is_symbolic(re->ref))
1731 if (strncmp(refname, "refs/tags/", 10) != 0)
1733 if (!name_is_absolute)
1734 refname += strlen("refs/tags/");
1735 if (strcmp(refname, name) != 0)
1737 err = got_ref_resolve(&tag_id, repo, re->ref);
1740 err = got_object_open_as_tag(tag, repo, tag_id);
1744 if (obj_type == GOT_OBJ_TYPE_ANY ||
1745 got_object_tag_get_object_type(*tag) == obj_type)
1747 got_object_tag_close(*tag);
1751 if (err == NULL && *tag == NULL)
1752 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1753 GOT_OBJ_LABEL_TAG, name);
1757 static const struct got_error *
1758 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1759 const char *name, mode_t mode, struct got_object_id *blob_id)
1761 const struct got_error *err = NULL;
1765 *new_te = calloc(1, sizeof(**new_te));
1766 if (*new_te == NULL)
1767 return got_error_from_errno("calloc");
1769 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1770 sizeof((*new_te)->name)) {
1771 err = got_error(GOT_ERR_NO_SPACE);
1775 if (S_ISLNK(mode)) {
1776 (*new_te)->mode = S_IFLNK;
1778 (*new_te)->mode = S_IFREG;
1779 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1781 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1783 if (err && *new_te) {
1790 static const struct got_error *
1791 import_file(struct got_tree_entry **new_te, struct dirent *de,
1792 const char *path, struct got_repository *repo)
1794 const struct got_error *err;
1795 struct got_object_id *blob_id = NULL;
1799 if (asprintf(&filepath, "%s%s%s", path,
1800 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1801 return got_error_from_errno("asprintf");
1803 if (lstat(filepath, &sb) != 0) {
1804 err = got_error_from_errno2("lstat", path);
1808 err = got_object_blob_create(&blob_id, filepath, repo);
1812 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1821 static const struct got_error *
1822 insert_tree_entry(struct got_tree_entry *new_te,
1823 struct got_pathlist_head *paths)
1825 const struct got_error *err = NULL;
1826 struct got_pathlist_entry *new_pe;
1828 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1832 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1836 static const struct got_error *write_tree(struct got_object_id **,
1837 const char *, struct got_pathlist_head *, struct got_repository *,
1838 got_repo_import_cb progress_cb, void *progress_arg);
1840 static const struct got_error *
1841 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1842 const char *path, struct got_pathlist_head *ignores,
1843 struct got_repository *repo,
1844 got_repo_import_cb progress_cb, void *progress_arg)
1846 const struct got_error *err;
1847 struct got_object_id *id = NULL;
1850 if (asprintf(&subdirpath, "%s%s%s", path,
1851 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1852 return got_error_from_errno("asprintf");
1854 (*new_te) = calloc(1, sizeof(**new_te));
1855 if (*new_te == NULL)
1856 return got_error_from_errno("calloc");
1857 (*new_te)->mode = S_IFDIR;
1858 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1859 sizeof((*new_te)->name)) {
1860 err = got_error(GOT_ERR_NO_SPACE);
1863 err = write_tree(&id, subdirpath, ignores, repo,
1864 progress_cb, progress_arg);
1867 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1879 static const struct got_error *
1880 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1881 struct got_pathlist_head *ignores, struct got_repository *repo,
1882 got_repo_import_cb progress_cb, void *progress_arg)
1884 const struct got_error *err = NULL;
1888 struct got_tree_entry *new_te = NULL;
1889 struct got_pathlist_head paths;
1890 struct got_pathlist_entry *pe;
1892 *new_tree_id = NULL;
1896 dir = opendir(path_dir);
1898 err = got_error_from_errno2("opendir", path_dir);
1903 while ((de = readdir(dir)) != NULL) {
1907 if (strcmp(de->d_name, ".") == 0 ||
1908 strcmp(de->d_name, "..") == 0)
1911 TAILQ_FOREACH(pe, ignores, entry) {
1912 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1920 err = got_path_dirent_type(&type, path_dir, de);
1924 if (type == DT_DIR) {
1925 err = import_subdir(&new_te, de, path_dir,
1926 ignores, repo, progress_cb, progress_arg);
1928 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1933 } else if (type == DT_REG || type == DT_LNK) {
1934 err = import_file(&new_te, de, path_dir, repo);
1940 err = insert_tree_entry(new_te, &paths);
1946 if (TAILQ_EMPTY(&paths)) {
1947 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1948 "cannot create tree without any entries");
1952 TAILQ_FOREACH(pe, &paths, entry) {
1953 struct got_tree_entry *te = pe->data;
1955 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1957 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1958 err = got_error_from_errno("asprintf");
1961 err = (*progress_cb)(progress_arg, path);
1967 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1971 got_pathlist_free(&paths);
1975 const struct got_error *
1976 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1977 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1978 struct got_repository *repo, got_repo_import_cb progress_cb,
1981 const struct got_error *err;
1982 struct got_object_id *new_tree_id;
1984 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1985 progress_cb, progress_arg);
1989 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1990 author, time(NULL), author, time(NULL), logmsg, repo);
1995 const struct got_error *
1996 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1997 struct got_repository *repo)
1999 const struct got_error *err = NULL;
2000 char *path_objects = NULL, *path = NULL;
2002 struct got_object_id id;
2008 path_objects = got_repo_get_path_objects(repo);
2009 if (path_objects == NULL)
2010 return got_error_from_errno("got_repo_get_path_objects");
2012 for (i = 0; i <= 0xff; i++) {
2013 struct dirent *dent;
2015 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2016 err = got_error_from_errno("asprintf");
2020 dir = opendir(path);
2022 if (errno == ENOENT) {
2026 err = got_error_from_errno2("opendir", path);
2030 while ((dent = readdir(dir)) != NULL) {
2035 if (strcmp(dent->d_name, ".") == 0 ||
2036 strcmp(dent->d_name, "..") == 0)
2039 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2040 err = got_error_from_errno("asprintf");
2044 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2050 err = got_object_open_loose_fd(&fd, &id, repo);
2054 if (fstat(fd, &sb) == -1) {
2055 err = got_error_from_errno("fstat");
2060 (*ondisk_size) += sb.st_size;
2062 if (close(fd) == -1) {
2063 err = got_error_from_errno("close");
2068 if (closedir(dir) != 0) {
2069 err = got_error_from_errno("closedir");
2078 if (dir && closedir(dir) != 0 && err == NULL)
2079 err = got_error_from_errno("closedir");
2090 const struct got_error *
2091 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2092 off_t *total_packsize, struct got_repository *repo)
2094 const struct got_error *err = NULL;
2095 DIR *packdir = NULL;
2096 struct dirent *dent;
2097 struct got_packidx *packidx = NULL;
2099 char *path_packfile;
2105 *total_packsize = 0;
2107 packdir_fd = openat(got_repo_get_fd(repo),
2108 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2109 if (packdir_fd == -1) {
2110 return got_error_from_errno_fmt("openat: %s/%s",
2111 got_repo_get_path_git_dir(repo),
2112 GOT_OBJECTS_PACK_DIR);
2115 packdir = fdopendir(packdir_fd);
2116 if (packdir == NULL) {
2117 err = got_error_from_errno("fdopendir");
2121 while ((dent = readdir(packdir)) != NULL) {
2122 if (!got_repo_is_packidx_filename(dent->d_name,
2123 strlen(dent->d_name)))
2126 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2127 dent->d_name) == -1) {
2128 err = got_error_from_errno("asprintf");
2132 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2138 if (fstat(packidx->fd, &sb) == -1)
2140 *total_packsize += sb.st_size;
2142 err = got_packidx_get_packfile_path(&path_packfile,
2143 packidx->path_packidx);
2147 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2149 free(path_packfile);
2152 free(path_packfile);
2153 *total_packsize += sb.st_size;
2155 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2159 got_packidx_close(packidx);
2164 got_packidx_close(packidx);
2165 if (packdir && closedir(packdir) != 0 && err == NULL)
2166 err = got_error_from_errno("closedir");
2170 *total_packsize = 0;
2175 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2176 got_packidx_bloom_filter_cmp);