2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <sys/resource.h>
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_cancel.h"
46 #include "got_object.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_object_cache.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_gotconfig.h"
61 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
65 got_packidx_bloom_filter_cmp);
68 got_repo_get_path(struct got_repository *repo)
74 got_repo_get_path_git_dir(struct got_repository *repo)
76 return repo->path_git_dir;
80 got_repo_get_fd(struct got_repository *repo)
82 return repo->gitdir_fd;
86 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 return repo->gitconfig_author_name;
92 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 return repo->gitconfig_author_email;
98 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
100 return repo->global_gitconfig_author_name;
104 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
106 return repo->global_gitconfig_author_email;
110 got_repo_get_gitconfig_owner(struct got_repository *repo)
112 return repo->gitconfig_owner;
116 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
117 struct got_repository *repo)
119 *extensions = repo->extensions;
120 *nextensions = repo->nextensions;
124 got_repo_is_bare(struct got_repository *repo)
126 return (strcmp(repo->path, repo->path_git_dir) == 0);
130 get_path_git_child(struct got_repository *repo, const char *basename)
134 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
142 got_repo_get_path_objects(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_DIR);
148 got_repo_get_path_objects_pack(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
154 got_repo_get_path_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_REFS_DIR);
160 got_repo_get_path_packed_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
166 get_path_head(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_HEAD_FILE);
172 got_repo_get_path_gitconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GITCONFIG);
178 got_repo_get_path_gotconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
183 const struct got_gotconfig *
184 got_repo_get_gotconfig(struct got_repository *repo)
186 return repo->gotconfig;
190 got_repo_get_gitconfig_remotes(int *nremotes,
191 const struct got_remote_repo **remotes, struct got_repository *repo)
193 *nremotes = repo->ngitconfig_remotes;
194 *remotes = repo->gitconfig_remotes;
198 is_git_repo(struct got_repository *repo)
200 const char *path_git = got_repo_get_path_git_dir(repo);
201 char *path_objects = got_repo_get_path_objects(repo);
202 char *path_refs = got_repo_get_path_refs(repo);
203 char *path_head = get_path_head(repo);
206 struct got_reference *head_ref;
208 if (lstat(path_git, &sb) == -1)
210 if (!S_ISDIR(sb.st_mode))
213 if (lstat(path_objects, &sb) == -1)
215 if (!S_ISDIR(sb.st_mode))
218 if (lstat(path_refs, &sb) == -1)
220 if (!S_ISDIR(sb.st_mode))
223 if (lstat(path_head, &sb) == -1)
225 if (!S_ISREG(sb.st_mode))
228 /* Check if the HEAD reference can be opened. */
229 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
231 got_ref_close(head_ref);
242 const struct got_error *
243 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
244 struct got_object *obj)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->objcache, id, obj);
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
261 got_repo_get_cached_object(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
267 const struct got_error *
268 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
269 struct got_tree_object *tree)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = got_object_cache_add(&repo->treecache, id, tree);
275 if (err->code == GOT_ERR_OBJ_EXISTS ||
276 err->code == GOT_ERR_OBJ_TOO_LARGE)
285 struct got_tree_object *
286 got_repo_get_cached_tree(struct got_repository *repo,
287 struct got_object_id *id)
289 return (struct got_tree_object *)got_object_cache_get(
290 &repo->treecache, id);
293 const struct got_error *
294 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
295 struct got_commit_object *commit)
297 #ifndef GOT_NO_OBJ_CACHE
298 const struct got_error *err = NULL;
299 err = got_object_cache_add(&repo->commitcache, id, commit);
301 if (err->code == GOT_ERR_OBJ_EXISTS ||
302 err->code == GOT_ERR_OBJ_TOO_LARGE)
311 struct got_commit_object *
312 got_repo_get_cached_commit(struct got_repository *repo,
313 struct got_object_id *id)
315 return (struct got_commit_object *)got_object_cache_get(
316 &repo->commitcache, id);
319 const struct got_error *
320 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
321 struct got_tag_object *tag)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->tagcache, id, tag);
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
337 struct got_tag_object *
338 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
340 return (struct got_tag_object *)got_object_cache_get(
341 &repo->tagcache, id);
344 const struct got_error *
345 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
346 struct got_raw_object *raw)
348 #ifndef GOT_NO_OBJ_CACHE
349 const struct got_error *err = NULL;
350 err = got_object_cache_add(&repo->rawcache, id, raw);
352 if (err->code == GOT_ERR_OBJ_EXISTS ||
353 err->code == GOT_ERR_OBJ_TOO_LARGE)
363 struct got_raw_object *
364 got_repo_get_cached_raw_object(struct got_repository *repo,
365 struct got_object_id *id)
367 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
371 static const struct got_error *
372 open_repo(struct got_repository *repo, const char *path)
374 const struct got_error *err = NULL;
376 repo->gitdir_fd = -1;
378 /* bare git repository? */
379 repo->path_git_dir = strdup(path);
380 if (repo->path_git_dir == NULL)
381 return got_error_from_errno("strdup");
382 if (is_git_repo(repo)) {
383 repo->path = strdup(repo->path_git_dir);
384 if (repo->path == NULL) {
385 err = got_error_from_errno("strdup");
388 repo->gitdir_fd = open(repo->path_git_dir,
389 O_DIRECTORY | O_CLOEXEC);
390 if (repo->gitdir_fd == -1) {
391 err = got_error_from_errno2("open",
398 /* git repository with working tree? */
399 free(repo->path_git_dir);
400 repo->path_git_dir = NULL;
401 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
402 err = got_error_from_errno("asprintf");
405 if (is_git_repo(repo)) {
406 repo->path = strdup(path);
407 if (repo->path == NULL) {
408 err = got_error_from_errno("strdup");
411 repo->gitdir_fd = open(repo->path_git_dir,
412 O_DIRECTORY | O_CLOEXEC);
413 if (repo->gitdir_fd == -1) {
414 err = got_error_from_errno2("open",
421 err = got_error(GOT_ERR_NOT_GIT_REPO);
426 free(repo->path_git_dir);
427 repo->path_git_dir = NULL;
428 if (repo->gitdir_fd != -1)
429 close(repo->gitdir_fd);
430 repo->gitdir_fd = -1;
436 static const struct got_error *
437 parse_gitconfig_file(int *gitconfig_repository_format_version,
438 char **gitconfig_author_name, char **gitconfig_author_email,
439 struct got_remote_repo **remotes, int *nremotes,
440 char **gitconfig_owner, char ***extensions, int *nextensions,
441 const char *gitconfig_path)
443 const struct got_error *err = NULL, *child_err = NULL;
445 int imsg_fds[2] = { -1, -1 };
447 struct imsgbuf *ibuf;
449 *gitconfig_repository_format_version = 0;
454 *gitconfig_author_name = NULL;
455 *gitconfig_author_email = NULL;
461 *gitconfig_owner = NULL;
463 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
467 return got_error_from_errno2("open", gitconfig_path);
470 ibuf = calloc(1, sizeof(*ibuf));
472 err = got_error_from_errno("calloc");
476 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
477 err = got_error_from_errno("socketpair");
483 err = got_error_from_errno("fork");
485 } else if (pid == 0) {
486 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
491 if (close(imsg_fds[1]) == -1) {
492 err = got_error_from_errno("close");
496 imsg_init(ibuf, imsg_fds[0]);
498 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
503 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
507 err = got_privsep_recv_gitconfig_int(
508 gitconfig_repository_format_version, ibuf);
512 if (extensions && nextensions) {
513 err = got_privsep_send_gitconfig_repository_extensions_req(
517 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
520 if (*nextensions > 0) {
522 *extensions = calloc(*nextensions, sizeof(char *));
523 if (*extensions == NULL) {
524 err = got_error_from_errno("calloc");
527 for (i = 0; i < *nextensions; i++) {
529 err = got_privsep_recv_gitconfig_str(&ext,
533 (*extensions)[i] = ext;
538 err = got_privsep_send_gitconfig_author_name_req(ibuf);
542 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
546 err = got_privsep_send_gitconfig_author_email_req(ibuf);
550 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
554 if (remotes && nremotes) {
555 err = got_privsep_send_gitconfig_remotes_req(ibuf);
559 err = got_privsep_recv_gitconfig_remotes(remotes,
565 if (gitconfig_owner) {
566 err = got_privsep_send_gitconfig_owner_req(ibuf);
569 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
574 err = got_privsep_send_stop(imsg_fds[0]);
575 child_err = got_privsep_wait_for_child(pid);
576 if (child_err && err == NULL)
579 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
580 err = got_error_from_errno("close");
581 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 if (fd != -1 && close(fd) == -1 && err == NULL)
584 err = got_error_from_errno2("close", gitconfig_path);
589 static const struct got_error *
590 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
592 const struct got_error *err = NULL;
593 char *repo_gitconfig_path = NULL;
595 if (global_gitconfig_path) {
596 /* Read settings from ~/.gitconfig. */
597 int dummy_repo_version;
598 err = parse_gitconfig_file(&dummy_repo_version,
599 &repo->global_gitconfig_author_name,
600 &repo->global_gitconfig_author_email,
601 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
606 /* Read repository's .git/config file. */
607 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
608 if (repo_gitconfig_path == NULL)
609 return got_error_from_errno("got_repo_get_path_gitconfig");
611 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
612 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
613 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
614 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
615 repo_gitconfig_path);
619 free(repo_gitconfig_path);
623 static const struct got_error *
624 read_gotconfig(struct got_repository *repo)
626 const struct got_error *err = NULL;
627 char *gotconfig_path;
629 gotconfig_path = got_repo_get_path_gotconfig(repo);
630 if (gotconfig_path == NULL)
631 return got_error_from_errno("got_repo_get_path_gotconfig");
633 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
634 free(gotconfig_path);
638 /* Supported repository format extensions. */
639 static const char *const repo_extensions[] = {
640 "noop", /* Got supports repository format version 1. */
641 "preciousObjects", /* Supported by gotadmin cleanup. */
642 "worktreeConfig", /* Got does not care about Git work trees. */
645 const struct got_error *
646 got_repo_open(struct got_repository **repop, const char *path,
647 const char *global_gitconfig_path)
649 struct got_repository *repo = NULL;
650 const struct got_error *err = NULL;
651 char *repo_path = NULL;
657 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
658 return got_error_from_errno("getrlimit");
660 repo = calloc(1, sizeof(*repo));
662 return got_error_from_errno("calloc");
664 RB_INIT(&repo->packidx_bloom_filters);
665 TAILQ_INIT(&repo->packidx_paths);
667 for (i = 0; i < nitems(repo->privsep_children); i++) {
668 memset(&repo->privsep_children[i], 0,
669 sizeof(repo->privsep_children[0]));
670 repo->privsep_children[i].imsg_fd = -1;
673 err = got_object_cache_init(&repo->objcache,
674 GOT_OBJECT_CACHE_TYPE_OBJ);
677 err = got_object_cache_init(&repo->treecache,
678 GOT_OBJECT_CACHE_TYPE_TREE);
681 err = got_object_cache_init(&repo->commitcache,
682 GOT_OBJECT_CACHE_TYPE_COMMIT);
685 err = got_object_cache_init(&repo->tagcache,
686 GOT_OBJECT_CACHE_TYPE_TAG);
689 err = got_object_cache_init(&repo->rawcache,
690 GOT_OBJECT_CACHE_TYPE_RAW);
694 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
695 if (repo->pack_cache_size > rl.rlim_cur / 8)
696 repo->pack_cache_size = rl.rlim_cur / 8;
698 repo_path = realpath(path, NULL);
699 if (repo_path == NULL) {
700 err = got_error_from_errno2("realpath", path);
707 err = open_repo(repo, repo_path);
710 if (err->code != GOT_ERR_NOT_GIT_REPO)
712 if (repo_path[0] == '/' && repo_path[1] == '\0') {
713 err = got_error(GOT_ERR_NOT_GIT_REPO);
716 err = got_path_dirname(&parent_path, repo_path);
720 repo_path = parent_path;
723 err = read_gotconfig(repo);
727 err = read_gitconfig(repo, global_gitconfig_path);
730 if (repo->gitconfig_repository_format_version != 0)
731 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
732 for (i = 0; i < repo->nextensions; i++) {
733 char *ext = repo->extensions[i];
734 int j, supported = 0;
735 for (j = 0; j < nitems(repo_extensions); j++) {
736 if (strcmp(ext, repo_extensions[j]) == 0) {
742 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
747 err = got_repo_list_packidx(&repo->packidx_paths, repo);
750 got_repo_close(repo);
757 const struct got_error *
758 got_repo_close(struct got_repository *repo)
760 const struct got_error *err = NULL, *child_err;
761 struct got_packidx_bloom_filter *bf;
762 struct got_pathlist_entry *pe;
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);
824 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
825 free((void *)pe->path);
826 got_pathlist_free(&repo->packidx_paths);
833 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
839 free(repo->fetch_url);
840 repo->fetch_url = NULL;
841 free(repo->send_url);
842 repo->send_url = NULL;
843 for (i = 0; i < repo->nfetch_branches; i++)
844 free(repo->fetch_branches[i]);
845 free(repo->fetch_branches);
846 repo->fetch_branches = NULL;
847 repo->nfetch_branches = 0;
848 for (i = 0; i < repo->nsend_branches; i++)
849 free(repo->send_branches[i]);
850 free(repo->send_branches);
851 repo->send_branches = NULL;
852 repo->nsend_branches = 0;
855 const struct got_error *
856 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
857 const char *input_path)
859 const struct got_error *err = NULL;
860 const char *repo_abspath = NULL;
862 char *canonpath, *path = NULL;
864 *in_repo_path = NULL;
866 canonpath = strdup(input_path);
867 if (canonpath == NULL) {
868 err = got_error_from_errno("strdup");
871 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
875 repo_abspath = got_repo_get_path(repo);
877 if (canonpath[0] == '\0') {
878 path = strdup(canonpath);
880 err = got_error_from_errno("strdup");
884 path = realpath(canonpath, NULL);
886 if (errno != ENOENT) {
887 err = got_error_from_errno2("realpath",
892 * Path is not on disk.
893 * Assume it is already relative to repository root.
895 path = strdup(canonpath);
897 err = got_error_from_errno("strdup");
902 repolen = strlen(repo_abspath);
906 if (strcmp(path, repo_abspath) == 0) {
910 err = got_error_from_errno("strdup");
913 } else if (len > repolen &&
914 got_path_is_child(path, repo_abspath, repolen)) {
915 /* Matched an on-disk path inside repository. */
916 if (got_repo_is_bare(repo)) {
918 * Matched an on-disk path inside repository
919 * database. Treat input as repository-relative.
926 /* Strip common prefix with repository path. */
927 err = got_path_skip_common_ancestor(&child,
936 * Matched unrelated on-disk path.
937 * Treat input as repository-relative.
945 /* Make in-repository path absolute */
946 if (path[0] != '/') {
948 if (asprintf(&abspath, "/%s", path) == -1) {
949 err = got_error_from_errno("asprintf");
961 *in_repo_path = path;
965 static const struct got_error *
966 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
967 const char *path_packidx)
969 const struct got_error *err = NULL;
972 for (i = 0; i < repo->pack_cache_size; i++) {
973 if (repo->packidx_cache[i] == NULL)
975 if (strcmp(repo->packidx_cache[i]->path_packidx,
976 path_packidx) == 0) {
977 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
980 if (i == repo->pack_cache_size) {
981 i = repo->pack_cache_size - 1;
982 err = got_packidx_close(repo->packidx_cache[i]);
987 repo->packidx_cache[i] = packidx;
993 got_repo_is_packidx_filename(const char *name, size_t len)
995 if (len != GOT_PACKIDX_NAMELEN)
998 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1001 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1002 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1008 static struct got_packidx_bloom_filter *
1009 get_packidx_bloom_filter(struct got_repository *repo,
1010 const char *path, size_t path_len)
1012 struct got_packidx_bloom_filter key;
1014 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1015 return NULL; /* XXX */
1016 key.path_len = path_len;
1018 return RB_FIND(got_packidx_bloom_filter_tree,
1019 &repo->packidx_bloom_filters, &key);
1023 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1024 const char *path_packidx, struct got_object_id *id)
1026 struct got_packidx_bloom_filter *bf;
1028 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1030 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1032 /* No bloom filter means this pack index must be searched. */
1036 static const struct got_error *
1037 add_packidx_bloom_filter(struct got_repository *repo,
1038 struct got_packidx *packidx, const char *path_packidx)
1040 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1041 struct got_packidx_bloom_filter *bf;
1045 * Don't use bloom filters for very large pack index files.
1046 * Large pack files will contain a relatively large fraction
1047 * of our objects so we will likely need to visit them anyway.
1048 * The more objects a pack file contains the higher the probability
1049 * of a false-positive match from the bloom filter. And reading
1050 * all object IDs from a large pack index file can be expensive.
1052 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1055 /* Do we already have a filter for this pack index? */
1056 if (get_packidx_bloom_filter(repo, path_packidx,
1057 strlen(path_packidx)) != NULL)
1060 bf = calloc(1, sizeof(*bf));
1062 return got_error_from_errno("calloc");
1063 bf->bloom = calloc(1, sizeof(*bf->bloom));
1064 if (bf->bloom == NULL) {
1066 return got_error_from_errno("calloc");
1069 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1070 if (len >= sizeof(bf->path)) {
1073 return got_error(GOT_ERR_NO_SPACE);
1077 /* Minimum size supported by our bloom filter is 1000 entries. */
1078 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1079 for (i = 0; i < nobjects; i++) {
1080 struct got_packidx_object_id *id;
1081 id = &packidx->hdr.sorted_ids[i];
1082 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1085 RB_INSERT(got_packidx_bloom_filter_tree,
1086 &repo->packidx_bloom_filters, bf);
1090 const struct got_error *
1091 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1092 struct got_repository *repo, struct got_object_id *id)
1094 const struct got_error *err;
1095 struct got_pathlist_entry *pe;
1098 /* Search pack index cache. */
1099 for (i = 0; i < repo->pack_cache_size; i++) {
1100 if (repo->packidx_cache[i] == NULL)
1102 if (!got_repo_check_packidx_bloom_filter(repo,
1103 repo->packidx_cache[i]->path_packidx, id))
1104 continue; /* object will not be found in this index */
1105 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1107 *packidx = repo->packidx_cache[i];
1109 * Move this cache entry to the front. Repeatedly
1110 * searching a wrong pack index can be expensive.
1113 memmove(&repo->packidx_cache[1],
1114 &repo->packidx_cache[0],
1115 i * sizeof(repo->packidx_cache[0]));
1116 repo->packidx_cache[0] = *packidx;
1121 /* No luck. Search the filesystem. */
1123 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1124 const char *path_packidx = pe->path;
1127 if (!got_repo_check_packidx_bloom_filter(repo,
1129 continue; /* object will not be found in this index */
1131 for (i = 0; i < repo->pack_cache_size; i++) {
1132 if (repo->packidx_cache[i] == NULL)
1134 if (strcmp(repo->packidx_cache[i]->path_packidx,
1135 path_packidx) == 0) {
1141 continue; /* already searched */
1143 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1148 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1152 err = cache_packidx(repo, *packidx, path_packidx);
1156 *idx = got_packidx_get_object_idx(*packidx, id);
1158 err = NULL; /* found the object */
1163 err = got_error_no_obj(id);
1168 const struct got_error *
1169 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1170 struct got_repository *repo)
1172 const struct got_error *err = NULL;
1173 DIR *packdir = NULL;
1174 struct dirent *dent;
1175 char *path_packidx = NULL;
1178 packdir_fd = openat(got_repo_get_fd(repo),
1179 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1180 if (packdir_fd == -1) {
1181 return got_error_from_errno_fmt("openat: %s/%s",
1182 got_repo_get_path_git_dir(repo),
1183 GOT_OBJECTS_PACK_DIR);
1186 packdir = fdopendir(packdir_fd);
1187 if (packdir == NULL) {
1188 err = got_error_from_errno("fdopendir");
1192 while ((dent = readdir(packdir)) != NULL) {
1193 if (!got_repo_is_packidx_filename(dent->d_name,
1194 strlen(dent->d_name)))
1197 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1198 dent->d_name) == -1) {
1199 err = got_error_from_errno("asprintf");
1200 path_packidx = NULL;
1204 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1211 if (packdir && closedir(packdir) != 0 && err == NULL)
1212 err = got_error_from_errno("closedir");
1216 const struct got_error *
1217 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1218 struct got_repository *repo)
1220 const struct got_error *err;
1225 /* Search pack index cache. */
1226 for (i = 0; i < repo->pack_cache_size; i++) {
1227 if (repo->packidx_cache[i] == NULL)
1229 if (strcmp(repo->packidx_cache[i]->path_packidx,
1230 path_packidx) == 0) {
1231 *packidx = repo->packidx_cache[i];
1235 /* No luck. Search the filesystem. */
1237 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1242 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1246 err = cache_packidx(repo, *packidx, path_packidx);
1249 got_packidx_close(*packidx);
1255 static const struct got_error *
1256 read_packfile_hdr(int fd, struct got_packidx *packidx)
1258 const struct got_error *err = NULL;
1259 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1260 struct got_packfile_hdr hdr;
1263 n = read(fd, &hdr, sizeof(hdr));
1265 return got_error_from_errno("read");
1266 if (n != sizeof(hdr))
1267 return got_error(GOT_ERR_BAD_PACKFILE);
1269 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1270 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1271 be32toh(hdr.nobjects) != totobj)
1272 err = got_error(GOT_ERR_BAD_PACKFILE);
1277 static const struct got_error *
1278 open_packfile(int *fd, struct got_repository *repo,
1279 const char *relpath, struct got_packidx *packidx)
1281 const struct got_error *err = NULL;
1283 *fd = openat(got_repo_get_fd(repo), relpath,
1284 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1286 return got_error_from_errno_fmt("openat: %s/%s",
1287 got_repo_get_path_git_dir(repo), relpath);
1290 err = read_packfile_hdr(*fd, packidx);
1300 const struct got_error *
1301 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1302 const char *path_packfile, struct got_packidx *packidx)
1304 const struct got_error *err = NULL;
1305 struct got_pack *pack = NULL;
1312 for (i = 0; i < repo->pack_cache_size; i++) {
1313 pack = &repo->packs[i];
1314 if (pack->path_packfile == NULL)
1316 if (strcmp(pack->path_packfile, path_packfile) == 0)
1317 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1320 if (i == repo->pack_cache_size) {
1321 err = got_pack_close(&repo->packs[i - 1]);
1324 memmove(&repo->packs[1], &repo->packs[0],
1325 sizeof(repo->packs) - sizeof(repo->packs[0]));
1329 pack = &repo->packs[i];
1331 pack->path_packfile = strdup(path_packfile);
1332 if (pack->path_packfile == NULL) {
1333 err = got_error_from_errno("strdup");
1337 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1341 if (fstat(pack->fd, &sb) != 0) {
1342 err = got_error_from_errno("fstat");
1345 pack->filesize = sb.st_size;
1347 pack->privsep_child = NULL;
1349 #ifndef GOT_PACK_NO_MMAP
1350 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1352 if (pack->map == MAP_FAILED) {
1353 if (errno != ENOMEM) {
1354 err = got_error_from_errno("mmap");
1357 pack->map = NULL; /* fall back to read(2) */
1363 free(pack->path_packfile);
1364 memset(pack, 0, sizeof(*pack));
1372 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1374 struct got_pack *pack = NULL;
1377 for (i = 0; i < repo->pack_cache_size; i++) {
1378 pack = &repo->packs[i];
1379 if (pack->path_packfile == NULL)
1381 if (strcmp(pack->path_packfile, path_packfile) == 0)
1388 const struct got_error *
1389 got_repo_init(const char *repo_path)
1391 const struct got_error *err = NULL;
1392 const char *dirnames[] = {
1394 GOT_OBJECTS_PACK_DIR,
1397 const char *description_str = "Unnamed repository; "
1398 "edit this file 'description' to name the repository.";
1399 const char *headref_str = "ref: refs/heads/main";
1400 const char *gitconfig_str = "[core]\n"
1401 "\trepositoryformatversion = 0\n"
1402 "\tfilemode = true\n"
1407 if (!got_path_dir_is_empty(repo_path))
1408 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1410 for (i = 0; i < nitems(dirnames); i++) {
1411 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1412 return got_error_from_errno("asprintf");
1414 err = got_path_mkdir(path);
1420 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1421 return got_error_from_errno("asprintf");
1422 err = got_path_create_file(path, description_str);
1427 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1428 return got_error_from_errno("asprintf");
1429 err = got_path_create_file(path, headref_str);
1434 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1435 return got_error_from_errno("asprintf");
1436 err = got_path_create_file(path, gitconfig_str);
1444 static const struct got_error *
1445 match_packed_object(struct got_object_id **unique_id,
1446 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1448 const struct got_error *err = NULL;
1449 struct got_object_id_queue matched_ids;
1450 struct got_pathlist_entry *pe;
1452 STAILQ_INIT(&matched_ids);
1454 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1455 const char *path_packidx = pe->path;
1456 struct got_packidx *packidx;
1457 struct got_object_qid *qid;
1459 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1464 err = got_packidx_match_id_str_prefix(&matched_ids,
1465 packidx, id_str_prefix);
1467 got_packidx_close(packidx);
1470 err = got_packidx_close(packidx);
1474 STAILQ_FOREACH(qid, &matched_ids, entry) {
1475 if (obj_type != GOT_OBJ_TYPE_ANY) {
1477 err = got_object_get_type(&matched_type, repo,
1481 if (matched_type != obj_type)
1484 if (*unique_id == NULL) {
1485 *unique_id = got_object_id_dup(qid->id);
1486 if (*unique_id == NULL) {
1487 err = got_error_from_errno("malloc");
1491 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1492 continue; /* packed multiple times */
1493 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1499 got_object_id_queue_free(&matched_ids);
1507 static const struct got_error *
1508 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1509 const char *object_dir, const char *id_str_prefix, int obj_type,
1510 struct got_repository *repo)
1512 const struct got_error *err = NULL;
1515 struct dirent *dent;
1516 struct got_object_id id;
1518 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1519 err = got_error_from_errno("asprintf");
1523 dir = opendir(path);
1525 if (errno == ENOENT) {
1529 err = got_error_from_errno2("opendir", path);
1532 while ((dent = readdir(dir)) != NULL) {
1536 if (strcmp(dent->d_name, ".") == 0 ||
1537 strcmp(dent->d_name, "..") == 0)
1540 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1541 err = got_error_from_errno("asprintf");
1545 if (!got_parse_sha1_digest(id.sha1, id_str))
1549 * Directory entries do not necessarily appear in
1550 * sorted order, so we must iterate over all of them.
1552 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1558 if (*unique_id == NULL) {
1559 if (obj_type != GOT_OBJ_TYPE_ANY) {
1561 err = got_object_get_type(&matched_type, repo,
1565 if (matched_type != obj_type)
1568 *unique_id = got_object_id_dup(&id);
1569 if (*unique_id == NULL) {
1570 err = got_error_from_errno("got_object_id_dup");
1575 if (got_object_id_cmp(*unique_id, &id) == 0)
1576 continue; /* both packed and loose */
1577 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1583 if (dir && closedir(dir) != 0 && err == NULL)
1584 err = got_error_from_errno("closedir");
1593 const struct got_error *
1594 got_repo_match_object_id_prefix(struct got_object_id **id,
1595 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 char *path_objects = got_repo_get_path_objects(repo);
1599 char *object_dir = NULL;
1605 len = strlen(id_str_prefix);
1606 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1607 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1609 for (i = 0; i < len; i++) {
1610 if (isxdigit((unsigned char)id_str_prefix[i]))
1612 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1616 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1619 object_dir = strndup(id_str_prefix, 2);
1620 if (object_dir == NULL) {
1621 err = got_error_from_errno("strdup");
1624 err = match_loose_object(id, path_objects, object_dir,
1625 id_str_prefix, obj_type, repo);
1626 } else if (len == 1) {
1628 for (i = 0; i < 0xf; i++) {
1629 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1631 err = got_error_from_errno("asprintf");
1634 err = match_packed_object(id, repo, object_dir,
1638 err = match_loose_object(id, path_objects, object_dir,
1639 id_str_prefix, obj_type, repo);
1644 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1652 } else if (*id == NULL) {
1654 case GOT_OBJ_TYPE_BLOB:
1655 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1656 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1658 case GOT_OBJ_TYPE_TREE:
1659 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1660 GOT_OBJ_LABEL_TREE, id_str_prefix);
1662 case GOT_OBJ_TYPE_COMMIT:
1663 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1664 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1666 case GOT_OBJ_TYPE_TAG:
1667 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1668 GOT_OBJ_LABEL_TAG, id_str_prefix);
1671 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1679 const struct got_error *
1680 got_repo_match_object_id(struct got_object_id **id, char **label,
1681 const char *id_str, int obj_type, struct got_reflist_head *refs,
1682 struct got_repository *repo)
1684 const struct got_error *err;
1685 struct got_tag_object *tag;
1686 struct got_reference *ref = NULL;
1693 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1696 *id = got_object_id_dup(
1697 got_object_tag_get_object_id(tag));
1699 err = got_error_from_errno("got_object_id_dup");
1700 else if (label && asprintf(label, "refs/tags/%s",
1701 got_object_tag_get_name(tag)) == -1) {
1702 err = got_error_from_errno("asprintf");
1706 got_object_tag_close(tag);
1708 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1709 err->code != GOT_ERR_NO_OBJ)
1713 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1715 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1717 err = got_ref_open(&ref, repo, id_str, 0);
1721 *label = strdup(got_ref_get_name(ref));
1722 if (*label == NULL) {
1723 err = got_error_from_errno("strdup");
1727 err = got_ref_resolve(id, repo, ref);
1729 err = got_object_id_str(label, *id);
1730 if (*label == NULL) {
1731 err = got_error_from_errno("strdup");
1741 const struct got_error *
1742 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1743 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1745 const struct got_error *err = NULL;
1746 struct got_reflist_entry *re;
1747 struct got_object_id *tag_id;
1748 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1752 TAILQ_FOREACH(re, refs, entry) {
1753 const char *refname;
1754 refname = got_ref_get_name(re->ref);
1755 if (got_ref_is_symbolic(re->ref))
1757 if (strncmp(refname, "refs/tags/", 10) != 0)
1759 if (!name_is_absolute)
1760 refname += strlen("refs/tags/");
1761 if (strcmp(refname, name) != 0)
1763 err = got_ref_resolve(&tag_id, repo, re->ref);
1766 err = got_object_open_as_tag(tag, repo, tag_id);
1770 if (obj_type == GOT_OBJ_TYPE_ANY ||
1771 got_object_tag_get_object_type(*tag) == obj_type)
1773 got_object_tag_close(*tag);
1777 if (err == NULL && *tag == NULL)
1778 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1779 GOT_OBJ_LABEL_TAG, name);
1783 static const struct got_error *
1784 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1785 const char *name, mode_t mode, struct got_object_id *blob_id)
1787 const struct got_error *err = NULL;
1791 *new_te = calloc(1, sizeof(**new_te));
1792 if (*new_te == NULL)
1793 return got_error_from_errno("calloc");
1795 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1796 sizeof((*new_te)->name)) {
1797 err = got_error(GOT_ERR_NO_SPACE);
1801 if (S_ISLNK(mode)) {
1802 (*new_te)->mode = S_IFLNK;
1804 (*new_te)->mode = S_IFREG;
1805 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1807 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1809 if (err && *new_te) {
1816 static const struct got_error *
1817 import_file(struct got_tree_entry **new_te, struct dirent *de,
1818 const char *path, struct got_repository *repo)
1820 const struct got_error *err;
1821 struct got_object_id *blob_id = NULL;
1825 if (asprintf(&filepath, "%s%s%s", path,
1826 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1827 return got_error_from_errno("asprintf");
1829 if (lstat(filepath, &sb) != 0) {
1830 err = got_error_from_errno2("lstat", path);
1834 err = got_object_blob_create(&blob_id, filepath, repo);
1838 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1847 static const struct got_error *
1848 insert_tree_entry(struct got_tree_entry *new_te,
1849 struct got_pathlist_head *paths)
1851 const struct got_error *err = NULL;
1852 struct got_pathlist_entry *new_pe;
1854 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1858 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1862 static const struct got_error *write_tree(struct got_object_id **,
1863 const char *, struct got_pathlist_head *, struct got_repository *,
1864 got_repo_import_cb progress_cb, void *progress_arg);
1866 static const struct got_error *
1867 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1868 const char *path, struct got_pathlist_head *ignores,
1869 struct got_repository *repo,
1870 got_repo_import_cb progress_cb, void *progress_arg)
1872 const struct got_error *err;
1873 struct got_object_id *id = NULL;
1876 if (asprintf(&subdirpath, "%s%s%s", path,
1877 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1878 return got_error_from_errno("asprintf");
1880 (*new_te) = calloc(1, sizeof(**new_te));
1881 if (*new_te == NULL)
1882 return got_error_from_errno("calloc");
1883 (*new_te)->mode = S_IFDIR;
1884 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1885 sizeof((*new_te)->name)) {
1886 err = got_error(GOT_ERR_NO_SPACE);
1889 err = write_tree(&id, subdirpath, ignores, repo,
1890 progress_cb, progress_arg);
1893 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1905 static const struct got_error *
1906 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1907 struct got_pathlist_head *ignores, struct got_repository *repo,
1908 got_repo_import_cb progress_cb, void *progress_arg)
1910 const struct got_error *err = NULL;
1914 struct got_tree_entry *new_te = NULL;
1915 struct got_pathlist_head paths;
1916 struct got_pathlist_entry *pe;
1918 *new_tree_id = NULL;
1922 dir = opendir(path_dir);
1924 err = got_error_from_errno2("opendir", path_dir);
1929 while ((de = readdir(dir)) != NULL) {
1933 if (strcmp(de->d_name, ".") == 0 ||
1934 strcmp(de->d_name, "..") == 0)
1937 TAILQ_FOREACH(pe, ignores, entry) {
1938 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1946 err = got_path_dirent_type(&type, path_dir, de);
1950 if (type == DT_DIR) {
1951 err = import_subdir(&new_te, de, path_dir,
1952 ignores, repo, progress_cb, progress_arg);
1954 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1959 } else if (type == DT_REG || type == DT_LNK) {
1960 err = import_file(&new_te, de, path_dir, repo);
1966 err = insert_tree_entry(new_te, &paths);
1972 if (TAILQ_EMPTY(&paths)) {
1973 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1974 "cannot create tree without any entries");
1978 TAILQ_FOREACH(pe, &paths, entry) {
1979 struct got_tree_entry *te = pe->data;
1981 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1983 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1984 err = got_error_from_errno("asprintf");
1987 err = (*progress_cb)(progress_arg, path);
1993 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1997 got_pathlist_free(&paths);
2001 const struct got_error *
2002 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2003 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2004 struct got_repository *repo, got_repo_import_cb progress_cb,
2007 const struct got_error *err;
2008 struct got_object_id *new_tree_id;
2010 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2011 progress_cb, progress_arg);
2015 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2016 author, time(NULL), author, time(NULL), logmsg, repo);
2021 const struct got_error *
2022 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2023 struct got_repository *repo)
2025 const struct got_error *err = NULL;
2026 char *path_objects = NULL, *path = NULL;
2028 struct got_object_id id;
2034 path_objects = got_repo_get_path_objects(repo);
2035 if (path_objects == NULL)
2036 return got_error_from_errno("got_repo_get_path_objects");
2038 for (i = 0; i <= 0xff; i++) {
2039 struct dirent *dent;
2041 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2042 err = got_error_from_errno("asprintf");
2046 dir = opendir(path);
2048 if (errno == ENOENT) {
2052 err = got_error_from_errno2("opendir", path);
2056 while ((dent = readdir(dir)) != NULL) {
2061 if (strcmp(dent->d_name, ".") == 0 ||
2062 strcmp(dent->d_name, "..") == 0)
2065 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2066 err = got_error_from_errno("asprintf");
2070 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2076 err = got_object_open_loose_fd(&fd, &id, repo);
2080 if (fstat(fd, &sb) == -1) {
2081 err = got_error_from_errno("fstat");
2086 (*ondisk_size) += sb.st_size;
2088 if (close(fd) == -1) {
2089 err = got_error_from_errno("close");
2094 if (closedir(dir) != 0) {
2095 err = got_error_from_errno("closedir");
2104 if (dir && closedir(dir) != 0 && err == NULL)
2105 err = got_error_from_errno("closedir");
2116 const struct got_error *
2117 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2118 off_t *total_packsize, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2121 DIR *packdir = NULL;
2122 struct dirent *dent;
2123 struct got_packidx *packidx = NULL;
2125 char *path_packfile;
2131 *total_packsize = 0;
2133 packdir_fd = openat(got_repo_get_fd(repo),
2134 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2135 if (packdir_fd == -1) {
2136 return got_error_from_errno_fmt("openat: %s/%s",
2137 got_repo_get_path_git_dir(repo),
2138 GOT_OBJECTS_PACK_DIR);
2141 packdir = fdopendir(packdir_fd);
2142 if (packdir == NULL) {
2143 err = got_error_from_errno("fdopendir");
2147 while ((dent = readdir(packdir)) != NULL) {
2148 if (!got_repo_is_packidx_filename(dent->d_name,
2149 strlen(dent->d_name)))
2152 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2153 dent->d_name) == -1) {
2154 err = got_error_from_errno("asprintf");
2158 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2164 if (fstat(packidx->fd, &sb) == -1)
2166 *total_packsize += sb.st_size;
2168 err = got_packidx_get_packfile_path(&path_packfile,
2169 packidx->path_packidx);
2173 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2175 free(path_packfile);
2178 free(path_packfile);
2179 *total_packsize += sb.st_size;
2181 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2185 got_packidx_close(packidx);
2190 got_packidx_close(packidx);
2191 if (packdir && closedir(packdir) != 0 && err == NULL)
2192 err = got_error_from_errno("closedir");
2196 *total_packsize = 0;
2201 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2202 got_packidx_bloom_filter_cmp);