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 const struct got_error *
351 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
352 struct got_raw_object *raw)
354 #ifndef GOT_NO_OBJ_CACHE
355 const struct got_error *err = NULL;
356 err = got_object_cache_add(&repo->rawcache, id, raw);
358 if (err->code == GOT_ERR_OBJ_EXISTS ||
359 err->code == GOT_ERR_OBJ_TOO_LARGE)
369 struct got_raw_object *
370 got_repo_get_cached_raw_object(struct got_repository *repo,
371 struct got_object_id *id)
373 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
377 static const struct got_error *
378 open_repo(struct got_repository *repo, const char *path)
380 const struct got_error *err = NULL;
382 repo->gitdir_fd = -1;
384 /* bare git repository? */
385 repo->path_git_dir = strdup(path);
386 if (repo->path_git_dir == NULL)
387 return got_error_from_errno("strdup");
388 if (is_git_repo(repo)) {
389 repo->path = strdup(repo->path_git_dir);
390 if (repo->path == NULL) {
391 err = got_error_from_errno("strdup");
394 repo->gitdir_fd = open(repo->path_git_dir,
395 O_DIRECTORY | O_CLOEXEC);
396 if (repo->gitdir_fd == -1) {
397 err = got_error_from_errno2("open",
404 /* git repository with working tree? */
405 free(repo->path_git_dir);
406 repo->path_git_dir = NULL;
407 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
408 err = got_error_from_errno("asprintf");
411 if (is_git_repo(repo)) {
412 repo->path = strdup(path);
413 if (repo->path == NULL) {
414 err = got_error_from_errno("strdup");
417 repo->gitdir_fd = open(repo->path_git_dir,
418 O_DIRECTORY | O_CLOEXEC);
419 if (repo->gitdir_fd == -1) {
420 err = got_error_from_errno2("open",
427 err = got_error(GOT_ERR_NOT_GIT_REPO);
432 free(repo->path_git_dir);
433 repo->path_git_dir = NULL;
434 if (repo->gitdir_fd != -1)
435 close(repo->gitdir_fd);
436 repo->gitdir_fd = -1;
442 static const struct got_error *
443 parse_gitconfig_file(int *gitconfig_repository_format_version,
444 char **gitconfig_author_name, char **gitconfig_author_email,
445 struct got_remote_repo **remotes, int *nremotes,
446 char **gitconfig_owner, char ***extensions, int *nextensions,
447 const char *gitconfig_path)
449 const struct got_error *err = NULL, *child_err = NULL;
451 int imsg_fds[2] = { -1, -1 };
453 struct imsgbuf *ibuf;
455 *gitconfig_repository_format_version = 0;
460 *gitconfig_author_name = NULL;
461 *gitconfig_author_email = NULL;
467 *gitconfig_owner = NULL;
469 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
473 return got_error_from_errno2("open", gitconfig_path);
476 ibuf = calloc(1, sizeof(*ibuf));
478 err = got_error_from_errno("calloc");
482 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
483 err = got_error_from_errno("socketpair");
489 err = got_error_from_errno("fork");
491 } else if (pid == 0) {
492 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
497 if (close(imsg_fds[1]) == -1) {
498 err = got_error_from_errno("close");
502 imsg_init(ibuf, imsg_fds[0]);
504 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
509 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
513 err = got_privsep_recv_gitconfig_int(
514 gitconfig_repository_format_version, ibuf);
518 if (extensions && nextensions) {
519 err = got_privsep_send_gitconfig_repository_extensions_req(
523 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
526 if (*nextensions > 0) {
528 *extensions = calloc(*nextensions, sizeof(char *));
529 if (*extensions == NULL) {
530 err = got_error_from_errno("calloc");
533 for (i = 0; i < *nextensions; i++) {
535 err = got_privsep_recv_gitconfig_str(&ext,
539 (*extensions)[i] = ext;
544 err = got_privsep_send_gitconfig_author_name_req(ibuf);
548 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
552 err = got_privsep_send_gitconfig_author_email_req(ibuf);
556 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
560 if (remotes && nremotes) {
561 err = got_privsep_send_gitconfig_remotes_req(ibuf);
565 err = got_privsep_recv_gitconfig_remotes(remotes,
571 if (gitconfig_owner) {
572 err = got_privsep_send_gitconfig_owner_req(ibuf);
575 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
580 err = got_privsep_send_stop(imsg_fds[0]);
581 child_err = got_privsep_wait_for_child(pid);
582 if (child_err && err == NULL)
585 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
586 err = got_error_from_errno("close");
587 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
588 err = got_error_from_errno("close");
589 if (fd != -1 && close(fd) == -1 && err == NULL)
590 err = got_error_from_errno2("close", gitconfig_path);
595 static const struct got_error *
596 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
598 const struct got_error *err = NULL;
599 char *repo_gitconfig_path = NULL;
601 if (global_gitconfig_path) {
602 /* Read settings from ~/.gitconfig. */
603 int dummy_repo_version;
604 err = parse_gitconfig_file(&dummy_repo_version,
605 &repo->global_gitconfig_author_name,
606 &repo->global_gitconfig_author_email,
607 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
612 /* Read repository's .git/config file. */
613 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
614 if (repo_gitconfig_path == NULL)
615 return got_error_from_errno("got_repo_get_path_gitconfig");
617 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
618 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
619 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
620 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
621 repo_gitconfig_path);
625 free(repo_gitconfig_path);
629 static const struct got_error *
630 read_gotconfig(struct got_repository *repo)
632 const struct got_error *err = NULL;
633 char *gotconfig_path;
635 gotconfig_path = got_repo_get_path_gotconfig(repo);
636 if (gotconfig_path == NULL)
637 return got_error_from_errno("got_repo_get_path_gotconfig");
639 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
640 free(gotconfig_path);
644 /* Supported repository format extensions. */
645 static const char *repo_extensions[] = {
646 "noop", /* Got supports repository format version 1. */
647 "preciousObjects", /* Supported by gotadmin cleanup. */
648 "worktreeConfig", /* Got does not care about Git work trees. */
651 const struct got_error *
652 got_repo_open(struct got_repository **repop, const char *path,
653 const char *global_gitconfig_path)
655 struct got_repository *repo = NULL;
656 const struct got_error *err = NULL;
657 char *repo_path = NULL;
663 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
664 return got_error_from_errno("getrlimit");
666 repo = calloc(1, sizeof(*repo));
668 err = got_error_from_errno("calloc");
672 RB_INIT(&repo->packidx_bloom_filters);
674 for (i = 0; i < nitems(repo->privsep_children); i++) {
675 memset(&repo->privsep_children[i], 0,
676 sizeof(repo->privsep_children[0]));
677 repo->privsep_children[i].imsg_fd = -1;
680 err = got_object_cache_init(&repo->objcache,
681 GOT_OBJECT_CACHE_TYPE_OBJ);
684 err = got_object_cache_init(&repo->treecache,
685 GOT_OBJECT_CACHE_TYPE_TREE);
688 err = got_object_cache_init(&repo->commitcache,
689 GOT_OBJECT_CACHE_TYPE_COMMIT);
692 err = got_object_cache_init(&repo->tagcache,
693 GOT_OBJECT_CACHE_TYPE_TAG);
696 err = got_object_cache_init(&repo->rawcache,
697 GOT_OBJECT_CACHE_TYPE_RAW);
701 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
702 if (repo->pack_cache_size > rl.rlim_cur / 8)
703 repo->pack_cache_size = rl.rlim_cur / 8;
705 repo_path = realpath(path, NULL);
706 if (repo_path == NULL) {
707 err = got_error_from_errno2("realpath", path);
714 err = open_repo(repo, repo_path);
717 if (err->code != GOT_ERR_NOT_GIT_REPO)
719 if (repo_path[0] == '/' && repo_path[1] == '\0') {
720 err = got_error(GOT_ERR_NOT_GIT_REPO);
723 err = got_path_dirname(&parent_path, repo_path);
727 repo_path = parent_path;
730 err = read_gotconfig(repo);
734 err = read_gitconfig(repo, global_gitconfig_path);
737 if (repo->gitconfig_repository_format_version != 0)
738 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
739 for (i = 0; i < repo->nextensions; i++) {
740 char *ext = repo->extensions[i];
741 int j, supported = 0;
742 for (j = 0; j < nitems(repo_extensions); j++) {
743 if (strcmp(ext, repo_extensions[j]) == 0) {
749 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
755 got_repo_close(repo);
762 const struct got_error *
763 got_repo_close(struct got_repository *repo)
765 const struct got_error *err = NULL, *child_err;
766 struct got_packidx_bloom_filter *bf;
769 for (i = 0; i < repo->pack_cache_size; i++) {
770 if (repo->packidx_cache[i] == NULL)
772 got_packidx_close(repo->packidx_cache[i]);
775 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
776 &repo->packidx_bloom_filters))) {
777 RB_REMOVE(got_packidx_bloom_filter_tree,
778 &repo->packidx_bloom_filters, bf);
783 for (i = 0; i < repo->pack_cache_size; i++) {
784 if (repo->packs[i].path_packfile == NULL)
786 got_pack_close(&repo->packs[i]);
790 free(repo->path_git_dir);
792 got_object_cache_close(&repo->objcache);
793 got_object_cache_close(&repo->treecache);
794 got_object_cache_close(&repo->commitcache);
795 got_object_cache_close(&repo->tagcache);
796 got_object_cache_close(&repo->rawcache);
798 for (i = 0; i < nitems(repo->privsep_children); i++) {
799 if (repo->privsep_children[i].imsg_fd == -1)
801 imsg_clear(repo->privsep_children[i].ibuf);
802 free(repo->privsep_children[i].ibuf);
803 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
804 child_err = got_privsep_wait_for_child(
805 repo->privsep_children[i].pid);
806 if (child_err && err == NULL)
808 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
810 err = got_error_from_errno("close");
813 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
815 err = got_error_from_errno("close");
818 got_gotconfig_free(repo->gotconfig);
819 free(repo->gitconfig_author_name);
820 free(repo->gitconfig_author_email);
821 for (i = 0; i < repo->ngitconfig_remotes; i++)
822 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
823 free(repo->gitconfig_remotes);
824 for (i = 0; i < repo->nextensions; i++)
825 free(repo->extensions[i]);
826 free(repo->extensions);
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 DIR *packdir = NULL;
1096 struct dirent *dent;
1101 /* Search pack index cache. */
1102 for (i = 0; i < repo->pack_cache_size; i++) {
1103 if (repo->packidx_cache[i] == NULL)
1105 if (!got_repo_check_packidx_bloom_filter(repo,
1106 repo->packidx_cache[i]->path_packidx, id))
1107 continue; /* object will not be found in this index */
1108 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1110 *packidx = repo->packidx_cache[i];
1112 * Move this cache entry to the front. Repeatedly
1113 * searching a wrong pack index can be expensive.
1116 memmove(&repo->packidx_cache[1],
1117 &repo->packidx_cache[0],
1118 i * sizeof(repo->packidx_cache[0]));
1119 repo->packidx_cache[0] = *packidx;
1124 /* No luck. Search the filesystem. */
1126 packdir_fd = openat(got_repo_get_fd(repo),
1127 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1128 if (packdir_fd == -1) {
1129 if (errno == ENOENT)
1130 err = got_error_no_obj(id);
1132 err = got_error_from_errno_fmt("openat: %s/%s",
1133 got_repo_get_path_git_dir(repo),
1134 GOT_OBJECTS_PACK_DIR);
1138 packdir = fdopendir(packdir_fd);
1139 if (packdir == NULL) {
1140 err = got_error_from_errno("fdopendir");
1144 while ((dent = readdir(packdir)) != NULL) {
1147 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1150 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1151 dent->d_name) == -1) {
1152 err = got_error_from_errno("asprintf");
1156 if (!got_repo_check_packidx_bloom_filter(repo,
1157 path_packidx, id)) {
1159 continue; /* object will not be found in this index */
1162 for (i = 0; i < repo->pack_cache_size; i++) {
1163 if (repo->packidx_cache[i] == NULL)
1165 if (strcmp(repo->packidx_cache[i]->path_packidx,
1166 path_packidx) == 0) {
1173 continue; /* already searched */
1176 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1183 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1189 err = cache_packidx(repo, *packidx, path_packidx);
1194 *idx = got_packidx_get_object_idx(*packidx, id);
1196 err = NULL; /* found the object */
1201 err = got_error_no_obj(id);
1203 if (packdir && closedir(packdir) != 0 && err == NULL)
1204 err = got_error_from_errno("closedir");
1208 const struct got_error *
1209 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1210 struct got_repository *repo)
1212 const struct got_error *err = NULL;
1213 DIR *packdir = NULL;
1214 struct dirent *dent;
1215 char *path_packidx = NULL;
1218 packdir_fd = openat(got_repo_get_fd(repo),
1219 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1220 if (packdir_fd == -1) {
1221 return got_error_from_errno_fmt("openat: %s/%s",
1222 got_repo_get_path_git_dir(repo),
1223 GOT_OBJECTS_PACK_DIR);
1226 packdir = fdopendir(packdir_fd);
1227 if (packdir == NULL) {
1228 err = got_error_from_errno("fdopendir");
1232 while ((dent = readdir(packdir)) != NULL) {
1233 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1236 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1237 dent->d_name) == -1) {
1238 err = got_error_from_errno("asprintf");
1239 path_packidx = NULL;
1243 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1250 if (packdir && closedir(packdir) != 0 && err == NULL)
1251 err = got_error_from_errno("closedir");
1255 const struct got_error *
1256 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1257 struct got_repository *repo)
1259 const struct got_error *err;
1264 /* Search pack index cache. */
1265 for (i = 0; i < repo->pack_cache_size; i++) {
1266 if (repo->packidx_cache[i] == NULL)
1268 if (strcmp(repo->packidx_cache[i]->path_packidx,
1269 path_packidx) == 0) {
1270 *packidx = repo->packidx_cache[i];
1274 /* No luck. Search the filesystem. */
1276 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1281 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1285 err = cache_packidx(repo, *packidx, path_packidx);
1288 got_packidx_close(*packidx);
1294 static const struct got_error *
1295 read_packfile_hdr(int fd, struct got_packidx *packidx)
1297 const struct got_error *err = NULL;
1298 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1299 struct got_packfile_hdr hdr;
1302 n = read(fd, &hdr, sizeof(hdr));
1304 return got_error_from_errno("read");
1305 if (n != sizeof(hdr))
1306 return got_error(GOT_ERR_BAD_PACKFILE);
1308 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1309 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1310 be32toh(hdr.nobjects) != totobj)
1311 err = got_error(GOT_ERR_BAD_PACKFILE);
1316 static const struct got_error *
1317 open_packfile(int *fd, struct got_repository *repo,
1318 const char *relpath, struct got_packidx *packidx)
1320 const struct got_error *err = NULL;
1322 *fd = openat(got_repo_get_fd(repo), relpath,
1323 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1325 return got_error_from_errno_fmt("openat: %s/%s",
1326 got_repo_get_path_git_dir(repo), relpath);
1329 err = read_packfile_hdr(*fd, packidx);
1339 const struct got_error *
1340 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1341 const char *path_packfile, struct got_packidx *packidx)
1343 const struct got_error *err = NULL;
1344 struct got_pack *pack = NULL;
1351 for (i = 0; i < repo->pack_cache_size; i++) {
1352 pack = &repo->packs[i];
1353 if (pack->path_packfile == NULL)
1355 if (strcmp(pack->path_packfile, path_packfile) == 0)
1356 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1359 if (i == repo->pack_cache_size) {
1360 err = got_pack_close(&repo->packs[i - 1]);
1363 memmove(&repo->packs[1], &repo->packs[0],
1364 sizeof(repo->packs) - sizeof(repo->packs[0]));
1368 pack = &repo->packs[i];
1370 pack->path_packfile = strdup(path_packfile);
1371 if (pack->path_packfile == NULL) {
1372 err = got_error_from_errno("strdup");
1376 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1380 if (fstat(pack->fd, &sb) != 0) {
1381 err = got_error_from_errno("fstat");
1384 pack->filesize = sb.st_size;
1386 pack->privsep_child = NULL;
1388 #ifndef GOT_PACK_NO_MMAP
1389 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1391 if (pack->map == MAP_FAILED) {
1392 if (errno != ENOMEM) {
1393 err = got_error_from_errno("mmap");
1396 pack->map = NULL; /* fall back to read(2) */
1402 free(pack->path_packfile);
1403 memset(pack, 0, sizeof(*pack));
1411 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1413 struct got_pack *pack = NULL;
1416 for (i = 0; i < repo->pack_cache_size; i++) {
1417 pack = &repo->packs[i];
1418 if (pack->path_packfile == NULL)
1420 if (strcmp(pack->path_packfile, path_packfile) == 0)
1427 const struct got_error *
1428 got_repo_init(const char *repo_path)
1430 const struct got_error *err = NULL;
1431 const char *dirnames[] = {
1433 GOT_OBJECTS_PACK_DIR,
1436 const char *description_str = "Unnamed repository; "
1437 "edit this file 'description' to name the repository.";
1438 const char *headref_str = "ref: refs/heads/main";
1439 const char *gitconfig_str = "[core]\n"
1440 "\trepositoryformatversion = 0\n"
1441 "\tfilemode = true\n"
1446 if (!got_path_dir_is_empty(repo_path))
1447 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1449 for (i = 0; i < nitems(dirnames); i++) {
1450 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1451 return got_error_from_errno("asprintf");
1453 err = got_path_mkdir(path);
1459 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1460 return got_error_from_errno("asprintf");
1461 err = got_path_create_file(path, description_str);
1466 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1467 return got_error_from_errno("asprintf");
1468 err = got_path_create_file(path, headref_str);
1473 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1474 return got_error_from_errno("asprintf");
1475 err = got_path_create_file(path, gitconfig_str);
1483 static const struct got_error *
1484 match_packed_object(struct got_object_id **unique_id,
1485 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1487 const struct got_error *err = NULL;
1488 DIR *packdir = NULL;
1489 struct dirent *dent;
1491 struct got_object_id_queue matched_ids;
1494 STAILQ_INIT(&matched_ids);
1496 packdir_fd = openat(got_repo_get_fd(repo),
1497 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1498 if (packdir_fd == -1) {
1499 if (errno != ENOENT) {
1500 err = got_error_from_errno2("openat",
1501 GOT_OBJECTS_PACK_DIR);
1506 packdir = fdopendir(packdir_fd);
1507 if (packdir == NULL) {
1508 err = got_error_from_errno("fdopendir");
1512 while ((dent = readdir(packdir)) != NULL) {
1513 struct got_packidx *packidx;
1514 struct got_object_qid *qid;
1516 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1519 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1520 dent->d_name) == -1) {
1521 err = got_error_from_errno("strdup");
1525 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1531 err = got_packidx_match_id_str_prefix(&matched_ids,
1532 packidx, id_str_prefix);
1534 got_packidx_close(packidx);
1537 err = got_packidx_close(packidx);
1541 STAILQ_FOREACH(qid, &matched_ids, entry) {
1542 if (obj_type != GOT_OBJ_TYPE_ANY) {
1544 err = got_object_get_type(&matched_type, repo,
1548 if (matched_type != obj_type)
1551 if (*unique_id == NULL) {
1552 *unique_id = got_object_id_dup(qid->id);
1553 if (*unique_id == NULL) {
1554 err = got_error_from_errno("malloc");
1558 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1559 continue; /* packed multiple times */
1560 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1566 got_object_id_queue_free(&matched_ids);
1567 if (packdir && closedir(packdir) != 0 && err == NULL)
1568 err = got_error_from_errno("closedir");
1576 static const struct got_error *
1577 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1578 const char *object_dir, const char *id_str_prefix, int obj_type,
1579 struct got_repository *repo)
1581 const struct got_error *err = NULL;
1584 struct dirent *dent;
1585 struct got_object_id id;
1587 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1588 err = got_error_from_errno("asprintf");
1592 dir = opendir(path);
1594 if (errno == ENOENT) {
1598 err = got_error_from_errno2("opendir", path);
1601 while ((dent = readdir(dir)) != NULL) {
1605 if (strcmp(dent->d_name, ".") == 0 ||
1606 strcmp(dent->d_name, "..") == 0)
1609 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1610 err = got_error_from_errno("asprintf");
1614 if (!got_parse_sha1_digest(id.sha1, id_str))
1618 * Directory entries do not necessarily appear in
1619 * sorted order, so we must iterate over all of them.
1621 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1627 if (*unique_id == NULL) {
1628 if (obj_type != GOT_OBJ_TYPE_ANY) {
1630 err = got_object_get_type(&matched_type, repo,
1634 if (matched_type != obj_type)
1637 *unique_id = got_object_id_dup(&id);
1638 if (*unique_id == NULL) {
1639 err = got_error_from_errno("got_object_id_dup");
1644 if (got_object_id_cmp(*unique_id, &id) == 0)
1645 continue; /* both packed and loose */
1646 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1652 if (dir && closedir(dir) != 0 && err == NULL)
1653 err = got_error_from_errno("closedir");
1662 const struct got_error *
1663 got_repo_match_object_id_prefix(struct got_object_id **id,
1664 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1666 const struct got_error *err = NULL;
1667 char *path_objects = got_repo_get_path_objects(repo);
1668 char *object_dir = NULL;
1674 len = strlen(id_str_prefix);
1675 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1676 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1678 for (i = 0; i < len; i++) {
1679 if (isxdigit((unsigned char)id_str_prefix[i]))
1681 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1685 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1688 object_dir = strndup(id_str_prefix, 2);
1689 if (object_dir == NULL) {
1690 err = got_error_from_errno("strdup");
1693 err = match_loose_object(id, path_objects, object_dir,
1694 id_str_prefix, obj_type, repo);
1695 } else if (len == 1) {
1697 for (i = 0; i < 0xf; i++) {
1698 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1700 err = got_error_from_errno("asprintf");
1703 err = match_packed_object(id, repo, object_dir,
1707 err = match_loose_object(id, path_objects, object_dir,
1708 id_str_prefix, obj_type, repo);
1713 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1721 } else if (*id == NULL) {
1723 case GOT_OBJ_TYPE_BLOB:
1724 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1725 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1727 case GOT_OBJ_TYPE_TREE:
1728 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1729 GOT_OBJ_LABEL_TREE, id_str_prefix);
1731 case GOT_OBJ_TYPE_COMMIT:
1732 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1733 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1735 case GOT_OBJ_TYPE_TAG:
1736 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1737 GOT_OBJ_LABEL_TAG, id_str_prefix);
1740 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1748 const struct got_error *
1749 got_repo_match_object_id(struct got_object_id **id, char **label,
1750 const char *id_str, int obj_type, struct got_reflist_head *refs,
1751 struct got_repository *repo)
1753 const struct got_error *err;
1754 struct got_tag_object *tag;
1755 struct got_reference *ref = NULL;
1762 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1765 *id = got_object_id_dup(
1766 got_object_tag_get_object_id(tag));
1768 err = got_error_from_errno("got_object_id_dup");
1769 else if (label && asprintf(label, "refs/tags/%s",
1770 got_object_tag_get_name(tag)) == -1) {
1771 err = got_error_from_errno("asprintf");
1775 got_object_tag_close(tag);
1777 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1778 err->code != GOT_ERR_NO_OBJ)
1782 err = got_ref_open(&ref, repo, id_str, 0);
1784 err = got_ref_resolve(id, repo, ref);
1788 *label = strdup(got_ref_get_name(ref));
1789 if (*label == NULL) {
1790 err = got_error_from_errno("strdup");
1795 if (err->code != GOT_ERR_NOT_REF &&
1796 err->code != GOT_ERR_BAD_REF_NAME)
1798 err = got_repo_match_object_id_prefix(id, id_str,
1801 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1802 err = got_error_not_ref(id_str);
1806 err = got_object_id_str(label, *id);
1807 if (*label == NULL) {
1808 err = got_error_from_errno("strdup");
1819 const struct got_error *
1820 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1821 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1823 const struct got_error *err = NULL;
1824 struct got_reflist_entry *re;
1825 struct got_object_id *tag_id;
1826 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1830 TAILQ_FOREACH(re, refs, entry) {
1831 const char *refname;
1832 refname = got_ref_get_name(re->ref);
1833 if (got_ref_is_symbolic(re->ref))
1835 if (strncmp(refname, "refs/tags/", 10) != 0)
1837 if (!name_is_absolute)
1838 refname += strlen("refs/tags/");
1839 if (strcmp(refname, name) != 0)
1841 err = got_ref_resolve(&tag_id, repo, re->ref);
1844 err = got_object_open_as_tag(tag, repo, tag_id);
1848 if (obj_type == GOT_OBJ_TYPE_ANY ||
1849 got_object_tag_get_object_type(*tag) == obj_type)
1851 got_object_tag_close(*tag);
1855 if (err == NULL && *tag == NULL)
1856 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1857 GOT_OBJ_LABEL_TAG, name);
1861 static const struct got_error *
1862 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1863 const char *name, mode_t mode, struct got_object_id *blob_id)
1865 const struct got_error *err = NULL;
1869 *new_te = calloc(1, sizeof(**new_te));
1870 if (*new_te == NULL)
1871 return got_error_from_errno("calloc");
1873 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1874 sizeof((*new_te)->name)) {
1875 err = got_error(GOT_ERR_NO_SPACE);
1879 if (S_ISLNK(mode)) {
1880 (*new_te)->mode = S_IFLNK;
1882 (*new_te)->mode = S_IFREG;
1883 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1885 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1887 if (err && *new_te) {
1894 static const struct got_error *
1895 import_file(struct got_tree_entry **new_te, struct dirent *de,
1896 const char *path, struct got_repository *repo)
1898 const struct got_error *err;
1899 struct got_object_id *blob_id = NULL;
1903 if (asprintf(&filepath, "%s%s%s", path,
1904 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1905 return got_error_from_errno("asprintf");
1907 if (lstat(filepath, &sb) != 0) {
1908 err = got_error_from_errno2("lstat", path);
1912 err = got_object_blob_create(&blob_id, filepath, repo);
1916 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1925 static const struct got_error *
1926 insert_tree_entry(struct got_tree_entry *new_te,
1927 struct got_pathlist_head *paths)
1929 const struct got_error *err = NULL;
1930 struct got_pathlist_entry *new_pe;
1932 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1936 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1940 static const struct got_error *write_tree(struct got_object_id **,
1941 const char *, struct got_pathlist_head *, struct got_repository *,
1942 got_repo_import_cb progress_cb, void *progress_arg);
1944 static const struct got_error *
1945 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1946 const char *path, struct got_pathlist_head *ignores,
1947 struct got_repository *repo,
1948 got_repo_import_cb progress_cb, void *progress_arg)
1950 const struct got_error *err;
1951 struct got_object_id *id = NULL;
1954 if (asprintf(&subdirpath, "%s%s%s", path,
1955 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1956 return got_error_from_errno("asprintf");
1958 (*new_te) = calloc(1, sizeof(**new_te));
1959 if (*new_te == NULL)
1960 return got_error_from_errno("calloc");
1961 (*new_te)->mode = S_IFDIR;
1962 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1963 sizeof((*new_te)->name)) {
1964 err = got_error(GOT_ERR_NO_SPACE);
1967 err = write_tree(&id, subdirpath, ignores, repo,
1968 progress_cb, progress_arg);
1971 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1983 static const struct got_error *
1984 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1985 struct got_pathlist_head *ignores, struct got_repository *repo,
1986 got_repo_import_cb progress_cb, void *progress_arg)
1988 const struct got_error *err = NULL;
1992 struct got_tree_entry *new_te = NULL;
1993 struct got_pathlist_head paths;
1994 struct got_pathlist_entry *pe;
1996 *new_tree_id = NULL;
2000 dir = opendir(path_dir);
2002 err = got_error_from_errno2("opendir", path_dir);
2007 while ((de = readdir(dir)) != NULL) {
2011 if (strcmp(de->d_name, ".") == 0 ||
2012 strcmp(de->d_name, "..") == 0)
2015 TAILQ_FOREACH(pe, ignores, entry) {
2016 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2024 err = got_path_dirent_type(&type, path_dir, de);
2028 if (type == DT_DIR) {
2029 err = import_subdir(&new_te, de, path_dir,
2030 ignores, repo, progress_cb, progress_arg);
2032 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2037 } else if (type == DT_REG || type == DT_LNK) {
2038 err = import_file(&new_te, de, path_dir, repo);
2044 err = insert_tree_entry(new_te, &paths);
2050 if (TAILQ_EMPTY(&paths)) {
2051 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2052 "cannot create tree without any entries");
2056 TAILQ_FOREACH(pe, &paths, entry) {
2057 struct got_tree_entry *te = pe->data;
2059 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2061 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2062 err = got_error_from_errno("asprintf");
2065 err = (*progress_cb)(progress_arg, path);
2071 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2075 got_pathlist_free(&paths);
2079 const struct got_error *
2080 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2081 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2082 struct got_repository *repo, got_repo_import_cb progress_cb,
2085 const struct got_error *err;
2086 struct got_object_id *new_tree_id;
2088 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2089 progress_cb, progress_arg);
2093 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2094 author, time(NULL), author, time(NULL), logmsg, repo);
2099 const struct got_error *
2100 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2101 struct got_repository *repo)
2103 const struct got_error *err = NULL;
2104 char *path_objects = NULL, *path = NULL;
2106 struct got_object_id id;
2112 path_objects = got_repo_get_path_objects(repo);
2113 if (path_objects == NULL)
2114 return got_error_from_errno("got_repo_get_path_objects");
2116 for (i = 0; i <= 0xff; i++) {
2117 struct dirent *dent;
2119 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2120 err = got_error_from_errno("asprintf");
2124 dir = opendir(path);
2126 if (errno == ENOENT) {
2130 err = got_error_from_errno2("opendir", path);
2134 while ((dent = readdir(dir)) != NULL) {
2139 if (strcmp(dent->d_name, ".") == 0 ||
2140 strcmp(dent->d_name, "..") == 0)
2143 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2144 err = got_error_from_errno("asprintf");
2148 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2154 err = got_object_open_loose_fd(&fd, &id, repo);
2158 if (fstat(fd, &sb) == -1) {
2159 err = got_error_from_errno("fstat");
2164 (*ondisk_size) += sb.st_size;
2166 if (close(fd) == -1) {
2167 err = got_error_from_errno("close");
2172 if (closedir(dir) != 0) {
2173 err = got_error_from_errno("closedir");
2182 if (dir && closedir(dir) != 0 && err == NULL)
2183 err = got_error_from_errno("closedir");
2194 const struct got_error *
2195 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2196 off_t *total_packsize, struct got_repository *repo)
2198 const struct got_error *err = NULL;
2199 DIR *packdir = NULL;
2200 struct dirent *dent;
2201 struct got_packidx *packidx = NULL;
2203 char *path_packfile;
2209 *total_packsize = 0;
2211 packdir_fd = openat(got_repo_get_fd(repo),
2212 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2213 if (packdir_fd == -1) {
2214 return got_error_from_errno_fmt("openat: %s/%s",
2215 got_repo_get_path_git_dir(repo),
2216 GOT_OBJECTS_PACK_DIR);
2219 packdir = fdopendir(packdir_fd);
2220 if (packdir == NULL) {
2221 err = got_error_from_errno("fdopendir");
2225 while ((dent = readdir(packdir)) != NULL) {
2226 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2229 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2230 dent->d_name) == -1) {
2231 err = got_error_from_errno("asprintf");
2235 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2241 if (fstat(packidx->fd, &sb) == -1)
2243 *total_packsize += sb.st_size;
2245 err = got_packidx_get_packfile_path(&path_packfile,
2246 packidx->path_packidx);
2250 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2252 free(path_packfile);
2255 free(path_packfile);
2256 *total_packsize += sb.st_size;
2258 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2262 got_packidx_close(packidx);
2267 got_packidx_close(packidx);
2268 if (packdir && closedir(packdir) != 0 && err == NULL)
2269 err = got_error_from_errno("closedir");
2273 *total_packsize = 0;
2278 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2279 got_packidx_bloom_filter_cmp);