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"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_sha1.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_gotconfig.h"
67 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
70 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
71 got_packidx_bloom_filter_cmp);
74 got_repo_get_path(struct got_repository *repo)
80 got_repo_get_path_git_dir(struct got_repository *repo)
82 return repo->path_git_dir;
86 got_repo_get_fd(struct got_repository *repo)
88 return repo->gitdir_fd;
92 got_repo_get_gitconfig_author_name(struct got_repository *repo)
94 return repo->gitconfig_author_name;
98 got_repo_get_gitconfig_author_email(struct got_repository *repo)
100 return repo->gitconfig_author_email;
104 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
106 return repo->global_gitconfig_author_name;
110 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
112 return repo->global_gitconfig_author_email;
116 got_repo_get_gitconfig_owner(struct got_repository *repo)
118 return repo->gitconfig_owner;
122 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
123 struct got_repository *repo)
125 *extensions = repo->extensions;
126 *nextensions = repo->nextensions;
130 got_repo_is_bare(struct got_repository *repo)
132 return (strcmp(repo->path, repo->path_git_dir) == 0);
136 get_path_git_child(struct got_repository *repo, const char *basename)
140 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
148 got_repo_get_path_objects(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_DIR);
154 got_repo_get_path_objects_pack(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
160 got_repo_get_path_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_REFS_DIR);
166 got_repo_get_path_packed_refs(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
172 get_path_head(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_HEAD_FILE);
178 got_repo_get_path_gitconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GITCONFIG);
184 got_repo_get_path_gotconfig(struct got_repository *repo)
186 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
189 const struct got_gotconfig *
190 got_repo_get_gotconfig(struct got_repository *repo)
192 return repo->gotconfig;
196 got_repo_get_gitconfig_remotes(int *nremotes,
197 const struct got_remote_repo **remotes, struct got_repository *repo)
199 *nremotes = repo->ngitconfig_remotes;
200 *remotes = repo->gitconfig_remotes;
204 is_git_repo(struct got_repository *repo)
206 const char *path_git = got_repo_get_path_git_dir(repo);
207 char *path_objects = got_repo_get_path_objects(repo);
208 char *path_refs = got_repo_get_path_refs(repo);
209 char *path_head = get_path_head(repo);
212 struct got_reference *head_ref;
214 if (lstat(path_git, &sb) == -1)
216 if (!S_ISDIR(sb.st_mode))
219 if (lstat(path_objects, &sb) == -1)
221 if (!S_ISDIR(sb.st_mode))
224 if (lstat(path_refs, &sb) == -1)
226 if (!S_ISDIR(sb.st_mode))
229 if (lstat(path_head, &sb) == -1)
231 if (!S_ISREG(sb.st_mode))
234 /* Check if the HEAD reference can be opened. */
235 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
237 got_ref_close(head_ref);
248 const struct got_error *
249 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
250 struct got_object *obj)
252 #ifndef GOT_NO_OBJ_CACHE
253 const struct got_error *err = NULL;
254 err = got_object_cache_add(&repo->objcache, id, obj);
256 if (err->code == GOT_ERR_OBJ_EXISTS ||
257 err->code == GOT_ERR_OBJ_TOO_LARGE)
267 got_repo_get_cached_object(struct got_repository *repo,
268 struct got_object_id *id)
270 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
273 const struct got_error *
274 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
275 struct got_tree_object *tree)
277 #ifndef GOT_NO_OBJ_CACHE
278 const struct got_error *err = NULL;
279 err = got_object_cache_add(&repo->treecache, id, tree);
281 if (err->code == GOT_ERR_OBJ_EXISTS ||
282 err->code == GOT_ERR_OBJ_TOO_LARGE)
291 struct got_tree_object *
292 got_repo_get_cached_tree(struct got_repository *repo,
293 struct got_object_id *id)
295 return (struct got_tree_object *)got_object_cache_get(
296 &repo->treecache, id);
299 const struct got_error *
300 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
301 struct got_commit_object *commit)
303 #ifndef GOT_NO_OBJ_CACHE
304 const struct got_error *err = NULL;
305 err = got_object_cache_add(&repo->commitcache, id, commit);
307 if (err->code == GOT_ERR_OBJ_EXISTS ||
308 err->code == GOT_ERR_OBJ_TOO_LARGE)
317 struct got_commit_object *
318 got_repo_get_cached_commit(struct got_repository *repo,
319 struct got_object_id *id)
321 return (struct got_commit_object *)got_object_cache_get(
322 &repo->commitcache, id);
325 const struct got_error *
326 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
327 struct got_tag_object *tag)
329 #ifndef GOT_NO_OBJ_CACHE
330 const struct got_error *err = NULL;
331 err = got_object_cache_add(&repo->tagcache, id, tag);
333 if (err->code == GOT_ERR_OBJ_EXISTS ||
334 err->code == GOT_ERR_OBJ_TOO_LARGE)
343 struct got_tag_object *
344 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
346 return (struct got_tag_object *)got_object_cache_get(
347 &repo->tagcache, id);
350 static const struct got_error *
351 open_repo(struct got_repository *repo, const char *path)
353 const struct got_error *err = NULL;
355 repo->gitdir_fd = -1;
357 /* bare git repository? */
358 repo->path_git_dir = strdup(path);
359 if (repo->path_git_dir == NULL)
360 return got_error_from_errno("strdup");
361 if (is_git_repo(repo)) {
362 repo->path = strdup(repo->path_git_dir);
363 if (repo->path == NULL) {
364 err = got_error_from_errno("strdup");
367 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
368 if (repo->gitdir_fd == -1) {
369 err = got_error_from_errno2("open",
376 /* git repository with working tree? */
377 free(repo->path_git_dir);
378 repo->path_git_dir = NULL;
379 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
380 err = got_error_from_errno("asprintf");
383 if (is_git_repo(repo)) {
384 repo->path = strdup(path);
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 err = got_error(GOT_ERR_NOT_GIT_REPO);
403 free(repo->path_git_dir);
404 repo->path_git_dir = NULL;
405 if (repo->gitdir_fd != -1)
406 close(repo->gitdir_fd);
407 repo->gitdir_fd = -1;
413 static const struct got_error *
414 parse_gitconfig_file(int *gitconfig_repository_format_version,
415 char **gitconfig_author_name, char **gitconfig_author_email,
416 struct got_remote_repo **remotes, int *nremotes,
417 char **gitconfig_owner, char ***extensions, int *nextensions,
418 const char *gitconfig_path)
420 const struct got_error *err = NULL, *child_err = NULL;
422 int imsg_fds[2] = { -1, -1 };
424 struct imsgbuf *ibuf;
426 *gitconfig_repository_format_version = 0;
431 *gitconfig_author_name = NULL;
432 *gitconfig_author_email = NULL;
438 *gitconfig_owner = NULL;
440 fd = open(gitconfig_path, O_RDONLY);
444 return got_error_from_errno2("open", gitconfig_path);
447 ibuf = calloc(1, sizeof(*ibuf));
449 err = got_error_from_errno("calloc");
453 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
454 err = got_error_from_errno("socketpair");
460 err = got_error_from_errno("fork");
462 } else if (pid == 0) {
463 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
468 if (close(imsg_fds[1]) == -1) {
469 err = got_error_from_errno("close");
473 imsg_init(ibuf, imsg_fds[0]);
475 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
480 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
484 err = got_privsep_recv_gitconfig_int(
485 gitconfig_repository_format_version, ibuf);
489 if (extensions && nextensions) {
490 err = got_privsep_send_gitconfig_repository_extensions_req(
494 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
497 if (*nextensions > 0) {
499 *extensions = calloc(*nextensions, sizeof(char *));
500 if (*extensions == NULL) {
501 err = got_error_from_errno("calloc");
504 for (i = 0; i < *nextensions; i++) {
506 err = got_privsep_recv_gitconfig_str(&ext,
510 (*extensions)[i] = ext;
515 err = got_privsep_send_gitconfig_author_name_req(ibuf);
519 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
523 err = got_privsep_send_gitconfig_author_email_req(ibuf);
527 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
531 if (remotes && nremotes) {
532 err = got_privsep_send_gitconfig_remotes_req(ibuf);
536 err = got_privsep_recv_gitconfig_remotes(remotes,
542 if (gitconfig_owner) {
543 err = got_privsep_send_gitconfig_owner_req(ibuf);
546 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
552 err = got_privsep_send_stop(imsg_fds[0]);
553 child_err = got_privsep_wait_for_child(pid);
554 if (child_err && err == NULL)
557 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
558 err = got_error_from_errno("close");
559 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
560 err = got_error_from_errno("close");
561 if (fd != -1 && close(fd) == -1 && err == NULL)
562 err = got_error_from_errno2("close", gitconfig_path);
567 static const struct got_error *
568 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
570 const struct got_error *err = NULL;
571 char *repo_gitconfig_path = NULL;
573 if (global_gitconfig_path) {
574 /* Read settings from ~/.gitconfig. */
575 int dummy_repo_version;
576 err = parse_gitconfig_file(&dummy_repo_version,
577 &repo->global_gitconfig_author_name,
578 &repo->global_gitconfig_author_email,
579 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
584 /* Read repository's .git/config file. */
585 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
586 if (repo_gitconfig_path == NULL)
587 return got_error_from_errno("got_repo_get_path_gitconfig");
589 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
590 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
591 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
592 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
593 repo_gitconfig_path);
597 free(repo_gitconfig_path);
601 static const struct got_error *
602 read_gotconfig(struct got_repository *repo)
604 const struct got_error *err = NULL;
605 char *gotconfig_path;
607 gotconfig_path = got_repo_get_path_gotconfig(repo);
608 if (gotconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gotconfig");
611 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
612 free(gotconfig_path);
616 /* Supported repository format extensions. */
617 static const char *repo_extensions[] = {
618 "noop", /* Got supports repository format version 1. */
619 "preciousObjects", /* Supported by gotadmin cleanup. */
620 "worktreeConfig", /* Got does not care about Git work trees. */
623 const struct got_error *
624 got_repo_open(struct got_repository **repop, const char *path,
625 const char *global_gitconfig_path)
627 struct got_repository *repo = NULL;
628 const struct got_error *err = NULL;
629 char *repo_path = NULL;
635 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
636 return got_error_from_errno("getrlimit");
638 repo = calloc(1, sizeof(*repo));
640 err = got_error_from_errno("calloc");
644 RB_INIT(&repo->packidx_bloom_filters);
646 for (i = 0; i < nitems(repo->privsep_children); i++) {
647 memset(&repo->privsep_children[i], 0,
648 sizeof(repo->privsep_children[0]));
649 repo->privsep_children[i].imsg_fd = -1;
652 err = got_object_cache_init(&repo->objcache,
653 GOT_OBJECT_CACHE_TYPE_OBJ);
656 err = got_object_cache_init(&repo->treecache,
657 GOT_OBJECT_CACHE_TYPE_TREE);
660 err = got_object_cache_init(&repo->commitcache,
661 GOT_OBJECT_CACHE_TYPE_COMMIT);
664 err = got_object_cache_init(&repo->tagcache,
665 GOT_OBJECT_CACHE_TYPE_TAG);
669 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
670 if (repo->pack_cache_size > rl.rlim_cur / 8)
671 repo->pack_cache_size = rl.rlim_cur / 8;
673 repo_path = realpath(path, NULL);
674 if (repo_path == NULL) {
675 err = got_error_from_errno2("realpath", path);
682 err = open_repo(repo, repo_path);
685 if (err->code != GOT_ERR_NOT_GIT_REPO)
687 if (repo_path[0] == '/' && repo_path[1] == '\0') {
688 err = got_error(GOT_ERR_NOT_GIT_REPO);
691 err = got_path_dirname(&parent_path, repo_path);
695 repo_path = parent_path;
698 err = read_gotconfig(repo);
702 err = read_gitconfig(repo, global_gitconfig_path);
705 if (repo->gitconfig_repository_format_version != 0)
706 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
707 for (i = 0; i < repo->nextensions; i++) {
708 char *ext = repo->extensions[i];
709 int j, supported = 0;
710 for (j = 0; j < nitems(repo_extensions); j++) {
711 if (strcmp(ext, repo_extensions[j]) == 0) {
717 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
723 got_repo_close(repo);
730 const struct got_error *
731 got_repo_close(struct got_repository *repo)
733 const struct got_error *err = NULL, *child_err;
734 struct got_packidx_bloom_filter *bf;
737 for (i = 0; i < repo->pack_cache_size; i++) {
738 if (repo->packidx_cache[i] == NULL)
740 got_packidx_close(repo->packidx_cache[i]);
743 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
744 &repo->packidx_bloom_filters))) {
745 RB_REMOVE(got_packidx_bloom_filter_tree,
746 &repo->packidx_bloom_filters, bf);
751 for (i = 0; i < repo->pack_cache_size; i++) {
752 if (repo->packs[i].path_packfile == NULL)
754 got_pack_close(&repo->packs[i]);
758 free(repo->path_git_dir);
760 got_object_cache_close(&repo->objcache);
761 got_object_cache_close(&repo->treecache);
762 got_object_cache_close(&repo->commitcache);
763 got_object_cache_close(&repo->tagcache);
765 for (i = 0; i < nitems(repo->privsep_children); i++) {
766 if (repo->privsep_children[i].imsg_fd == -1)
768 imsg_clear(repo->privsep_children[i].ibuf);
769 free(repo->privsep_children[i].ibuf);
770 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
771 child_err = got_privsep_wait_for_child(
772 repo->privsep_children[i].pid);
773 if (child_err && err == NULL)
775 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
777 err = got_error_from_errno("close");
780 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
782 err = got_error_from_errno("close");
785 got_gotconfig_free(repo->gotconfig);
786 free(repo->gitconfig_author_name);
787 free(repo->gitconfig_author_email);
788 for (i = 0; i < repo->ngitconfig_remotes; i++)
789 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
790 free(repo->gitconfig_remotes);
791 for (i = 0; i < repo->nextensions; i++)
792 free(repo->extensions[i]);
793 free(repo->extensions);
800 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
806 free(repo->fetch_url);
807 repo->fetch_url = NULL;
808 free(repo->send_url);
809 repo->send_url = NULL;
810 for (i = 0; i < repo->nfetch_branches; i++)
811 free(repo->fetch_branches[i]);
812 free(repo->fetch_branches);
813 repo->fetch_branches = NULL;
814 repo->nfetch_branches = 0;
815 for (i = 0; i < repo->nsend_branches; i++)
816 free(repo->send_branches[i]);
817 free(repo->send_branches);
818 repo->send_branches = NULL;
819 repo->nsend_branches = 0;
822 const struct got_error *
823 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
824 const char *input_path)
826 const struct got_error *err = NULL;
827 const char *repo_abspath = NULL;
829 char *canonpath, *path = NULL;
831 *in_repo_path = NULL;
833 canonpath = strdup(input_path);
834 if (canonpath == NULL) {
835 err = got_error_from_errno("strdup");
838 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
842 repo_abspath = got_repo_get_path(repo);
844 if (canonpath[0] == '\0') {
845 path = strdup(canonpath);
847 err = got_error_from_errno("strdup");
851 path = realpath(canonpath, NULL);
853 if (errno != ENOENT) {
854 err = got_error_from_errno2("realpath",
859 * Path is not on disk.
860 * Assume it is already relative to repository root.
862 path = strdup(canonpath);
864 err = got_error_from_errno("strdup");
869 repolen = strlen(repo_abspath);
873 if (strcmp(path, repo_abspath) == 0) {
877 err = got_error_from_errno("strdup");
880 } else if (len > repolen &&
881 got_path_is_child(path, repo_abspath, repolen)) {
882 /* Matched an on-disk path inside repository. */
883 if (got_repo_is_bare(repo)) {
885 * Matched an on-disk path inside repository
886 * database. Treat input as repository-relative.
893 /* Strip common prefix with repository path. */
894 err = got_path_skip_common_ancestor(&child,
903 * Matched unrelated on-disk path.
904 * Treat input as repository-relative.
912 /* Make in-repository path absolute */
913 if (path[0] != '/') {
915 if (asprintf(&abspath, "/%s", path) == -1) {
916 err = got_error_from_errno("asprintf");
928 *in_repo_path = path;
932 static const struct got_error *
933 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
934 const char *path_packidx)
936 const struct got_error *err = NULL;
939 for (i = 0; i < repo->pack_cache_size; i++) {
940 if (repo->packidx_cache[i] == NULL)
942 if (strcmp(repo->packidx_cache[i]->path_packidx,
943 path_packidx) == 0) {
944 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
947 if (i == repo->pack_cache_size) {
948 i = repo->pack_cache_size - 1;
949 err = got_packidx_close(repo->packidx_cache[i]);
954 repo->packidx_cache[i] = packidx;
960 got_repo_is_packidx_filename(const char *name, size_t len)
962 if (len != GOT_PACKIDX_NAMELEN)
965 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
968 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
969 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
975 static struct got_packidx_bloom_filter *
976 get_packidx_bloom_filter(struct got_repository *repo,
977 const char *path, size_t path_len)
979 struct got_packidx_bloom_filter key;
981 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
982 return NULL; /* XXX */
983 key.path_len = path_len;
985 return RB_FIND(got_packidx_bloom_filter_tree,
986 &repo->packidx_bloom_filters, &key);
990 check_packidx_bloom_filter(struct got_repository *repo,
991 const char *path_packidx, struct got_object_id *id)
993 struct got_packidx_bloom_filter *bf;
995 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
997 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
999 /* No bloom filter means this pack index must be searched. */
1003 static const struct got_error *
1004 add_packidx_bloom_filter(struct got_repository *repo,
1005 struct got_packidx *packidx, const char *path_packidx)
1007 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1008 struct got_packidx_bloom_filter *bf;
1012 * Don't use bloom filters for very large pack index files.
1013 * Large pack files will contain a relatively large fraction
1014 * of our objects so we will likely need to visit them anyway.
1015 * The more objects a pack file contains the higher the probability
1016 * of a false-positive match from the bloom filter. And reading
1017 * all object IDs from a large pack index file can be expensive.
1019 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1022 /* Do we already have a filter for this pack index? */
1023 if (get_packidx_bloom_filter(repo, path_packidx,
1024 strlen(path_packidx)) != NULL)
1027 bf = calloc(1, sizeof(*bf));
1029 return got_error_from_errno("calloc");
1030 bf->bloom = calloc(1, sizeof(*bf->bloom));
1031 if (bf->bloom == NULL) {
1033 return got_error_from_errno("calloc");
1036 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1037 if (len >= sizeof(bf->path)) {
1040 return got_error(GOT_ERR_NO_SPACE);
1044 /* Minimum size supported by our bloom filter is 1000 entries. */
1045 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1046 for (i = 0; i < nobjects; i++) {
1047 struct got_packidx_object_id *id;
1048 id = &packidx->hdr.sorted_ids[i];
1049 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1052 RB_INSERT(got_packidx_bloom_filter_tree,
1053 &repo->packidx_bloom_filters, bf);
1057 const struct got_error *
1058 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1059 struct got_repository *repo, struct got_object_id *id)
1061 const struct got_error *err;
1062 DIR *packdir = NULL;
1063 struct dirent *dent;
1068 /* Search pack index cache. */
1069 for (i = 0; i < repo->pack_cache_size; i++) {
1070 if (repo->packidx_cache[i] == NULL)
1072 if (!check_packidx_bloom_filter(repo,
1073 repo->packidx_cache[i]->path_packidx, id))
1074 continue; /* object will not be found in this index */
1075 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1077 *packidx = repo->packidx_cache[i];
1079 * Move this cache entry to the front. Repeatedly
1080 * searching a wrong pack index can be expensive.
1083 memmove(&repo->packidx_cache[1],
1084 &repo->packidx_cache[0],
1085 i * sizeof(repo->packidx_cache[0]));
1086 repo->packidx_cache[0] = *packidx;
1091 /* No luck. Search the filesystem. */
1093 packdir_fd = openat(got_repo_get_fd(repo),
1094 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1095 if (packdir_fd == -1) {
1096 if (errno == ENOENT)
1097 err = got_error_no_obj(id);
1099 err = got_error_from_errno_fmt("openat: %s/%s",
1100 got_repo_get_path_git_dir(repo),
1101 GOT_OBJECTS_PACK_DIR);
1105 packdir = fdopendir(packdir_fd);
1106 if (packdir == NULL) {
1107 err = got_error_from_errno("fdopendir");
1111 while ((dent = readdir(packdir)) != NULL) {
1114 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1117 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1118 dent->d_name) == -1) {
1119 err = got_error_from_errno("asprintf");
1123 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1125 continue; /* object will not be found in this index */
1128 for (i = 0; i < repo->pack_cache_size; i++) {
1129 if (repo->packidx_cache[i] == NULL)
1131 if (strcmp(repo->packidx_cache[i]->path_packidx,
1132 path_packidx) == 0) {
1139 continue; /* already searched */
1142 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1149 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1155 err = cache_packidx(repo, *packidx, path_packidx);
1160 *idx = got_packidx_get_object_idx(*packidx, id);
1162 err = NULL; /* found the object */
1167 err = got_error_no_obj(id);
1169 if (packdir && closedir(packdir) != 0 && err == NULL)
1170 err = got_error_from_errno("closedir");
1174 static const struct got_error *
1175 read_packfile_hdr(int fd, struct got_packidx *packidx)
1177 const struct got_error *err = NULL;
1178 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1179 struct got_packfile_hdr hdr;
1182 n = read(fd, &hdr, sizeof(hdr));
1184 return got_error_from_errno("read");
1185 if (n != sizeof(hdr))
1186 return got_error(GOT_ERR_BAD_PACKFILE);
1188 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1189 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1190 be32toh(hdr.nobjects) != totobj)
1191 err = got_error(GOT_ERR_BAD_PACKFILE);
1196 static const struct got_error *
1197 open_packfile(int *fd, struct got_repository *repo,
1198 const char *relpath, struct got_packidx *packidx)
1200 const struct got_error *err = NULL;
1202 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1204 return got_error_from_errno_fmt("openat: %s/%s",
1205 got_repo_get_path_git_dir(repo), relpath);
1208 err = read_packfile_hdr(*fd, packidx);
1218 const struct got_error *
1219 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1220 const char *path_packfile, struct got_packidx *packidx)
1222 const struct got_error *err = NULL;
1223 struct got_pack *pack = NULL;
1230 for (i = 0; i < repo->pack_cache_size; i++) {
1231 pack = &repo->packs[i];
1232 if (pack->path_packfile == NULL)
1234 if (strcmp(pack->path_packfile, path_packfile) == 0)
1235 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1238 if (i == repo->pack_cache_size) {
1239 err = got_pack_close(&repo->packs[i - 1]);
1242 memmove(&repo->packs[1], &repo->packs[0],
1243 sizeof(repo->packs) - sizeof(repo->packs[0]));
1247 pack = &repo->packs[i];
1249 pack->path_packfile = strdup(path_packfile);
1250 if (pack->path_packfile == NULL) {
1251 err = got_error_from_errno("strdup");
1255 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1259 if (fstat(pack->fd, &sb) != 0) {
1260 err = got_error_from_errno("fstat");
1263 pack->filesize = sb.st_size;
1265 pack->privsep_child = NULL;
1267 #ifndef GOT_PACK_NO_MMAP
1268 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1270 if (pack->map == MAP_FAILED) {
1271 if (errno != ENOMEM) {
1272 err = got_error_from_errno("mmap");
1275 pack->map = NULL; /* fall back to read(2) */
1281 free(pack->path_packfile);
1282 memset(pack, 0, sizeof(*pack));
1290 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1292 struct got_pack *pack = NULL;
1295 for (i = 0; i < repo->pack_cache_size; i++) {
1296 pack = &repo->packs[i];
1297 if (pack->path_packfile == NULL)
1299 if (strcmp(pack->path_packfile, path_packfile) == 0)
1306 const struct got_error *
1307 got_repo_init(const char *repo_path)
1309 const struct got_error *err = NULL;
1310 const char *dirnames[] = {
1312 GOT_OBJECTS_PACK_DIR,
1315 const char *description_str = "Unnamed repository; "
1316 "edit this file 'description' to name the repository.";
1317 const char *headref_str = "ref: refs/heads/main";
1318 const char *gitconfig_str = "[core]\n"
1319 "\trepositoryformatversion = 0\n"
1320 "\tfilemode = true\n"
1325 if (!got_path_dir_is_empty(repo_path))
1326 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1328 for (i = 0; i < nitems(dirnames); i++) {
1329 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1330 return got_error_from_errno("asprintf");
1332 err = got_path_mkdir(path);
1338 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1339 return got_error_from_errno("asprintf");
1340 err = got_path_create_file(path, description_str);
1345 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1346 return got_error_from_errno("asprintf");
1347 err = got_path_create_file(path, headref_str);
1352 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1353 return got_error_from_errno("asprintf");
1354 err = got_path_create_file(path, gitconfig_str);
1362 static const struct got_error *
1363 match_packed_object(struct got_object_id **unique_id,
1364 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1366 const struct got_error *err = NULL;
1367 DIR *packdir = NULL;
1368 struct dirent *dent;
1370 struct got_object_id_queue matched_ids;
1373 STAILQ_INIT(&matched_ids);
1375 packdir_fd = openat(got_repo_get_fd(repo),
1376 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1377 if (packdir_fd == -1) {
1378 if (errno != ENOENT)
1379 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1383 packdir = fdopendir(packdir_fd);
1384 if (packdir == NULL) {
1385 err = got_error_from_errno("fdopendir");
1389 while ((dent = readdir(packdir)) != NULL) {
1390 struct got_packidx *packidx;
1391 struct got_object_qid *qid;
1393 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1396 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1397 dent->d_name) == -1) {
1398 err = got_error_from_errno("strdup");
1402 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1408 err = got_packidx_match_id_str_prefix(&matched_ids,
1409 packidx, id_str_prefix);
1411 got_packidx_close(packidx);
1414 err = got_packidx_close(packidx);
1418 STAILQ_FOREACH(qid, &matched_ids, entry) {
1419 if (obj_type != GOT_OBJ_TYPE_ANY) {
1421 err = got_object_get_type(&matched_type, repo,
1425 if (matched_type != obj_type)
1428 if (*unique_id == NULL) {
1429 *unique_id = got_object_id_dup(qid->id);
1430 if (*unique_id == NULL) {
1431 err = got_error_from_errno("malloc");
1435 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1436 continue; /* packed multiple times */
1437 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1443 got_object_id_queue_free(&matched_ids);
1444 if (packdir && closedir(packdir) != 0 && err == NULL)
1445 err = got_error_from_errno("closedir");
1453 static const struct got_error *
1454 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1455 const char *object_dir, const char *id_str_prefix, int obj_type,
1456 struct got_repository *repo)
1458 const struct got_error *err = NULL;
1461 struct dirent *dent;
1462 struct got_object_id id;
1464 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1465 err = got_error_from_errno("asprintf");
1469 dir = opendir(path);
1471 if (errno == ENOENT) {
1475 err = got_error_from_errno2("opendir", path);
1478 while ((dent = readdir(dir)) != NULL) {
1482 if (strcmp(dent->d_name, ".") == 0 ||
1483 strcmp(dent->d_name, "..") == 0)
1486 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1487 err = got_error_from_errno("asprintf");
1491 if (!got_parse_sha1_digest(id.sha1, id_str))
1495 * Directory entries do not necessarily appear in
1496 * sorted order, so we must iterate over all of them.
1498 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1504 if (*unique_id == NULL) {
1505 if (obj_type != GOT_OBJ_TYPE_ANY) {
1507 err = got_object_get_type(&matched_type, repo,
1511 if (matched_type != obj_type)
1514 *unique_id = got_object_id_dup(&id);
1515 if (*unique_id == NULL) {
1516 err = got_error_from_errno("got_object_id_dup");
1521 if (got_object_id_cmp(*unique_id, &id) == 0)
1522 continue; /* both packed and loose */
1523 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1529 if (dir && closedir(dir) != 0 && err == NULL)
1530 err = got_error_from_errno("closedir");
1539 const struct got_error *
1540 got_repo_match_object_id_prefix(struct got_object_id **id,
1541 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1543 const struct got_error *err = NULL;
1544 char *path_objects = got_repo_get_path_objects(repo);
1545 char *object_dir = NULL;
1551 for (i = 0; i < strlen(id_str_prefix); i++) {
1552 if (isxdigit((unsigned char)id_str_prefix[i]))
1554 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1557 len = strlen(id_str_prefix);
1559 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1562 object_dir = strndup(id_str_prefix, 2);
1563 if (object_dir == NULL) {
1564 err = got_error_from_errno("strdup");
1567 err = match_loose_object(id, path_objects, object_dir,
1568 id_str_prefix, obj_type, repo);
1569 } else if (len == 1) {
1571 for (i = 0; i < 0xf; i++) {
1572 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1574 err = got_error_from_errno("asprintf");
1577 err = match_packed_object(id, repo, object_dir,
1581 err = match_loose_object(id, path_objects, object_dir,
1582 id_str_prefix, obj_type, repo);
1587 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1595 } else if (*id == NULL) {
1597 case GOT_OBJ_TYPE_BLOB:
1598 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1599 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1601 case GOT_OBJ_TYPE_TREE:
1602 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1603 GOT_OBJ_LABEL_TREE, id_str_prefix);
1605 case GOT_OBJ_TYPE_COMMIT:
1606 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1607 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1609 case GOT_OBJ_TYPE_TAG:
1610 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1611 GOT_OBJ_LABEL_TAG, id_str_prefix);
1614 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1622 const struct got_error *
1623 got_repo_match_object_id(struct got_object_id **id, char **label,
1624 const char *id_str, int obj_type, struct got_reflist_head *refs,
1625 struct got_repository *repo)
1627 const struct got_error *err;
1628 struct got_tag_object *tag;
1629 struct got_reference *ref = NULL;
1636 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1639 *id = got_object_id_dup(
1640 got_object_tag_get_object_id(tag));
1642 err = got_error_from_errno("got_object_id_dup");
1643 else if (label && asprintf(label, "refs/tags/%s",
1644 got_object_tag_get_name(tag)) == -1) {
1645 err = got_error_from_errno("asprintf");
1649 got_object_tag_close(tag);
1651 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1652 err->code != GOT_ERR_NO_OBJ)
1656 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1658 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1660 err = got_ref_open(&ref, repo, id_str, 0);
1664 *label = strdup(got_ref_get_name(ref));
1665 if (*label == NULL) {
1666 err = got_error_from_errno("strdup");
1670 err = got_ref_resolve(id, repo, ref);
1672 err = got_object_id_str(label, *id);
1673 if (*label == NULL) {
1674 err = got_error_from_errno("strdup");
1684 const struct got_error *
1685 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1686 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1688 const struct got_error *err = NULL;
1689 struct got_reflist_entry *re;
1690 struct got_object_id *tag_id;
1691 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1695 TAILQ_FOREACH(re, refs, entry) {
1696 const char *refname;
1697 refname = got_ref_get_name(re->ref);
1698 if (got_ref_is_symbolic(re->ref))
1700 if (strncmp(refname, "refs/tags/", 10) != 0)
1702 if (!name_is_absolute)
1703 refname += strlen("refs/tags/");
1704 if (strcmp(refname, name) != 0)
1706 err = got_ref_resolve(&tag_id, repo, re->ref);
1709 err = got_object_open_as_tag(tag, repo, tag_id);
1713 if (obj_type == GOT_OBJ_TYPE_ANY ||
1714 got_object_tag_get_object_type(*tag) == obj_type)
1716 got_object_tag_close(*tag);
1720 if (err == NULL && *tag == NULL)
1721 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1722 GOT_OBJ_LABEL_TAG, name);
1726 static const struct got_error *
1727 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1728 const char *name, mode_t mode, struct got_object_id *blob_id)
1730 const struct got_error *err = NULL;
1734 *new_te = calloc(1, sizeof(**new_te));
1735 if (*new_te == NULL)
1736 return got_error_from_errno("calloc");
1738 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1739 sizeof((*new_te)->name)) {
1740 err = got_error(GOT_ERR_NO_SPACE);
1744 if (S_ISLNK(mode)) {
1745 (*new_te)->mode = S_IFLNK;
1747 (*new_te)->mode = S_IFREG;
1748 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1750 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1752 if (err && *new_te) {
1759 static const struct got_error *
1760 import_file(struct got_tree_entry **new_te, struct dirent *de,
1761 const char *path, struct got_repository *repo)
1763 const struct got_error *err;
1764 struct got_object_id *blob_id = NULL;
1768 if (asprintf(&filepath, "%s%s%s", path,
1769 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1770 return got_error_from_errno("asprintf");
1772 if (lstat(filepath, &sb) != 0) {
1773 err = got_error_from_errno2("lstat", path);
1777 err = got_object_blob_create(&blob_id, filepath, repo);
1781 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1790 static const struct got_error *
1791 insert_tree_entry(struct got_tree_entry *new_te,
1792 struct got_pathlist_head *paths)
1794 const struct got_error *err = NULL;
1795 struct got_pathlist_entry *new_pe;
1797 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1801 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1805 static const struct got_error *write_tree(struct got_object_id **,
1806 const char *, struct got_pathlist_head *, struct got_repository *,
1807 got_repo_import_cb progress_cb, void *progress_arg);
1809 static const struct got_error *
1810 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1811 const char *path, struct got_pathlist_head *ignores,
1812 struct got_repository *repo,
1813 got_repo_import_cb progress_cb, void *progress_arg)
1815 const struct got_error *err;
1816 struct got_object_id *id = NULL;
1819 if (asprintf(&subdirpath, "%s%s%s", path,
1820 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1821 return got_error_from_errno("asprintf");
1823 (*new_te) = calloc(1, sizeof(**new_te));
1824 if (*new_te == NULL)
1825 return got_error_from_errno("calloc");
1826 (*new_te)->mode = S_IFDIR;
1827 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1828 sizeof((*new_te)->name)) {
1829 err = got_error(GOT_ERR_NO_SPACE);
1832 err = write_tree(&id, subdirpath, ignores, repo,
1833 progress_cb, progress_arg);
1836 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1848 static const struct got_error *
1849 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1850 struct got_pathlist_head *ignores, struct got_repository *repo,
1851 got_repo_import_cb progress_cb, void *progress_arg)
1853 const struct got_error *err = NULL;
1857 struct got_tree_entry *new_te = NULL;
1858 struct got_pathlist_head paths;
1859 struct got_pathlist_entry *pe;
1861 *new_tree_id = NULL;
1865 dir = opendir(path_dir);
1867 err = got_error_from_errno2("opendir", path_dir);
1872 while ((de = readdir(dir)) != NULL) {
1876 if (strcmp(de->d_name, ".") == 0 ||
1877 strcmp(de->d_name, "..") == 0)
1880 TAILQ_FOREACH(pe, ignores, entry) {
1881 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1889 err = got_path_dirent_type(&type, path_dir, de);
1893 if (type == DT_DIR) {
1894 err = import_subdir(&new_te, de, path_dir,
1895 ignores, repo, progress_cb, progress_arg);
1897 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1902 } else if (type == DT_REG || type == DT_LNK) {
1903 err = import_file(&new_te, de, path_dir, repo);
1909 err = insert_tree_entry(new_te, &paths);
1915 if (TAILQ_EMPTY(&paths)) {
1916 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1917 "cannot create tree without any entries");
1921 TAILQ_FOREACH(pe, &paths, entry) {
1922 struct got_tree_entry *te = pe->data;
1924 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1926 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1927 err = got_error_from_errno("asprintf");
1930 err = (*progress_cb)(progress_arg, path);
1936 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1940 got_pathlist_free(&paths);
1944 const struct got_error *
1945 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1946 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1947 struct got_repository *repo, got_repo_import_cb progress_cb,
1950 const struct got_error *err;
1951 struct got_object_id *new_tree_id;
1953 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1954 progress_cb, progress_arg);
1958 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1959 author, time(NULL), author, time(NULL), logmsg, repo);
1964 const struct got_error *
1965 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1966 struct got_repository *repo)
1968 const struct got_error *err = NULL;
1969 char *path_objects = NULL, *path = NULL;
1971 struct got_object_id id;
1977 path_objects = got_repo_get_path_objects(repo);
1978 if (path_objects == NULL)
1979 return got_error_from_errno("got_repo_get_path_objects");
1981 for (i = 0; i <= 0xff; i++) {
1982 struct dirent *dent;
1984 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1985 err = got_error_from_errno("asprintf");
1989 dir = opendir(path);
1991 if (errno == ENOENT) {
1995 err = got_error_from_errno2("opendir", path);
1999 while ((dent = readdir(dir)) != NULL) {
2004 if (strcmp(dent->d_name, ".") == 0 ||
2005 strcmp(dent->d_name, "..") == 0)
2008 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2009 err = got_error_from_errno("asprintf");
2013 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2019 err = got_object_open_loose_fd(&fd, &id, repo);
2023 if (fstat(fd, &sb) == -1) {
2024 err = got_error_from_errno("fstat");
2029 (*ondisk_size) += sb.st_size;
2031 if (close(fd) == -1) {
2032 err = got_error_from_errno("close");
2037 if (closedir(dir) != 0) {
2038 err = got_error_from_errno("closedir");
2047 if (dir && closedir(dir) != 0 && err == NULL)
2048 err = got_error_from_errno("closedir");
2059 const struct got_error *
2060 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2061 off_t *total_packsize, struct got_repository *repo)
2063 const struct got_error *err = NULL;
2064 DIR *packdir = NULL;
2065 struct dirent *dent;
2066 struct got_packidx *packidx = NULL;
2068 char *path_packfile;
2074 *total_packsize = 0;
2076 packdir_fd = openat(got_repo_get_fd(repo),
2077 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2078 if (packdir_fd == -1) {
2079 return got_error_from_errno_fmt("openat: %s/%s",
2080 got_repo_get_path_git_dir(repo),
2081 GOT_OBJECTS_PACK_DIR);
2084 packdir = fdopendir(packdir_fd);
2085 if (packdir == NULL) {
2086 err = got_error_from_errno("fdopendir");
2090 while ((dent = readdir(packdir)) != NULL) {
2091 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2094 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2095 dent->d_name) == -1) {
2096 err = got_error_from_errno("asprintf");
2100 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2106 if (fstat(packidx->fd, &sb) == -1)
2108 *total_packsize += sb.st_size;
2110 err = got_packidx_get_packfile_path(&path_packfile,
2111 packidx->path_packidx);
2115 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2117 free(path_packfile);
2120 free(path_packfile);
2121 *total_packsize += sb.st_size;
2123 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2127 got_packidx_close(packidx);
2132 got_packidx_close(packidx);
2133 if (packdir && closedir(packdir) != 0 && err == NULL)
2134 err = got_error_from_errno("closedir");
2138 *total_packsize = 0;
2143 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2144 got_packidx_bloom_filter_cmp);