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"
47 #include "got_opentemp.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
66 got_packidx_bloom_filter_cmp);
69 got_repo_get_path(struct got_repository *repo)
75 got_repo_get_path_git_dir(struct got_repository *repo)
77 return repo->path_git_dir;
81 got_repo_get_fd(struct got_repository *repo)
83 return repo->gitdir_fd;
87 got_repo_get_gitconfig_author_name(struct got_repository *repo)
89 return repo->gitconfig_author_name;
93 got_repo_get_gitconfig_author_email(struct got_repository *repo)
95 return repo->gitconfig_author_email;
99 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
101 return repo->global_gitconfig_author_name;
105 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
107 return repo->global_gitconfig_author_email;
111 got_repo_get_gitconfig_owner(struct got_repository *repo)
113 return repo->gitconfig_owner;
117 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
118 struct got_repository *repo)
120 *extensions = repo->extensions;
121 *nextensions = repo->nextensions;
125 got_repo_is_bare(struct got_repository *repo)
127 return (strcmp(repo->path, repo->path_git_dir) == 0);
131 get_path_git_child(struct got_repository *repo, const char *basename)
135 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
143 got_repo_get_path_objects(struct got_repository *repo)
145 return get_path_git_child(repo, GOT_OBJECTS_DIR);
149 got_repo_get_path_objects_pack(struct got_repository *repo)
151 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
155 got_repo_get_path_refs(struct got_repository *repo)
157 return get_path_git_child(repo, GOT_REFS_DIR);
161 got_repo_get_path_packed_refs(struct got_repository *repo)
163 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
167 get_path_head(struct got_repository *repo)
169 return get_path_git_child(repo, GOT_HEAD_FILE);
173 got_repo_get_path_gitconfig(struct got_repository *repo)
175 return get_path_git_child(repo, GOT_GITCONFIG);
179 got_repo_get_path_gotconfig(struct got_repository *repo)
181 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
184 const struct got_gotconfig *
185 got_repo_get_gotconfig(struct got_repository *repo)
187 return repo->gotconfig;
191 got_repo_get_gitconfig_remotes(int *nremotes,
192 const struct got_remote_repo **remotes, struct got_repository *repo)
194 *nremotes = repo->ngitconfig_remotes;
195 *remotes = repo->gitconfig_remotes;
199 is_git_repo(struct got_repository *repo)
201 const char *path_git = got_repo_get_path_git_dir(repo);
202 char *path_objects = got_repo_get_path_objects(repo);
203 char *path_refs = got_repo_get_path_refs(repo);
204 char *path_head = get_path_head(repo);
207 struct got_reference *head_ref;
209 if (lstat(path_git, &sb) == -1)
211 if (!S_ISDIR(sb.st_mode))
214 if (lstat(path_objects, &sb) == -1)
216 if (!S_ISDIR(sb.st_mode))
219 if (lstat(path_refs, &sb) == -1)
221 if (!S_ISDIR(sb.st_mode))
224 if (lstat(path_head, &sb) == -1)
226 if (!S_ISREG(sb.st_mode))
229 /* Check if the HEAD reference can be opened. */
230 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
232 got_ref_close(head_ref);
243 const struct got_error *
244 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
245 struct got_object *obj)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->objcache, id, obj);
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
262 got_repo_get_cached_object(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
268 const struct got_error *
269 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
270 struct got_tree_object *tree)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->treecache, id, tree);
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
286 struct got_tree_object *
287 got_repo_get_cached_tree(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_tree_object *)got_object_cache_get(
291 &repo->treecache, id);
294 const struct got_error *
295 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
296 struct got_commit_object *commit)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->commitcache, id, commit);
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
312 struct got_commit_object *
313 got_repo_get_cached_commit(struct got_repository *repo,
314 struct got_object_id *id)
316 return (struct got_commit_object *)got_object_cache_get(
317 &repo->commitcache, id);
320 const struct got_error *
321 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
322 struct got_tag_object *tag)
324 #ifndef GOT_NO_OBJ_CACHE
325 const struct got_error *err = NULL;
326 err = got_object_cache_add(&repo->tagcache, id, tag);
328 if (err->code == GOT_ERR_OBJ_EXISTS ||
329 err->code == GOT_ERR_OBJ_TOO_LARGE)
338 struct got_tag_object *
339 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
341 return (struct got_tag_object *)got_object_cache_get(
342 &repo->tagcache, id);
345 const struct got_error *
346 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
347 struct got_raw_object *raw)
349 #ifndef GOT_NO_OBJ_CACHE
350 const struct got_error *err = NULL;
351 err = got_object_cache_add(&repo->rawcache, id, raw);
353 if (err->code == GOT_ERR_OBJ_EXISTS ||
354 err->code == GOT_ERR_OBJ_TOO_LARGE)
364 struct got_raw_object *
365 got_repo_get_cached_raw_object(struct got_repository *repo,
366 struct got_object_id *id)
368 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
372 static const struct got_error *
373 open_repo(struct got_repository *repo, const char *path)
375 const struct got_error *err = NULL;
377 repo->gitdir_fd = -1;
379 /* bare git repository? */
380 repo->path_git_dir = strdup(path);
381 if (repo->path_git_dir == NULL)
382 return got_error_from_errno("strdup");
383 if (is_git_repo(repo)) {
384 repo->path = strdup(repo->path_git_dir);
385 if (repo->path == NULL) {
386 err = got_error_from_errno("strdup");
389 repo->gitdir_fd = open(repo->path_git_dir,
390 O_DIRECTORY | O_CLOEXEC);
391 if (repo->gitdir_fd == -1) {
392 err = got_error_from_errno2("open",
399 /* git repository with working tree? */
400 free(repo->path_git_dir);
401 repo->path_git_dir = NULL;
402 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
403 err = got_error_from_errno("asprintf");
406 if (is_git_repo(repo)) {
407 repo->path = strdup(path);
408 if (repo->path == NULL) {
409 err = got_error_from_errno("strdup");
412 repo->gitdir_fd = open(repo->path_git_dir,
413 O_DIRECTORY | O_CLOEXEC);
414 if (repo->gitdir_fd == -1) {
415 err = got_error_from_errno2("open",
422 err = got_error(GOT_ERR_NOT_GIT_REPO);
427 free(repo->path_git_dir);
428 repo->path_git_dir = NULL;
429 if (repo->gitdir_fd != -1)
430 close(repo->gitdir_fd);
431 repo->gitdir_fd = -1;
437 static const struct got_error *
438 parse_gitconfig_file(int *gitconfig_repository_format_version,
439 char **gitconfig_author_name, char **gitconfig_author_email,
440 struct got_remote_repo **remotes, int *nremotes,
441 char **gitconfig_owner, char ***extensions, int *nextensions,
442 const char *gitconfig_path)
444 const struct got_error *err = NULL, *child_err = NULL;
446 int imsg_fds[2] = { -1, -1 };
448 struct imsgbuf *ibuf;
450 *gitconfig_repository_format_version = 0;
455 *gitconfig_author_name = NULL;
456 *gitconfig_author_email = NULL;
462 *gitconfig_owner = NULL;
464 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
468 return got_error_from_errno2("open", gitconfig_path);
471 ibuf = calloc(1, sizeof(*ibuf));
473 err = got_error_from_errno("calloc");
477 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
478 err = got_error_from_errno("socketpair");
484 err = got_error_from_errno("fork");
486 } else if (pid == 0) {
487 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
492 if (close(imsg_fds[1]) == -1) {
493 err = got_error_from_errno("close");
497 imsg_init(ibuf, imsg_fds[0]);
499 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
504 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
508 err = got_privsep_recv_gitconfig_int(
509 gitconfig_repository_format_version, ibuf);
513 if (extensions && nextensions) {
514 err = got_privsep_send_gitconfig_repository_extensions_req(
518 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
521 if (*nextensions > 0) {
523 *extensions = calloc(*nextensions, sizeof(char *));
524 if (*extensions == NULL) {
525 err = got_error_from_errno("calloc");
528 for (i = 0; i < *nextensions; i++) {
530 err = got_privsep_recv_gitconfig_str(&ext,
534 (*extensions)[i] = ext;
539 err = got_privsep_send_gitconfig_author_name_req(ibuf);
543 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
547 err = got_privsep_send_gitconfig_author_email_req(ibuf);
551 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
555 if (remotes && nremotes) {
556 err = got_privsep_send_gitconfig_remotes_req(ibuf);
560 err = got_privsep_recv_gitconfig_remotes(remotes,
566 if (gitconfig_owner) {
567 err = got_privsep_send_gitconfig_owner_req(ibuf);
570 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
575 err = got_privsep_send_stop(imsg_fds[0]);
576 child_err = got_privsep_wait_for_child(pid);
577 if (child_err && err == NULL)
580 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
581 err = got_error_from_errno("close");
582 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
583 err = got_error_from_errno("close");
584 if (fd != -1 && close(fd) == -1 && err == NULL)
585 err = got_error_from_errno2("close", gitconfig_path);
590 static const struct got_error *
591 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
593 const struct got_error *err = NULL;
594 char *repo_gitconfig_path = NULL;
596 if (global_gitconfig_path) {
597 /* Read settings from ~/.gitconfig. */
598 int dummy_repo_version;
599 err = parse_gitconfig_file(&dummy_repo_version,
600 &repo->global_gitconfig_author_name,
601 &repo->global_gitconfig_author_email,
602 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
607 /* Read repository's .git/config file. */
608 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
609 if (repo_gitconfig_path == NULL)
610 return got_error_from_errno("got_repo_get_path_gitconfig");
612 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
613 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
614 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
615 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
616 repo_gitconfig_path);
620 free(repo_gitconfig_path);
624 static const struct got_error *
625 read_gotconfig(struct got_repository *repo)
627 const struct got_error *err = NULL;
628 char *gotconfig_path;
630 gotconfig_path = got_repo_get_path_gotconfig(repo);
631 if (gotconfig_path == NULL)
632 return got_error_from_errno("got_repo_get_path_gotconfig");
634 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
635 free(gotconfig_path);
639 /* Supported repository format extensions. */
640 static const char *const repo_extensions[] = {
641 "noop", /* Got supports repository format version 1. */
642 "preciousObjects", /* Supported by gotadmin cleanup. */
643 "worktreeConfig", /* Got does not care about Git work trees. */
646 const struct got_error *
647 got_repo_open(struct got_repository **repop, const char *path,
648 const char *global_gitconfig_path)
650 struct got_repository *repo = NULL;
651 const struct got_error *err = NULL;
652 char *repo_path = NULL;
658 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
659 return got_error_from_errno("getrlimit");
661 repo = calloc(1, sizeof(*repo));
663 return got_error_from_errno("calloc");
665 RB_INIT(&repo->packidx_bloom_filters);
666 TAILQ_INIT(&repo->packidx_paths);
668 for (i = 0; i < nitems(repo->privsep_children); i++) {
669 memset(&repo->privsep_children[i], 0,
670 sizeof(repo->privsep_children[0]));
671 repo->privsep_children[i].imsg_fd = -1;
674 err = got_object_cache_init(&repo->objcache,
675 GOT_OBJECT_CACHE_TYPE_OBJ);
678 err = got_object_cache_init(&repo->treecache,
679 GOT_OBJECT_CACHE_TYPE_TREE);
682 err = got_object_cache_init(&repo->commitcache,
683 GOT_OBJECT_CACHE_TYPE_COMMIT);
686 err = got_object_cache_init(&repo->tagcache,
687 GOT_OBJECT_CACHE_TYPE_TAG);
690 err = got_object_cache_init(&repo->rawcache,
691 GOT_OBJECT_CACHE_TYPE_RAW);
695 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
696 if (repo->pack_cache_size > rl.rlim_cur / 8)
697 repo->pack_cache_size = rl.rlim_cur / 8;
698 for (i = 0; i < nitems(repo->packs); i++) {
699 if (i < repo->pack_cache_size) {
700 repo->packs[i].basefd = got_opentempfd();
701 if (repo->packs[i].basefd == -1)
702 return got_error_from_errno("got_opentempfd");
703 repo->packs[i].accumfd = got_opentempfd();
704 if (repo->packs[i].accumfd == -1)
705 return got_error_from_errno("got_opentempfd");
707 repo->packs[i].basefd = -1;
708 repo->packs[i].accumfd = -1;
712 repo_path = realpath(path, NULL);
713 if (repo_path == NULL) {
714 err = got_error_from_errno2("realpath", path);
721 err = open_repo(repo, repo_path);
724 if (err->code != GOT_ERR_NOT_GIT_REPO)
726 if (repo_path[0] == '/' && repo_path[1] == '\0') {
727 err = got_error(GOT_ERR_NOT_GIT_REPO);
730 err = got_path_dirname(&parent_path, repo_path);
734 repo_path = parent_path;
737 err = read_gotconfig(repo);
741 err = read_gitconfig(repo, global_gitconfig_path);
744 if (repo->gitconfig_repository_format_version != 0)
745 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
746 for (i = 0; i < repo->nextensions; i++) {
747 char *ext = repo->extensions[i];
748 int j, supported = 0;
749 for (j = 0; j < nitems(repo_extensions); j++) {
750 if (strcmp(ext, repo_extensions[j]) == 0) {
756 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
761 err = got_repo_list_packidx(&repo->packidx_paths, repo);
764 got_repo_close(repo);
771 const struct got_error *
772 got_repo_close(struct got_repository *repo)
774 const struct got_error *err = NULL, *child_err;
775 struct got_packidx_bloom_filter *bf;
776 struct got_pathlist_entry *pe;
779 for (i = 0; i < repo->pack_cache_size; i++) {
780 if (repo->packidx_cache[i] == NULL)
782 got_packidx_close(repo->packidx_cache[i]);
785 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
786 &repo->packidx_bloom_filters))) {
787 RB_REMOVE(got_packidx_bloom_filter_tree,
788 &repo->packidx_bloom_filters, bf);
793 for (i = 0; i < repo->pack_cache_size; i++) {
794 if (repo->packs[i].path_packfile)
795 got_pack_close(&repo->packs[i]);
796 if (repo->packs[i].basefd != -1) {
797 if (close(repo->packs[i].basefd) == -1 && err == NULL)
798 err = got_error_from_errno("close");
799 repo->packs[i].basefd = -1;
801 if (repo->packs[i].accumfd != -1) {
802 if (close(repo->packs[i].accumfd) == -1 && err == NULL)
803 err = got_error_from_errno("close");
804 repo->packs[i].accumfd = -1;
809 free(repo->path_git_dir);
811 got_object_cache_close(&repo->objcache);
812 got_object_cache_close(&repo->treecache);
813 got_object_cache_close(&repo->commitcache);
814 got_object_cache_close(&repo->tagcache);
815 got_object_cache_close(&repo->rawcache);
817 for (i = 0; i < nitems(repo->privsep_children); i++) {
818 if (repo->privsep_children[i].imsg_fd == -1)
820 imsg_clear(repo->privsep_children[i].ibuf);
821 free(repo->privsep_children[i].ibuf);
822 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
823 child_err = got_privsep_wait_for_child(
824 repo->privsep_children[i].pid);
825 if (child_err && err == NULL)
827 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
829 err = got_error_from_errno("close");
832 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
834 err = got_error_from_errno("close");
837 got_gotconfig_free(repo->gotconfig);
838 free(repo->gitconfig_author_name);
839 free(repo->gitconfig_author_email);
840 for (i = 0; i < repo->ngitconfig_remotes; i++)
841 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
842 free(repo->gitconfig_remotes);
843 for (i = 0; i < repo->nextensions; i++)
844 free(repo->extensions[i]);
845 free(repo->extensions);
847 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
848 free((void *)pe->path);
849 got_pathlist_free(&repo->packidx_paths);
856 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
862 free(repo->fetch_url);
863 repo->fetch_url = NULL;
864 free(repo->send_url);
865 repo->send_url = NULL;
866 for (i = 0; i < repo->nfetch_branches; i++)
867 free(repo->fetch_branches[i]);
868 free(repo->fetch_branches);
869 repo->fetch_branches = NULL;
870 repo->nfetch_branches = 0;
871 for (i = 0; i < repo->nsend_branches; i++)
872 free(repo->send_branches[i]);
873 free(repo->send_branches);
874 repo->send_branches = NULL;
875 repo->nsend_branches = 0;
878 const struct got_error *
879 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
880 const char *input_path)
882 const struct got_error *err = NULL;
883 const char *repo_abspath = NULL;
885 char *canonpath, *path = NULL;
887 *in_repo_path = NULL;
889 canonpath = strdup(input_path);
890 if (canonpath == NULL) {
891 err = got_error_from_errno("strdup");
894 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
898 repo_abspath = got_repo_get_path(repo);
900 if (canonpath[0] == '\0') {
901 path = strdup(canonpath);
903 err = got_error_from_errno("strdup");
907 path = realpath(canonpath, NULL);
909 if (errno != ENOENT) {
910 err = got_error_from_errno2("realpath",
915 * Path is not on disk.
916 * Assume it is already relative to repository root.
918 path = strdup(canonpath);
920 err = got_error_from_errno("strdup");
925 repolen = strlen(repo_abspath);
929 if (strcmp(path, repo_abspath) == 0) {
933 err = got_error_from_errno("strdup");
936 } else if (len > repolen &&
937 got_path_is_child(path, repo_abspath, repolen)) {
938 /* Matched an on-disk path inside repository. */
939 if (got_repo_is_bare(repo)) {
941 * Matched an on-disk path inside repository
942 * database. Treat input as repository-relative.
949 /* Strip common prefix with repository path. */
950 err = got_path_skip_common_ancestor(&child,
959 * Matched unrelated on-disk path.
960 * Treat input as repository-relative.
968 /* Make in-repository path absolute */
969 if (path[0] != '/') {
971 if (asprintf(&abspath, "/%s", path) == -1) {
972 err = got_error_from_errno("asprintf");
984 *in_repo_path = path;
988 static const struct got_error *
989 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
990 const char *path_packidx)
992 const struct got_error *err = NULL;
995 for (i = 0; i < repo->pack_cache_size; i++) {
996 if (repo->packidx_cache[i] == NULL)
998 if (strcmp(repo->packidx_cache[i]->path_packidx,
999 path_packidx) == 0) {
1000 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1003 if (i == repo->pack_cache_size) {
1004 i = repo->pack_cache_size - 1;
1005 err = got_packidx_close(repo->packidx_cache[i]);
1010 repo->packidx_cache[i] = packidx;
1016 got_repo_is_packidx_filename(const char *name, size_t len)
1018 if (len != GOT_PACKIDX_NAMELEN)
1021 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1024 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1025 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1031 static struct got_packidx_bloom_filter *
1032 get_packidx_bloom_filter(struct got_repository *repo,
1033 const char *path, size_t path_len)
1035 struct got_packidx_bloom_filter key;
1037 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1038 return NULL; /* XXX */
1039 key.path_len = path_len;
1041 return RB_FIND(got_packidx_bloom_filter_tree,
1042 &repo->packidx_bloom_filters, &key);
1046 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1047 const char *path_packidx, struct got_object_id *id)
1049 struct got_packidx_bloom_filter *bf;
1051 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1053 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1055 /* No bloom filter means this pack index must be searched. */
1059 static const struct got_error *
1060 add_packidx_bloom_filter(struct got_repository *repo,
1061 struct got_packidx *packidx, const char *path_packidx)
1063 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1064 struct got_packidx_bloom_filter *bf;
1068 * Don't use bloom filters for very large pack index files.
1069 * Large pack files will contain a relatively large fraction
1070 * of our objects so we will likely need to visit them anyway.
1071 * The more objects a pack file contains the higher the probability
1072 * of a false-positive match from the bloom filter. And reading
1073 * all object IDs from a large pack index file can be expensive.
1075 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1078 /* Do we already have a filter for this pack index? */
1079 if (get_packidx_bloom_filter(repo, path_packidx,
1080 strlen(path_packidx)) != NULL)
1083 bf = calloc(1, sizeof(*bf));
1085 return got_error_from_errno("calloc");
1086 bf->bloom = calloc(1, sizeof(*bf->bloom));
1087 if (bf->bloom == NULL) {
1089 return got_error_from_errno("calloc");
1092 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1093 if (len >= sizeof(bf->path)) {
1096 return got_error(GOT_ERR_NO_SPACE);
1100 /* Minimum size supported by our bloom filter is 1000 entries. */
1101 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1102 for (i = 0; i < nobjects; i++) {
1103 struct got_packidx_object_id *id;
1104 id = &packidx->hdr.sorted_ids[i];
1105 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1108 RB_INSERT(got_packidx_bloom_filter_tree,
1109 &repo->packidx_bloom_filters, bf);
1113 const struct got_error *
1114 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1115 struct got_repository *repo, struct got_object_id *id)
1117 const struct got_error *err;
1118 struct got_pathlist_entry *pe;
1121 /* Search pack index cache. */
1122 for (i = 0; i < repo->pack_cache_size; i++) {
1123 if (repo->packidx_cache[i] == NULL)
1125 if (!got_repo_check_packidx_bloom_filter(repo,
1126 repo->packidx_cache[i]->path_packidx, id))
1127 continue; /* object will not be found in this index */
1128 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1130 *packidx = repo->packidx_cache[i];
1132 * Move this cache entry to the front. Repeatedly
1133 * searching a wrong pack index can be expensive.
1136 memmove(&repo->packidx_cache[1],
1137 &repo->packidx_cache[0],
1138 i * sizeof(repo->packidx_cache[0]));
1139 repo->packidx_cache[0] = *packidx;
1144 /* No luck. Search the filesystem. */
1146 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1147 const char *path_packidx = pe->path;
1150 if (!got_repo_check_packidx_bloom_filter(repo,
1152 continue; /* object will not be found in this index */
1154 for (i = 0; i < repo->pack_cache_size; i++) {
1155 if (repo->packidx_cache[i] == NULL)
1157 if (strcmp(repo->packidx_cache[i]->path_packidx,
1158 path_packidx) == 0) {
1164 continue; /* already searched */
1166 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1171 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1175 err = cache_packidx(repo, *packidx, path_packidx);
1179 *idx = got_packidx_get_object_idx(*packidx, id);
1181 err = NULL; /* found the object */
1186 err = got_error_no_obj(id);
1191 const struct got_error *
1192 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1193 struct got_repository *repo)
1195 const struct got_error *err = NULL;
1196 DIR *packdir = NULL;
1197 struct dirent *dent;
1198 char *path_packidx = NULL;
1201 packdir_fd = openat(got_repo_get_fd(repo),
1202 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1203 if (packdir_fd == -1) {
1204 return got_error_from_errno_fmt("openat: %s/%s",
1205 got_repo_get_path_git_dir(repo),
1206 GOT_OBJECTS_PACK_DIR);
1209 packdir = fdopendir(packdir_fd);
1210 if (packdir == NULL) {
1211 err = got_error_from_errno("fdopendir");
1215 while ((dent = readdir(packdir)) != NULL) {
1216 if (!got_repo_is_packidx_filename(dent->d_name,
1217 strlen(dent->d_name)))
1220 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1221 dent->d_name) == -1) {
1222 err = got_error_from_errno("asprintf");
1223 path_packidx = NULL;
1227 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1234 if (packdir && closedir(packdir) != 0 && err == NULL)
1235 err = got_error_from_errno("closedir");
1239 const struct got_error *
1240 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1241 struct got_repository *repo)
1243 const struct got_error *err;
1248 /* Search pack index cache. */
1249 for (i = 0; i < repo->pack_cache_size; i++) {
1250 if (repo->packidx_cache[i] == NULL)
1252 if (strcmp(repo->packidx_cache[i]->path_packidx,
1253 path_packidx) == 0) {
1254 *packidx = repo->packidx_cache[i];
1258 /* No luck. Search the filesystem. */
1260 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1265 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1269 err = cache_packidx(repo, *packidx, path_packidx);
1272 got_packidx_close(*packidx);
1278 static const struct got_error *
1279 read_packfile_hdr(int fd, struct got_packidx *packidx)
1281 const struct got_error *err = NULL;
1282 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1283 struct got_packfile_hdr hdr;
1286 n = read(fd, &hdr, sizeof(hdr));
1288 return got_error_from_errno("read");
1289 if (n != sizeof(hdr))
1290 return got_error(GOT_ERR_BAD_PACKFILE);
1292 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1293 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1294 be32toh(hdr.nobjects) != totobj)
1295 err = got_error(GOT_ERR_BAD_PACKFILE);
1300 static const struct got_error *
1301 open_packfile(int *fd, struct got_repository *repo,
1302 const char *relpath, struct got_packidx *packidx)
1304 const struct got_error *err = NULL;
1306 *fd = openat(got_repo_get_fd(repo), relpath,
1307 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1309 return got_error_from_errno_fmt("openat: %s/%s",
1310 got_repo_get_path_git_dir(repo), relpath);
1313 err = read_packfile_hdr(*fd, packidx);
1323 const struct got_error *
1324 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1325 const char *path_packfile, struct got_packidx *packidx)
1327 const struct got_error *err = NULL;
1328 struct got_pack *pack = NULL;
1335 for (i = 0; i < repo->pack_cache_size; i++) {
1336 pack = &repo->packs[i];
1337 if (pack->path_packfile == NULL)
1339 if (strcmp(pack->path_packfile, path_packfile) == 0)
1340 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1343 if (i == repo->pack_cache_size) {
1344 struct got_pack tmp;
1345 err = got_pack_close(&repo->packs[i - 1]);
1348 if (ftruncate(repo->packs[i - 1].basefd, 0L) == -1)
1349 return got_error_from_errno("ftruncate");
1350 if (ftruncate(repo->packs[i - 1].accumfd, 0L) == -1)
1351 return got_error_from_errno("ftruncate");
1352 memcpy(&tmp, &repo->packs[i - 1], sizeof(tmp));
1353 memcpy(&repo->packs[i - 1], &repo->packs[0],
1354 sizeof(repo->packs[i - 1]));
1355 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1359 pack = &repo->packs[i];
1361 pack->path_packfile = strdup(path_packfile);
1362 if (pack->path_packfile == NULL) {
1363 err = got_error_from_errno("strdup");
1367 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1371 if (fstat(pack->fd, &sb) != 0) {
1372 err = got_error_from_errno("fstat");
1375 pack->filesize = sb.st_size;
1377 pack->privsep_child = NULL;
1379 #ifndef GOT_PACK_NO_MMAP
1380 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1382 if (pack->map == MAP_FAILED) {
1383 if (errno != ENOMEM) {
1384 err = got_error_from_errno("mmap");
1387 pack->map = NULL; /* fall back to read(2) */
1393 free(pack->path_packfile);
1394 memset(pack, 0, sizeof(*pack));
1402 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1404 struct got_pack *pack = NULL;
1407 for (i = 0; i < repo->pack_cache_size; i++) {
1408 pack = &repo->packs[i];
1409 if (pack->path_packfile == NULL)
1411 if (strcmp(pack->path_packfile, path_packfile) == 0)
1418 const struct got_error *
1419 got_repo_init(const char *repo_path)
1421 const struct got_error *err = NULL;
1422 const char *dirnames[] = {
1424 GOT_OBJECTS_PACK_DIR,
1427 const char *description_str = "Unnamed repository; "
1428 "edit this file 'description' to name the repository.";
1429 const char *headref_str = "ref: refs/heads/main";
1430 const char *gitconfig_str = "[core]\n"
1431 "\trepositoryformatversion = 0\n"
1432 "\tfilemode = true\n"
1437 if (!got_path_dir_is_empty(repo_path))
1438 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1440 for (i = 0; i < nitems(dirnames); i++) {
1441 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1442 return got_error_from_errno("asprintf");
1444 err = got_path_mkdir(path);
1450 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1451 return got_error_from_errno("asprintf");
1452 err = got_path_create_file(path, description_str);
1457 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1458 return got_error_from_errno("asprintf");
1459 err = got_path_create_file(path, headref_str);
1464 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1465 return got_error_from_errno("asprintf");
1466 err = got_path_create_file(path, gitconfig_str);
1474 static const struct got_error *
1475 match_packed_object(struct got_object_id **unique_id,
1476 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1478 const struct got_error *err = NULL;
1479 struct got_object_id_queue matched_ids;
1480 struct got_pathlist_entry *pe;
1482 STAILQ_INIT(&matched_ids);
1484 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1485 const char *path_packidx = pe->path;
1486 struct got_packidx *packidx;
1487 struct got_object_qid *qid;
1489 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1494 err = got_packidx_match_id_str_prefix(&matched_ids,
1495 packidx, id_str_prefix);
1497 got_packidx_close(packidx);
1500 err = got_packidx_close(packidx);
1504 STAILQ_FOREACH(qid, &matched_ids, entry) {
1505 if (obj_type != GOT_OBJ_TYPE_ANY) {
1507 err = got_object_get_type(&matched_type, repo,
1511 if (matched_type != obj_type)
1514 if (*unique_id == NULL) {
1515 *unique_id = got_object_id_dup(&qid->id);
1516 if (*unique_id == NULL) {
1517 err = got_error_from_errno("malloc");
1521 if (got_object_id_cmp(*unique_id,
1523 continue; /* packed multiple times */
1524 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1530 got_object_id_queue_free(&matched_ids);
1538 static const struct got_error *
1539 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1540 const char *object_dir, const char *id_str_prefix, int obj_type,
1541 struct got_repository *repo)
1543 const struct got_error *err = NULL;
1546 struct dirent *dent;
1547 struct got_object_id id;
1549 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1550 err = got_error_from_errno("asprintf");
1554 dir = opendir(path);
1556 if (errno == ENOENT) {
1560 err = got_error_from_errno2("opendir", path);
1563 while ((dent = readdir(dir)) != NULL) {
1567 if (strcmp(dent->d_name, ".") == 0 ||
1568 strcmp(dent->d_name, "..") == 0)
1571 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1572 err = got_error_from_errno("asprintf");
1576 if (!got_parse_sha1_digest(id.sha1, id_str))
1580 * Directory entries do not necessarily appear in
1581 * sorted order, so we must iterate over all of them.
1583 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1589 if (*unique_id == NULL) {
1590 if (obj_type != GOT_OBJ_TYPE_ANY) {
1592 err = got_object_get_type(&matched_type, repo,
1596 if (matched_type != obj_type)
1599 *unique_id = got_object_id_dup(&id);
1600 if (*unique_id == NULL) {
1601 err = got_error_from_errno("got_object_id_dup");
1606 if (got_object_id_cmp(*unique_id, &id) == 0)
1607 continue; /* both packed and loose */
1608 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1614 if (dir && closedir(dir) != 0 && err == NULL)
1615 err = got_error_from_errno("closedir");
1624 const struct got_error *
1625 got_repo_match_object_id_prefix(struct got_object_id **id,
1626 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 char *path_objects = got_repo_get_path_objects(repo);
1630 char *object_dir = NULL;
1636 len = strlen(id_str_prefix);
1637 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1638 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1640 for (i = 0; i < len; i++) {
1641 if (isxdigit((unsigned char)id_str_prefix[i]))
1643 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1647 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1650 object_dir = strndup(id_str_prefix, 2);
1651 if (object_dir == NULL) {
1652 err = got_error_from_errno("strdup");
1655 err = match_loose_object(id, path_objects, object_dir,
1656 id_str_prefix, obj_type, repo);
1657 } else if (len == 1) {
1659 for (i = 0; i < 0xf; i++) {
1660 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1662 err = got_error_from_errno("asprintf");
1665 err = match_packed_object(id, repo, object_dir,
1669 err = match_loose_object(id, path_objects, object_dir,
1670 id_str_prefix, obj_type, repo);
1675 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1683 } else if (*id == NULL) {
1685 case GOT_OBJ_TYPE_BLOB:
1686 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1687 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1689 case GOT_OBJ_TYPE_TREE:
1690 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1691 GOT_OBJ_LABEL_TREE, id_str_prefix);
1693 case GOT_OBJ_TYPE_COMMIT:
1694 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1695 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1697 case GOT_OBJ_TYPE_TAG:
1698 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1699 GOT_OBJ_LABEL_TAG, id_str_prefix);
1702 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1710 const struct got_error *
1711 got_repo_match_object_id(struct got_object_id **id, char **label,
1712 const char *id_str, int obj_type, struct got_reflist_head *refs,
1713 struct got_repository *repo)
1715 const struct got_error *err;
1716 struct got_tag_object *tag;
1717 struct got_reference *ref = NULL;
1724 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1727 *id = got_object_id_dup(
1728 got_object_tag_get_object_id(tag));
1730 err = got_error_from_errno("got_object_id_dup");
1731 else if (label && asprintf(label, "refs/tags/%s",
1732 got_object_tag_get_name(tag)) == -1) {
1733 err = got_error_from_errno("asprintf");
1737 got_object_tag_close(tag);
1739 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1740 err->code != GOT_ERR_NO_OBJ)
1744 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1746 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1748 err = got_ref_open(&ref, repo, id_str, 0);
1752 *label = strdup(got_ref_get_name(ref));
1753 if (*label == NULL) {
1754 err = got_error_from_errno("strdup");
1758 err = got_ref_resolve(id, repo, ref);
1760 err = got_object_id_str(label, *id);
1761 if (*label == NULL) {
1762 err = got_error_from_errno("strdup");
1772 const struct got_error *
1773 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1774 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1776 const struct got_error *err = NULL;
1777 struct got_reflist_entry *re;
1778 struct got_object_id *tag_id;
1779 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1783 TAILQ_FOREACH(re, refs, entry) {
1784 const char *refname;
1785 refname = got_ref_get_name(re->ref);
1786 if (got_ref_is_symbolic(re->ref))
1788 if (strncmp(refname, "refs/tags/", 10) != 0)
1790 if (!name_is_absolute)
1791 refname += strlen("refs/tags/");
1792 if (strcmp(refname, name) != 0)
1794 err = got_ref_resolve(&tag_id, repo, re->ref);
1797 err = got_object_open_as_tag(tag, repo, tag_id);
1801 if (obj_type == GOT_OBJ_TYPE_ANY ||
1802 got_object_tag_get_object_type(*tag) == obj_type)
1804 got_object_tag_close(*tag);
1808 if (err == NULL && *tag == NULL)
1809 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1810 GOT_OBJ_LABEL_TAG, name);
1814 static const struct got_error *
1815 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1816 const char *name, mode_t mode, struct got_object_id *blob_id)
1818 const struct got_error *err = NULL;
1822 *new_te = calloc(1, sizeof(**new_te));
1823 if (*new_te == NULL)
1824 return got_error_from_errno("calloc");
1826 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1827 sizeof((*new_te)->name)) {
1828 err = got_error(GOT_ERR_NO_SPACE);
1832 if (S_ISLNK(mode)) {
1833 (*new_te)->mode = S_IFLNK;
1835 (*new_te)->mode = S_IFREG;
1836 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1838 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1840 if (err && *new_te) {
1847 static const struct got_error *
1848 import_file(struct got_tree_entry **new_te, struct dirent *de,
1849 const char *path, struct got_repository *repo)
1851 const struct got_error *err;
1852 struct got_object_id *blob_id = NULL;
1856 if (asprintf(&filepath, "%s%s%s", path,
1857 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1858 return got_error_from_errno("asprintf");
1860 if (lstat(filepath, &sb) != 0) {
1861 err = got_error_from_errno2("lstat", path);
1865 err = got_object_blob_create(&blob_id, filepath, repo);
1869 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1878 static const struct got_error *
1879 insert_tree_entry(struct got_tree_entry *new_te,
1880 struct got_pathlist_head *paths)
1882 const struct got_error *err = NULL;
1883 struct got_pathlist_entry *new_pe;
1885 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1889 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1893 static const struct got_error *write_tree(struct got_object_id **,
1894 const char *, struct got_pathlist_head *, struct got_repository *,
1895 got_repo_import_cb progress_cb, void *progress_arg);
1897 static const struct got_error *
1898 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1899 const char *path, struct got_pathlist_head *ignores,
1900 struct got_repository *repo,
1901 got_repo_import_cb progress_cb, void *progress_arg)
1903 const struct got_error *err;
1904 struct got_object_id *id = NULL;
1907 if (asprintf(&subdirpath, "%s%s%s", path,
1908 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1909 return got_error_from_errno("asprintf");
1911 (*new_te) = calloc(1, sizeof(**new_te));
1912 if (*new_te == NULL)
1913 return got_error_from_errno("calloc");
1914 (*new_te)->mode = S_IFDIR;
1915 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1916 sizeof((*new_te)->name)) {
1917 err = got_error(GOT_ERR_NO_SPACE);
1920 err = write_tree(&id, subdirpath, ignores, repo,
1921 progress_cb, progress_arg);
1924 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1936 static const struct got_error *
1937 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1938 struct got_pathlist_head *ignores, struct got_repository *repo,
1939 got_repo_import_cb progress_cb, void *progress_arg)
1941 const struct got_error *err = NULL;
1945 struct got_tree_entry *new_te = NULL;
1946 struct got_pathlist_head paths;
1947 struct got_pathlist_entry *pe;
1949 *new_tree_id = NULL;
1953 dir = opendir(path_dir);
1955 err = got_error_from_errno2("opendir", path_dir);
1960 while ((de = readdir(dir)) != NULL) {
1964 if (strcmp(de->d_name, ".") == 0 ||
1965 strcmp(de->d_name, "..") == 0)
1968 TAILQ_FOREACH(pe, ignores, entry) {
1969 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1977 err = got_path_dirent_type(&type, path_dir, de);
1981 if (type == DT_DIR) {
1982 err = import_subdir(&new_te, de, path_dir,
1983 ignores, repo, progress_cb, progress_arg);
1985 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1990 } else if (type == DT_REG || type == DT_LNK) {
1991 err = import_file(&new_te, de, path_dir, repo);
1997 err = insert_tree_entry(new_te, &paths);
2003 if (TAILQ_EMPTY(&paths)) {
2004 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2005 "cannot create tree without any entries");
2009 TAILQ_FOREACH(pe, &paths, entry) {
2010 struct got_tree_entry *te = pe->data;
2012 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2014 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2015 err = got_error_from_errno("asprintf");
2018 err = (*progress_cb)(progress_arg, path);
2024 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2028 got_pathlist_free(&paths);
2032 const struct got_error *
2033 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2034 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2035 struct got_repository *repo, got_repo_import_cb progress_cb,
2038 const struct got_error *err;
2039 struct got_object_id *new_tree_id;
2041 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2042 progress_cb, progress_arg);
2046 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2047 author, time(NULL), author, time(NULL), logmsg, repo);
2052 const struct got_error *
2053 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2054 struct got_repository *repo)
2056 const struct got_error *err = NULL;
2057 char *path_objects = NULL, *path = NULL;
2059 struct got_object_id id;
2065 path_objects = got_repo_get_path_objects(repo);
2066 if (path_objects == NULL)
2067 return got_error_from_errno("got_repo_get_path_objects");
2069 for (i = 0; i <= 0xff; i++) {
2070 struct dirent *dent;
2072 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2073 err = got_error_from_errno("asprintf");
2077 dir = opendir(path);
2079 if (errno == ENOENT) {
2083 err = got_error_from_errno2("opendir", path);
2087 while ((dent = readdir(dir)) != NULL) {
2092 if (strcmp(dent->d_name, ".") == 0 ||
2093 strcmp(dent->d_name, "..") == 0)
2096 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2097 err = got_error_from_errno("asprintf");
2101 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2107 err = got_object_open_loose_fd(&fd, &id, repo);
2111 if (fstat(fd, &sb) == -1) {
2112 err = got_error_from_errno("fstat");
2117 (*ondisk_size) += sb.st_size;
2119 if (close(fd) == -1) {
2120 err = got_error_from_errno("close");
2125 if (closedir(dir) != 0) {
2126 err = got_error_from_errno("closedir");
2135 if (dir && closedir(dir) != 0 && err == NULL)
2136 err = got_error_from_errno("closedir");
2147 const struct got_error *
2148 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2149 off_t *total_packsize, struct got_repository *repo)
2151 const struct got_error *err = NULL;
2152 DIR *packdir = NULL;
2153 struct dirent *dent;
2154 struct got_packidx *packidx = NULL;
2156 char *path_packfile;
2162 *total_packsize = 0;
2164 packdir_fd = openat(got_repo_get_fd(repo),
2165 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2166 if (packdir_fd == -1) {
2167 return got_error_from_errno_fmt("openat: %s/%s",
2168 got_repo_get_path_git_dir(repo),
2169 GOT_OBJECTS_PACK_DIR);
2172 packdir = fdopendir(packdir_fd);
2173 if (packdir == NULL) {
2174 err = got_error_from_errno("fdopendir");
2178 while ((dent = readdir(packdir)) != NULL) {
2179 if (!got_repo_is_packidx_filename(dent->d_name,
2180 strlen(dent->d_name)))
2183 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2184 dent->d_name) == -1) {
2185 err = got_error_from_errno("asprintf");
2189 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2195 if (fstat(packidx->fd, &sb) == -1)
2197 *total_packsize += sb.st_size;
2199 err = got_packidx_get_packfile_path(&path_packfile,
2200 packidx->path_packidx);
2204 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2206 free(path_packfile);
2209 free(path_packfile);
2210 *total_packsize += sb.st_size;
2212 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2216 got_packidx_close(packidx);
2221 got_packidx_close(packidx);
2222 if (packdir && closedir(packdir) != 0 && err == NULL)
2223 err = got_error_from_errno("closedir");
2227 *total_packsize = 0;
2232 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2233 got_packidx_bloom_filter_cmp);