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>
20 #include <sys/socket.h>
23 #include <sys/resource.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_object.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_gotconfig.h"
66 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
70 got_repo_get_path(struct got_repository *repo)
76 got_repo_get_path_git_dir(struct got_repository *repo)
78 return repo->path_git_dir;
82 got_repo_get_fd(struct got_repository *repo)
84 return repo->gitdir_fd;
88 got_repo_get_gitconfig_author_name(struct got_repository *repo)
90 return repo->gitconfig_author_name;
94 got_repo_get_gitconfig_author_email(struct got_repository *repo)
96 return repo->gitconfig_author_email;
100 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
102 return repo->global_gitconfig_author_name;
106 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
108 return repo->global_gitconfig_author_email;
112 got_repo_get_gitconfig_owner(struct got_repository *repo)
114 return repo->gitconfig_owner;
118 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
119 struct got_repository *repo)
121 *extensions = repo->extensions;
122 *nextensions = repo->nextensions;
126 got_repo_is_bare(struct got_repository *repo)
128 return (strcmp(repo->path, repo->path_git_dir) == 0);
132 get_path_git_child(struct got_repository *repo, const char *basename)
136 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 got_repo_get_path_objects(struct got_repository *repo)
146 return get_path_git_child(repo, GOT_OBJECTS_DIR);
150 got_repo_get_path_objects_pack(struct got_repository *repo)
152 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
156 got_repo_get_path_refs(struct got_repository *repo)
158 return get_path_git_child(repo, GOT_REFS_DIR);
162 got_repo_get_path_packed_refs(struct got_repository *repo)
164 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
168 get_path_head(struct got_repository *repo)
170 return get_path_git_child(repo, GOT_HEAD_FILE);
174 got_repo_get_path_gitconfig(struct got_repository *repo)
176 return get_path_git_child(repo, GOT_GITCONFIG);
180 got_repo_get_path_gotconfig(struct got_repository *repo)
182 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
185 const struct got_gotconfig *
186 got_repo_get_gotconfig(struct got_repository *repo)
188 return repo->gotconfig;
192 got_repo_get_gitconfig_remotes(int *nremotes,
193 const struct got_remote_repo **remotes, struct got_repository *repo)
195 *nremotes = repo->ngitconfig_remotes;
196 *remotes = repo->gitconfig_remotes;
200 is_git_repo(struct got_repository *repo)
202 const char *path_git = got_repo_get_path_git_dir(repo);
203 char *path_objects = got_repo_get_path_objects(repo);
204 char *path_refs = got_repo_get_path_refs(repo);
205 char *path_head = get_path_head(repo);
208 struct got_reference *head_ref;
210 if (lstat(path_git, &sb) == -1)
212 if (!S_ISDIR(sb.st_mode))
215 if (lstat(path_objects, &sb) == -1)
217 if (!S_ISDIR(sb.st_mode))
220 if (lstat(path_refs, &sb) == -1)
222 if (!S_ISDIR(sb.st_mode))
225 if (lstat(path_head, &sb) == -1)
227 if (!S_ISREG(sb.st_mode))
230 /* Check if the HEAD reference can be opened. */
231 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
233 got_ref_close(head_ref);
244 const struct got_error *
245 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
246 struct got_object *obj)
248 #ifndef GOT_NO_OBJ_CACHE
249 const struct got_error *err = NULL;
250 err = got_object_cache_add(&repo->objcache, id, obj);
252 if (err->code == GOT_ERR_OBJ_EXISTS ||
253 err->code == GOT_ERR_OBJ_TOO_LARGE)
263 got_repo_get_cached_object(struct got_repository *repo,
264 struct got_object_id *id)
266 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
269 const struct got_error *
270 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
271 struct got_tree_object *tree)
273 #ifndef GOT_NO_OBJ_CACHE
274 const struct got_error *err = NULL;
275 err = got_object_cache_add(&repo->treecache, id, tree);
277 if (err->code == GOT_ERR_OBJ_EXISTS ||
278 err->code == GOT_ERR_OBJ_TOO_LARGE)
287 struct got_tree_object *
288 got_repo_get_cached_tree(struct got_repository *repo,
289 struct got_object_id *id)
291 return (struct got_tree_object *)got_object_cache_get(
292 &repo->treecache, id);
295 const struct got_error *
296 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
297 struct got_commit_object *commit)
299 #ifndef GOT_NO_OBJ_CACHE
300 const struct got_error *err = NULL;
301 err = got_object_cache_add(&repo->commitcache, id, commit);
303 if (err->code == GOT_ERR_OBJ_EXISTS ||
304 err->code == GOT_ERR_OBJ_TOO_LARGE)
313 struct got_commit_object *
314 got_repo_get_cached_commit(struct got_repository *repo,
315 struct got_object_id *id)
317 return (struct got_commit_object *)got_object_cache_get(
318 &repo->commitcache, id);
321 const struct got_error *
322 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
323 struct got_tag_object *tag)
325 #ifndef GOT_NO_OBJ_CACHE
326 const struct got_error *err = NULL;
327 err = got_object_cache_add(&repo->tagcache, id, tag);
329 if (err->code == GOT_ERR_OBJ_EXISTS ||
330 err->code == GOT_ERR_OBJ_TOO_LARGE)
339 struct got_tag_object *
340 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
342 return (struct got_tag_object *)got_object_cache_get(
343 &repo->tagcache, id);
346 static const struct got_error *
347 open_repo(struct got_repository *repo, const char *path)
349 const struct got_error *err = NULL;
351 repo->gitdir_fd = -1;
353 /* bare git repository? */
354 repo->path_git_dir = strdup(path);
355 if (repo->path_git_dir == NULL)
356 return got_error_from_errno("strdup");
357 if (is_git_repo(repo)) {
358 repo->path = strdup(repo->path_git_dir);
359 if (repo->path == NULL) {
360 err = got_error_from_errno("strdup");
363 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
364 if (repo->gitdir_fd == -1) {
365 err = got_error_from_errno2("open",
372 /* git repository with working tree? */
373 free(repo->path_git_dir);
374 repo->path_git_dir = NULL;
375 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
376 err = got_error_from_errno("asprintf");
379 if (is_git_repo(repo)) {
380 repo->path = strdup(path);
381 if (repo->path == NULL) {
382 err = got_error_from_errno("strdup");
385 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
386 if (repo->gitdir_fd == -1) {
387 err = got_error_from_errno2("open",
394 err = got_error(GOT_ERR_NOT_GIT_REPO);
399 free(repo->path_git_dir);
400 repo->path_git_dir = NULL;
401 if (repo->gitdir_fd != -1)
402 close(repo->gitdir_fd);
403 repo->gitdir_fd = -1;
409 static const struct got_error *
410 parse_gitconfig_file(int *gitconfig_repository_format_version,
411 char **gitconfig_author_name, char **gitconfig_author_email,
412 struct got_remote_repo **remotes, int *nremotes,
413 char **gitconfig_owner, char ***extensions, int *nextensions,
414 const char *gitconfig_path)
416 const struct got_error *err = NULL, *child_err = NULL;
418 int imsg_fds[2] = { -1, -1 };
420 struct imsgbuf *ibuf;
422 *gitconfig_repository_format_version = 0;
427 *gitconfig_author_name = NULL;
428 *gitconfig_author_email = NULL;
434 *gitconfig_owner = NULL;
436 fd = open(gitconfig_path, O_RDONLY);
440 return got_error_from_errno2("open", gitconfig_path);
443 ibuf = calloc(1, sizeof(*ibuf));
445 err = got_error_from_errno("calloc");
449 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
450 err = got_error_from_errno("socketpair");
456 err = got_error_from_errno("fork");
458 } else if (pid == 0) {
459 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
464 if (close(imsg_fds[1]) == -1) {
465 err = got_error_from_errno("close");
469 imsg_init(ibuf, imsg_fds[0]);
471 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
476 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
480 err = got_privsep_recv_gitconfig_int(
481 gitconfig_repository_format_version, ibuf);
485 if (extensions && nextensions) {
486 err = got_privsep_send_gitconfig_repository_extensions_req(
490 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
493 if (*nextensions > 0) {
495 *extensions = calloc(*nextensions, sizeof(char *));
496 if (*extensions == NULL) {
497 err = got_error_from_errno("calloc");
500 for (i = 0; i < *nextensions; i++) {
502 err = got_privsep_recv_gitconfig_str(&ext,
506 (*extensions)[i] = ext;
511 err = got_privsep_send_gitconfig_author_name_req(ibuf);
515 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
519 err = got_privsep_send_gitconfig_author_email_req(ibuf);
523 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
527 if (remotes && nremotes) {
528 err = got_privsep_send_gitconfig_remotes_req(ibuf);
532 err = got_privsep_recv_gitconfig_remotes(remotes,
538 if (gitconfig_owner) {
539 err = got_privsep_send_gitconfig_owner_req(ibuf);
542 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
548 err = got_privsep_send_stop(imsg_fds[0]);
549 child_err = got_privsep_wait_for_child(pid);
550 if (child_err && err == NULL)
553 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
554 err = got_error_from_errno("close");
555 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
556 err = got_error_from_errno("close");
557 if (fd != -1 && close(fd) == -1 && err == NULL)
558 err = got_error_from_errno2("close", gitconfig_path);
563 static const struct got_error *
564 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
566 const struct got_error *err = NULL;
567 char *repo_gitconfig_path = NULL;
569 if (global_gitconfig_path) {
570 /* Read settings from ~/.gitconfig. */
571 int dummy_repo_version;
572 err = parse_gitconfig_file(&dummy_repo_version,
573 &repo->global_gitconfig_author_name,
574 &repo->global_gitconfig_author_email,
575 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
580 /* Read repository's .git/config file. */
581 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
582 if (repo_gitconfig_path == NULL)
583 return got_error_from_errno("got_repo_get_path_gitconfig");
585 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
586 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
587 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
588 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
589 repo_gitconfig_path);
593 free(repo_gitconfig_path);
597 static const struct got_error *
598 read_gotconfig(struct got_repository *repo)
600 const struct got_error *err = NULL;
601 char *gotconfig_path;
603 gotconfig_path = got_repo_get_path_gotconfig(repo);
604 if (gotconfig_path == NULL)
605 return got_error_from_errno("got_repo_get_path_gotconfig");
607 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
608 free(gotconfig_path);
612 /* Supported repository format extensions. */
613 static const char *repo_extensions[] = {
614 "noop", /* Got supports repository format version 1. */
615 "preciousObjects", /* Supported by gotadmin cleanup. */
616 "worktreeConfig", /* Got does not care about Git work trees. */
619 const struct got_error *
620 got_repo_open(struct got_repository **repop, const char *path,
621 const char *global_gitconfig_path)
623 struct got_repository *repo = NULL;
624 const struct got_error *err = NULL;
625 char *repo_path = NULL;
631 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
632 return got_error_from_errno("getrlimit");
634 repo = calloc(1, sizeof(*repo));
636 err = got_error_from_errno("calloc");
640 STAILQ_INIT(&repo->packidx_bloom_filters);
642 for (i = 0; i < nitems(repo->privsep_children); i++) {
643 memset(&repo->privsep_children[i], 0,
644 sizeof(repo->privsep_children[0]));
645 repo->privsep_children[i].imsg_fd = -1;
648 err = got_object_cache_init(&repo->objcache,
649 GOT_OBJECT_CACHE_TYPE_OBJ);
652 err = got_object_cache_init(&repo->treecache,
653 GOT_OBJECT_CACHE_TYPE_TREE);
656 err = got_object_cache_init(&repo->commitcache,
657 GOT_OBJECT_CACHE_TYPE_COMMIT);
660 err = got_object_cache_init(&repo->tagcache,
661 GOT_OBJECT_CACHE_TYPE_TAG);
665 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
666 if (repo->pack_cache_size > rl.rlim_cur / 8)
667 repo->pack_cache_size = rl.rlim_cur / 8;
669 repo_path = realpath(path, NULL);
670 if (repo_path == NULL) {
671 err = got_error_from_errno2("realpath", path);
678 err = open_repo(repo, repo_path);
681 if (err->code != GOT_ERR_NOT_GIT_REPO)
683 if (repo_path[0] == '/' && repo_path[1] == '\0') {
684 err = got_error(GOT_ERR_NOT_GIT_REPO);
687 err = got_path_dirname(&parent_path, repo_path);
691 repo_path = parent_path;
694 err = read_gotconfig(repo);
698 err = read_gitconfig(repo, global_gitconfig_path);
701 if (repo->gitconfig_repository_format_version != 0)
702 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
703 for (i = 0; i < repo->nextensions; i++) {
704 char *ext = repo->extensions[i];
705 int j, supported = 0;
706 for (j = 0; j < nitems(repo_extensions); j++) {
707 if (strcmp(ext, repo_extensions[j]) == 0) {
713 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
719 got_repo_close(repo);
726 const struct got_error *
727 got_repo_close(struct got_repository *repo)
729 const struct got_error *err = NULL, *child_err;
732 for (i = 0; i < repo->pack_cache_size; i++) {
733 if (repo->packidx_cache[i] == NULL)
735 got_packidx_close(repo->packidx_cache[i]);
738 while (!STAILQ_EMPTY(&repo->packidx_bloom_filters)) {
739 struct got_packidx_bloom_filter *bf;
740 bf = STAILQ_FIRST(&repo->packidx_bloom_filters);
741 STAILQ_REMOVE_HEAD(&repo->packidx_bloom_filters, entry);
746 for (i = 0; i < repo->pack_cache_size; i++) {
747 if (repo->packs[i].path_packfile == NULL)
749 got_pack_close(&repo->packs[i]);
753 free(repo->path_git_dir);
755 got_object_cache_close(&repo->objcache);
756 got_object_cache_close(&repo->treecache);
757 got_object_cache_close(&repo->commitcache);
758 got_object_cache_close(&repo->tagcache);
760 for (i = 0; i < nitems(repo->privsep_children); i++) {
761 if (repo->privsep_children[i].imsg_fd == -1)
763 imsg_clear(repo->privsep_children[i].ibuf);
764 free(repo->privsep_children[i].ibuf);
765 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
766 child_err = got_privsep_wait_for_child(
767 repo->privsep_children[i].pid);
768 if (child_err && err == NULL)
770 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
772 err = got_error_from_errno("close");
775 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
777 err = got_error_from_errno("close");
780 got_gotconfig_free(repo->gotconfig);
781 free(repo->gitconfig_author_name);
782 free(repo->gitconfig_author_email);
783 for (i = 0; i < repo->ngitconfig_remotes; i++)
784 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
785 free(repo->gitconfig_remotes);
786 for (i = 0; i < repo->nextensions; i++)
787 free(repo->extensions[i]);
788 free(repo->extensions);
795 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
801 free(repo->fetch_url);
802 repo->fetch_url = NULL;
803 free(repo->send_url);
804 repo->send_url = NULL;
805 for (i = 0; i < repo->nfetch_branches; i++)
806 free(repo->fetch_branches[i]);
807 free(repo->fetch_branches);
808 repo->fetch_branches = NULL;
809 repo->nfetch_branches = 0;
810 for (i = 0; i < repo->nsend_branches; i++)
811 free(repo->send_branches[i]);
812 free(repo->send_branches);
813 repo->send_branches = NULL;
814 repo->nsend_branches = 0;
817 const struct got_error *
818 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
819 const char *input_path)
821 const struct got_error *err = NULL;
822 const char *repo_abspath = NULL;
824 char *canonpath, *path = NULL;
826 *in_repo_path = NULL;
828 canonpath = strdup(input_path);
829 if (canonpath == NULL) {
830 err = got_error_from_errno("strdup");
833 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
837 repo_abspath = got_repo_get_path(repo);
839 if (canonpath[0] == '\0') {
840 path = strdup(canonpath);
842 err = got_error_from_errno("strdup");
846 path = realpath(canonpath, NULL);
848 if (errno != ENOENT) {
849 err = got_error_from_errno2("realpath",
854 * Path is not on disk.
855 * Assume it is already relative to repository root.
857 path = strdup(canonpath);
859 err = got_error_from_errno("strdup");
864 repolen = strlen(repo_abspath);
868 if (strcmp(path, repo_abspath) == 0) {
872 err = got_error_from_errno("strdup");
875 } else if (len > repolen &&
876 got_path_is_child(path, repo_abspath, repolen)) {
877 /* Matched an on-disk path inside repository. */
878 if (got_repo_is_bare(repo)) {
880 * Matched an on-disk path inside repository
881 * database. Treat input as repository-relative.
888 /* Strip common prefix with repository path. */
889 err = got_path_skip_common_ancestor(&child,
898 * Matched unrelated on-disk path.
899 * Treat input as repository-relative.
907 /* Make in-repository path absolute */
908 if (path[0] != '/') {
910 if (asprintf(&abspath, "/%s", path) == -1) {
911 err = got_error_from_errno("asprintf");
923 *in_repo_path = path;
927 static const struct got_error *
928 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
929 const char *path_packidx)
931 const struct got_error *err = NULL;
934 for (i = 0; i < repo->pack_cache_size; i++) {
935 if (repo->packidx_cache[i] == NULL)
937 if (strcmp(repo->packidx_cache[i]->path_packidx,
938 path_packidx) == 0) {
939 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
942 if (i == repo->pack_cache_size) {
943 i = repo->pack_cache_size - 1;
944 err = got_packidx_close(repo->packidx_cache[i]);
949 repo->packidx_cache[i] = packidx;
955 got_repo_is_packidx_filename(const char *name, size_t len)
957 if (len != GOT_PACKIDX_NAMELEN)
960 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
963 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
964 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
971 check_packidx_bloom_filter(struct got_repository *repo,
972 const char *path_packidx, struct got_object_id *id)
974 struct got_packidx_bloom_filter *bf;
976 STAILQ_FOREACH(bf, &repo->packidx_bloom_filters, entry) {
977 if (got_path_cmp(bf->path_packidx, path_packidx,
978 bf->path_packidx_len, strlen(path_packidx)) == 0) {
979 return bloom_check(bf->bloom, id->sha1,
984 /* No bloom filter means this pack index must be searched. */
988 static const struct got_error *
989 add_packidx_bloom_filter(struct got_repository *repo,
990 struct got_packidx *packidx, const char *path_packidx)
992 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
993 struct got_packidx_bloom_filter *bf;
997 * Don't use bloom filters for very large pack index files.
998 * Large pack files will contain a relatively large fraction
999 * of our objects so we will likely need to visit them anyway.
1000 * The more objects a pack file contains the higher the probability
1001 * of a false-positive match from the bloom filter. And reading
1002 * all object IDs from a large pack index file can be expensive.
1004 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1007 /* Do we already have a filter for this pack index? */
1008 STAILQ_FOREACH(bf, &repo->packidx_bloom_filters, entry) {
1009 if (got_path_cmp(bf->path_packidx, path_packidx,
1010 bf->path_packidx_len, strlen(path_packidx)) == 0)
1014 bf = calloc(1, sizeof(*bf));
1016 return got_error_from_errno("calloc");
1017 bf->bloom = calloc(1, sizeof(*bf->bloom));
1018 if (bf->bloom == NULL) {
1020 return got_error_from_errno("calloc");
1024 len = strlcpy(bf->path_packidx, path_packidx, sizeof(bf->path_packidx));
1025 if (len >= sizeof(bf->path_packidx)) {
1028 return got_error(GOT_ERR_NO_SPACE);
1030 bf->path_packidx_len = len;
1032 /* Minimum size supported by our bloom filter is 1000 entries. */
1033 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1034 for (i = 0; i < nobjects; i++) {
1035 struct got_packidx_object_id *id;
1036 id = &packidx->hdr.sorted_ids[i];
1037 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1040 STAILQ_INSERT_TAIL(&repo->packidx_bloom_filters, bf, entry);
1044 const struct got_error *
1045 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1046 struct got_repository *repo, struct got_object_id *id)
1048 const struct got_error *err;
1049 DIR *packdir = NULL;
1050 struct dirent *dent;
1055 /* Search pack index cache. */
1056 for (i = 0; i < repo->pack_cache_size; i++) {
1057 if (repo->packidx_cache[i] == NULL)
1059 if (!check_packidx_bloom_filter(repo,
1060 repo->packidx_cache[i]->path_packidx, id))
1061 continue; /* object will not be found in this index */
1062 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1064 *packidx = repo->packidx_cache[i];
1066 * Move this cache entry to the front. Repeatedly
1067 * searching a wrong pack index can be expensive.
1070 memmove(&repo->packidx_cache[1],
1071 &repo->packidx_cache[0],
1072 i * sizeof(repo->packidx_cache[0]));
1073 repo->packidx_cache[0] = *packidx;
1078 /* No luck. Search the filesystem. */
1080 packdir_fd = openat(got_repo_get_fd(repo),
1081 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1082 if (packdir_fd == -1) {
1083 if (errno == ENOENT)
1084 err = got_error_no_obj(id);
1086 err = got_error_from_errno_fmt("openat: %s/%s",
1087 got_repo_get_path_git_dir(repo),
1088 GOT_OBJECTS_PACK_DIR);
1092 packdir = fdopendir(packdir_fd);
1093 if (packdir == NULL) {
1094 err = got_error_from_errno("fdopendir");
1098 while ((dent = readdir(packdir)) != NULL) {
1101 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1104 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1105 dent->d_name) == -1) {
1106 err = got_error_from_errno("asprintf");
1110 if (!check_packidx_bloom_filter(repo, path_packidx, id)) {
1112 continue; /* object will not be found in this index */
1115 for (i = 0; i < repo->pack_cache_size; i++) {
1116 if (repo->packidx_cache[i] == NULL)
1118 if (strcmp(repo->packidx_cache[i]->path_packidx,
1119 path_packidx) == 0) {
1126 continue; /* already searched */
1129 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1136 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1142 err = cache_packidx(repo, *packidx, path_packidx);
1147 *idx = got_packidx_get_object_idx(*packidx, id);
1149 err = NULL; /* found the object */
1154 err = got_error_no_obj(id);
1156 if (packdir && closedir(packdir) != 0 && err == NULL)
1157 err = got_error_from_errno("closedir");
1161 static const struct got_error *
1162 read_packfile_hdr(int fd, struct got_packidx *packidx)
1164 const struct got_error *err = NULL;
1165 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1166 struct got_packfile_hdr hdr;
1169 n = read(fd, &hdr, sizeof(hdr));
1171 return got_error_from_errno("read");
1172 if (n != sizeof(hdr))
1173 return got_error(GOT_ERR_BAD_PACKFILE);
1175 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1176 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1177 be32toh(hdr.nobjects) != totobj)
1178 err = got_error(GOT_ERR_BAD_PACKFILE);
1183 static const struct got_error *
1184 open_packfile(int *fd, struct got_repository *repo,
1185 const char *relpath, struct got_packidx *packidx)
1187 const struct got_error *err = NULL;
1189 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1191 return got_error_from_errno_fmt("openat: %s/%s",
1192 got_repo_get_path_git_dir(repo), relpath);
1195 err = read_packfile_hdr(*fd, packidx);
1205 const struct got_error *
1206 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1207 const char *path_packfile, struct got_packidx *packidx)
1209 const struct got_error *err = NULL;
1210 struct got_pack *pack = NULL;
1217 for (i = 0; i < repo->pack_cache_size; i++) {
1218 pack = &repo->packs[i];
1219 if (pack->path_packfile == NULL)
1221 if (strcmp(pack->path_packfile, path_packfile) == 0)
1222 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1225 if (i == repo->pack_cache_size) {
1226 err = got_pack_close(&repo->packs[i - 1]);
1229 memmove(&repo->packs[1], &repo->packs[0],
1230 sizeof(repo->packs) - sizeof(repo->packs[0]));
1234 pack = &repo->packs[i];
1236 pack->path_packfile = strdup(path_packfile);
1237 if (pack->path_packfile == NULL) {
1238 err = got_error_from_errno("strdup");
1242 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1246 if (fstat(pack->fd, &sb) != 0) {
1247 err = got_error_from_errno("fstat");
1250 pack->filesize = sb.st_size;
1252 pack->privsep_child = NULL;
1254 #ifndef GOT_PACK_NO_MMAP
1255 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1257 if (pack->map == MAP_FAILED) {
1258 if (errno != ENOMEM) {
1259 err = got_error_from_errno("mmap");
1262 pack->map = NULL; /* fall back to read(2) */
1268 free(pack->path_packfile);
1269 memset(pack, 0, sizeof(*pack));
1277 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1279 struct got_pack *pack = NULL;
1282 for (i = 0; i < repo->pack_cache_size; i++) {
1283 pack = &repo->packs[i];
1284 if (pack->path_packfile == NULL)
1286 if (strcmp(pack->path_packfile, path_packfile) == 0)
1293 const struct got_error *
1294 got_repo_init(const char *repo_path)
1296 const struct got_error *err = NULL;
1297 const char *dirnames[] = {
1299 GOT_OBJECTS_PACK_DIR,
1302 const char *description_str = "Unnamed repository; "
1303 "edit this file 'description' to name the repository.";
1304 const char *headref_str = "ref: refs/heads/main";
1305 const char *gitconfig_str = "[core]\n"
1306 "\trepositoryformatversion = 0\n"
1307 "\tfilemode = true\n"
1312 if (!got_path_dir_is_empty(repo_path))
1313 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1315 for (i = 0; i < nitems(dirnames); i++) {
1316 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1317 return got_error_from_errno("asprintf");
1319 err = got_path_mkdir(path);
1325 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1326 return got_error_from_errno("asprintf");
1327 err = got_path_create_file(path, description_str);
1332 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1333 return got_error_from_errno("asprintf");
1334 err = got_path_create_file(path, headref_str);
1339 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1340 return got_error_from_errno("asprintf");
1341 err = got_path_create_file(path, gitconfig_str);
1349 static const struct got_error *
1350 match_packed_object(struct got_object_id **unique_id,
1351 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1353 const struct got_error *err = NULL;
1354 DIR *packdir = NULL;
1355 struct dirent *dent;
1357 struct got_object_id_queue matched_ids;
1360 STAILQ_INIT(&matched_ids);
1362 packdir_fd = openat(got_repo_get_fd(repo),
1363 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1364 if (packdir_fd == -1) {
1365 if (errno != ENOENT)
1366 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1370 packdir = fdopendir(packdir_fd);
1371 if (packdir == NULL) {
1372 err = got_error_from_errno("fdopendir");
1376 while ((dent = readdir(packdir)) != NULL) {
1377 struct got_packidx *packidx;
1378 struct got_object_qid *qid;
1380 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1383 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1384 dent->d_name) == -1) {
1385 err = got_error_from_errno("strdup");
1389 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1395 err = got_packidx_match_id_str_prefix(&matched_ids,
1396 packidx, id_str_prefix);
1398 got_packidx_close(packidx);
1401 err = got_packidx_close(packidx);
1405 STAILQ_FOREACH(qid, &matched_ids, entry) {
1406 if (obj_type != GOT_OBJ_TYPE_ANY) {
1408 err = got_object_get_type(&matched_type, repo,
1412 if (matched_type != obj_type)
1415 if (*unique_id == NULL) {
1416 *unique_id = got_object_id_dup(qid->id);
1417 if (*unique_id == NULL) {
1418 err = got_error_from_errno("malloc");
1422 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1423 continue; /* packed multiple times */
1424 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1430 got_object_id_queue_free(&matched_ids);
1431 if (packdir && closedir(packdir) != 0 && err == NULL)
1432 err = got_error_from_errno("closedir");
1440 static const struct got_error *
1441 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1442 const char *object_dir, const char *id_str_prefix, int obj_type,
1443 struct got_repository *repo)
1445 const struct got_error *err = NULL;
1448 struct dirent *dent;
1449 struct got_object_id id;
1451 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1452 err = got_error_from_errno("asprintf");
1456 dir = opendir(path);
1458 if (errno == ENOENT) {
1462 err = got_error_from_errno2("opendir", path);
1465 while ((dent = readdir(dir)) != NULL) {
1469 if (strcmp(dent->d_name, ".") == 0 ||
1470 strcmp(dent->d_name, "..") == 0)
1473 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1474 err = got_error_from_errno("asprintf");
1478 if (!got_parse_sha1_digest(id.sha1, id_str))
1482 * Directory entries do not necessarily appear in
1483 * sorted order, so we must iterate over all of them.
1485 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1491 if (*unique_id == NULL) {
1492 if (obj_type != GOT_OBJ_TYPE_ANY) {
1494 err = got_object_get_type(&matched_type, repo,
1498 if (matched_type != obj_type)
1501 *unique_id = got_object_id_dup(&id);
1502 if (*unique_id == NULL) {
1503 err = got_error_from_errno("got_object_id_dup");
1508 if (got_object_id_cmp(*unique_id, &id) == 0)
1509 continue; /* both packed and loose */
1510 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1516 if (dir && closedir(dir) != 0 && err == NULL)
1517 err = got_error_from_errno("closedir");
1526 const struct got_error *
1527 got_repo_match_object_id_prefix(struct got_object_id **id,
1528 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1530 const struct got_error *err = NULL;
1531 char *path_objects = got_repo_get_path_objects(repo);
1532 char *object_dir = NULL;
1538 for (i = 0; i < strlen(id_str_prefix); i++) {
1539 if (isxdigit((unsigned char)id_str_prefix[i]))
1541 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1544 len = strlen(id_str_prefix);
1546 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1549 object_dir = strndup(id_str_prefix, 2);
1550 if (object_dir == NULL) {
1551 err = got_error_from_errno("strdup");
1554 err = match_loose_object(id, path_objects, object_dir,
1555 id_str_prefix, obj_type, repo);
1556 } else if (len == 1) {
1558 for (i = 0; i < 0xf; i++) {
1559 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1561 err = got_error_from_errno("asprintf");
1564 err = match_packed_object(id, repo, object_dir,
1568 err = match_loose_object(id, path_objects, object_dir,
1569 id_str_prefix, obj_type, repo);
1574 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1582 } else if (*id == NULL) {
1584 case GOT_OBJ_TYPE_BLOB:
1585 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1586 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1588 case GOT_OBJ_TYPE_TREE:
1589 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1590 GOT_OBJ_LABEL_TREE, id_str_prefix);
1592 case GOT_OBJ_TYPE_COMMIT:
1593 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1594 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1596 case GOT_OBJ_TYPE_TAG:
1597 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1598 GOT_OBJ_LABEL_TAG, id_str_prefix);
1601 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1609 const struct got_error *
1610 got_repo_match_object_id(struct got_object_id **id, char **label,
1611 const char *id_str, int obj_type, struct got_reflist_head *refs,
1612 struct got_repository *repo)
1614 const struct got_error *err;
1615 struct got_tag_object *tag;
1616 struct got_reference *ref = NULL;
1623 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1626 *id = got_object_id_dup(
1627 got_object_tag_get_object_id(tag));
1629 err = got_error_from_errno("got_object_id_dup");
1630 else if (label && asprintf(label, "refs/tags/%s",
1631 got_object_tag_get_name(tag)) == -1) {
1632 err = got_error_from_errno("asprintf");
1636 got_object_tag_close(tag);
1638 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1639 err->code != GOT_ERR_NO_OBJ)
1643 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1645 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1647 err = got_ref_open(&ref, repo, id_str, 0);
1651 *label = strdup(got_ref_get_name(ref));
1652 if (*label == NULL) {
1653 err = got_error_from_errno("strdup");
1657 err = got_ref_resolve(id, repo, ref);
1659 err = got_object_id_str(label, *id);
1660 if (*label == NULL) {
1661 err = got_error_from_errno("strdup");
1671 const struct got_error *
1672 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1673 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1675 const struct got_error *err = NULL;
1676 struct got_reflist_entry *re;
1677 struct got_object_id *tag_id;
1678 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1682 TAILQ_FOREACH(re, refs, entry) {
1683 const char *refname;
1684 refname = got_ref_get_name(re->ref);
1685 if (got_ref_is_symbolic(re->ref))
1687 if (strncmp(refname, "refs/tags/", 10) != 0)
1689 if (!name_is_absolute)
1690 refname += strlen("refs/tags/");
1691 if (strcmp(refname, name) != 0)
1693 err = got_ref_resolve(&tag_id, repo, re->ref);
1696 err = got_object_open_as_tag(tag, repo, tag_id);
1700 if (obj_type == GOT_OBJ_TYPE_ANY ||
1701 got_object_tag_get_object_type(*tag) == obj_type)
1703 got_object_tag_close(*tag);
1707 if (err == NULL && *tag == NULL)
1708 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1709 GOT_OBJ_LABEL_TAG, name);
1713 static const struct got_error *
1714 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1715 const char *name, mode_t mode, struct got_object_id *blob_id)
1717 const struct got_error *err = NULL;
1721 *new_te = calloc(1, sizeof(**new_te));
1722 if (*new_te == NULL)
1723 return got_error_from_errno("calloc");
1725 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1726 sizeof((*new_te)->name)) {
1727 err = got_error(GOT_ERR_NO_SPACE);
1731 if (S_ISLNK(mode)) {
1732 (*new_te)->mode = S_IFLNK;
1734 (*new_te)->mode = S_IFREG;
1735 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1737 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1739 if (err && *new_te) {
1746 static const struct got_error *
1747 import_file(struct got_tree_entry **new_te, struct dirent *de,
1748 const char *path, struct got_repository *repo)
1750 const struct got_error *err;
1751 struct got_object_id *blob_id = NULL;
1755 if (asprintf(&filepath, "%s%s%s", path,
1756 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1757 return got_error_from_errno("asprintf");
1759 if (lstat(filepath, &sb) != 0) {
1760 err = got_error_from_errno2("lstat", path);
1764 err = got_object_blob_create(&blob_id, filepath, repo);
1768 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1777 static const struct got_error *
1778 insert_tree_entry(struct got_tree_entry *new_te,
1779 struct got_pathlist_head *paths)
1781 const struct got_error *err = NULL;
1782 struct got_pathlist_entry *new_pe;
1784 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1788 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1792 static const struct got_error *write_tree(struct got_object_id **,
1793 const char *, struct got_pathlist_head *, struct got_repository *,
1794 got_repo_import_cb progress_cb, void *progress_arg);
1796 static const struct got_error *
1797 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1798 const char *path, struct got_pathlist_head *ignores,
1799 struct got_repository *repo,
1800 got_repo_import_cb progress_cb, void *progress_arg)
1802 const struct got_error *err;
1803 struct got_object_id *id = NULL;
1806 if (asprintf(&subdirpath, "%s%s%s", path,
1807 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1808 return got_error_from_errno("asprintf");
1810 (*new_te) = calloc(1, sizeof(**new_te));
1811 if (*new_te == NULL)
1812 return got_error_from_errno("calloc");
1813 (*new_te)->mode = S_IFDIR;
1814 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1815 sizeof((*new_te)->name)) {
1816 err = got_error(GOT_ERR_NO_SPACE);
1819 err = write_tree(&id, subdirpath, ignores, repo,
1820 progress_cb, progress_arg);
1823 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1835 static const struct got_error *
1836 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1837 struct got_pathlist_head *ignores, struct got_repository *repo,
1838 got_repo_import_cb progress_cb, void *progress_arg)
1840 const struct got_error *err = NULL;
1844 struct got_tree_entry *new_te = NULL;
1845 struct got_pathlist_head paths;
1846 struct got_pathlist_entry *pe;
1848 *new_tree_id = NULL;
1852 dir = opendir(path_dir);
1854 err = got_error_from_errno2("opendir", path_dir);
1859 while ((de = readdir(dir)) != NULL) {
1863 if (strcmp(de->d_name, ".") == 0 ||
1864 strcmp(de->d_name, "..") == 0)
1867 TAILQ_FOREACH(pe, ignores, entry) {
1868 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1876 err = got_path_dirent_type(&type, path_dir, de);
1880 if (type == DT_DIR) {
1881 err = import_subdir(&new_te, de, path_dir,
1882 ignores, repo, progress_cb, progress_arg);
1884 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1889 } else if (type == DT_REG || type == DT_LNK) {
1890 err = import_file(&new_te, de, path_dir, repo);
1896 err = insert_tree_entry(new_te, &paths);
1902 if (TAILQ_EMPTY(&paths)) {
1903 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1904 "cannot create tree without any entries");
1908 TAILQ_FOREACH(pe, &paths, entry) {
1909 struct got_tree_entry *te = pe->data;
1911 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1913 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1914 err = got_error_from_errno("asprintf");
1917 err = (*progress_cb)(progress_arg, path);
1923 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1927 got_pathlist_free(&paths);
1931 const struct got_error *
1932 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1933 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1934 struct got_repository *repo, got_repo_import_cb progress_cb,
1937 const struct got_error *err;
1938 struct got_object_id *new_tree_id;
1940 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1941 progress_cb, progress_arg);
1945 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1946 author, time(NULL), author, time(NULL), logmsg, repo);
1951 const struct got_error *
1952 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1953 struct got_repository *repo)
1955 const struct got_error *err = NULL;
1956 char *path_objects = NULL, *path = NULL;
1958 struct got_object_id id;
1964 path_objects = got_repo_get_path_objects(repo);
1965 if (path_objects == NULL)
1966 return got_error_from_errno("got_repo_get_path_objects");
1968 for (i = 0; i <= 0xff; i++) {
1969 struct dirent *dent;
1971 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1972 err = got_error_from_errno("asprintf");
1976 dir = opendir(path);
1978 if (errno == ENOENT) {
1982 err = got_error_from_errno2("opendir", path);
1986 while ((dent = readdir(dir)) != NULL) {
1991 if (strcmp(dent->d_name, ".") == 0 ||
1992 strcmp(dent->d_name, "..") == 0)
1995 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1996 err = got_error_from_errno("asprintf");
2000 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2006 err = got_object_open_loose_fd(&fd, &id, repo);
2010 if (fstat(fd, &sb) == -1) {
2011 err = got_error_from_errno("fstat");
2016 (*ondisk_size) += sb.st_size;
2018 if (close(fd) == -1) {
2019 err = got_error_from_errno("close");
2024 if (closedir(dir) != 0) {
2025 err = got_error_from_errno("closedir");
2034 if (dir && closedir(dir) != 0 && err == NULL)
2035 err = got_error_from_errno("closedir");
2046 const struct got_error *
2047 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2048 off_t *total_packsize, struct got_repository *repo)
2050 const struct got_error *err = NULL;
2051 DIR *packdir = NULL;
2052 struct dirent *dent;
2053 struct got_packidx *packidx = NULL;
2055 char *path_packfile;
2061 *total_packsize = 0;
2063 packdir_fd = openat(got_repo_get_fd(repo),
2064 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2065 if (packdir_fd == -1) {
2066 return got_error_from_errno_fmt("openat: %s/%s",
2067 got_repo_get_path_git_dir(repo),
2068 GOT_OBJECTS_PACK_DIR);
2071 packdir = fdopendir(packdir_fd);
2072 if (packdir == NULL) {
2073 err = got_error_from_errno("fdopendir");
2077 while ((dent = readdir(packdir)) != NULL) {
2078 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2081 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2082 dent->d_name) == -1) {
2083 err = got_error_from_errno("asprintf");
2087 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2093 if (fstat(packidx->fd, &sb) == -1)
2095 *total_packsize += sb.st_size;
2097 err = got_packidx_get_packfile_path(&path_packfile,
2098 packidx->path_packidx);
2102 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2104 free(path_packfile);
2107 free(path_packfile);
2108 *total_packsize += sb.st_size;
2110 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2114 got_packidx_close(packidx);
2119 got_packidx_close(packidx);
2120 if (packdir && closedir(packdir) != 0 && err == NULL)
2121 err = got_error_from_errno("closedir");
2125 *total_packsize = 0;