2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
72 got_repo_get_path(struct got_repository *repo)
78 got_repo_get_path_git_dir(struct got_repository *repo)
80 return repo->path_git_dir;
84 got_repo_get_fd(struct got_repository *repo)
86 return repo->gitdir_fd;
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
92 return repo->gitconfig_author_name;
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
98 return repo->gitconfig_author_email;
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
134 get_path_git_child(struct got_repository *repo, const char *basename)
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
214 if (!S_ISDIR(sb.st_mode))
217 if (lstat(path_objects, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_refs, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_head, &sb) == -1)
229 if (!S_ISREG(sb.st_mode))
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
235 got_ref_close(head_ref);
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i, pack_fds_tmp[GOT_PACK_NUM_TEMPFILES];
252 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
253 if (*pack_fds == NULL)
254 return got_error_from_errno("calloc");
256 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
257 pack_fds_tmp[i] = got_opentempfd();
258 if (pack_fds_tmp[i] == -1) {
259 err = got_error_from_errno("got_opentempfd");
260 got_repo_pack_fds_close(pack_fds_tmp);
264 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
268 const struct got_error *
269 got_repo_pack_fds_close(int *pack_fds)
271 const struct got_error *err = NULL;
274 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
275 if (pack_fds[i] == -1)
277 if (close(pack_fds[i]) == -1) {
278 err = got_error_from_errno("close");
286 const struct got_error *
287 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
288 struct got_object *obj)
290 #ifndef GOT_NO_OBJ_CACHE
291 const struct got_error *err = NULL;
292 err = got_object_cache_add(&repo->objcache, id, obj);
294 if (err->code == GOT_ERR_OBJ_EXISTS ||
295 err->code == GOT_ERR_OBJ_TOO_LARGE)
305 got_repo_get_cached_object(struct got_repository *repo,
306 struct got_object_id *id)
308 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
311 const struct got_error *
312 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
313 struct got_tree_object *tree)
315 #ifndef GOT_NO_OBJ_CACHE
316 const struct got_error *err = NULL;
317 err = got_object_cache_add(&repo->treecache, id, tree);
319 if (err->code == GOT_ERR_OBJ_EXISTS ||
320 err->code == GOT_ERR_OBJ_TOO_LARGE)
329 struct got_tree_object *
330 got_repo_get_cached_tree(struct got_repository *repo,
331 struct got_object_id *id)
333 return (struct got_tree_object *)got_object_cache_get(
334 &repo->treecache, id);
337 const struct got_error *
338 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
339 struct got_commit_object *commit)
341 #ifndef GOT_NO_OBJ_CACHE
342 const struct got_error *err = NULL;
343 err = got_object_cache_add(&repo->commitcache, id, commit);
345 if (err->code == GOT_ERR_OBJ_EXISTS ||
346 err->code == GOT_ERR_OBJ_TOO_LARGE)
355 struct got_commit_object *
356 got_repo_get_cached_commit(struct got_repository *repo,
357 struct got_object_id *id)
359 return (struct got_commit_object *)got_object_cache_get(
360 &repo->commitcache, id);
363 const struct got_error *
364 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
365 struct got_tag_object *tag)
367 #ifndef GOT_NO_OBJ_CACHE
368 const struct got_error *err = NULL;
369 err = got_object_cache_add(&repo->tagcache, id, tag);
371 if (err->code == GOT_ERR_OBJ_EXISTS ||
372 err->code == GOT_ERR_OBJ_TOO_LARGE)
381 struct got_tag_object *
382 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
384 return (struct got_tag_object *)got_object_cache_get(
385 &repo->tagcache, id);
388 const struct got_error *
389 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
390 struct got_raw_object *raw)
392 #ifndef GOT_NO_OBJ_CACHE
393 const struct got_error *err = NULL;
394 err = got_object_cache_add(&repo->rawcache, id, raw);
396 if (err->code == GOT_ERR_OBJ_EXISTS ||
397 err->code == GOT_ERR_OBJ_TOO_LARGE)
407 struct got_raw_object *
408 got_repo_get_cached_raw_object(struct got_repository *repo,
409 struct got_object_id *id)
411 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
415 static const struct got_error *
416 open_repo(struct got_repository *repo, const char *path)
418 const struct got_error *err = NULL;
420 repo->gitdir_fd = -1;
422 /* bare git repository? */
423 repo->path_git_dir = strdup(path);
424 if (repo->path_git_dir == NULL)
425 return got_error_from_errno("strdup");
426 if (is_git_repo(repo)) {
427 repo->path = strdup(repo->path_git_dir);
428 if (repo->path == NULL) {
429 err = got_error_from_errno("strdup");
432 repo->gitdir_fd = open(repo->path_git_dir,
433 O_DIRECTORY | O_CLOEXEC);
434 if (repo->gitdir_fd == -1) {
435 err = got_error_from_errno2("open",
442 /* git repository with working tree? */
443 free(repo->path_git_dir);
444 repo->path_git_dir = NULL;
445 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
446 err = got_error_from_errno("asprintf");
449 if (is_git_repo(repo)) {
450 repo->path = strdup(path);
451 if (repo->path == NULL) {
452 err = got_error_from_errno("strdup");
455 repo->gitdir_fd = open(repo->path_git_dir,
456 O_DIRECTORY | O_CLOEXEC);
457 if (repo->gitdir_fd == -1) {
458 err = got_error_from_errno2("open",
465 err = got_error(GOT_ERR_NOT_GIT_REPO);
470 free(repo->path_git_dir);
471 repo->path_git_dir = NULL;
472 if (repo->gitdir_fd != -1)
473 close(repo->gitdir_fd);
474 repo->gitdir_fd = -1;
480 static const struct got_error *
481 parse_gitconfig_file(int *gitconfig_repository_format_version,
482 char **gitconfig_author_name, char **gitconfig_author_email,
483 struct got_remote_repo **remotes, int *nremotes,
484 char **gitconfig_owner, char ***extensions, int *nextensions,
485 const char *gitconfig_path)
487 const struct got_error *err = NULL, *child_err = NULL;
489 int imsg_fds[2] = { -1, -1 };
491 struct imsgbuf *ibuf;
493 *gitconfig_repository_format_version = 0;
498 *gitconfig_author_name = NULL;
499 *gitconfig_author_email = NULL;
505 *gitconfig_owner = NULL;
507 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
511 return got_error_from_errno2("open", gitconfig_path);
514 ibuf = calloc(1, sizeof(*ibuf));
516 err = got_error_from_errno("calloc");
520 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
521 err = got_error_from_errno("socketpair");
527 err = got_error_from_errno("fork");
529 } else if (pid == 0) {
530 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
535 if (close(imsg_fds[1]) == -1) {
536 err = got_error_from_errno("close");
540 imsg_init(ibuf, imsg_fds[0]);
542 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
547 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
551 err = got_privsep_recv_gitconfig_int(
552 gitconfig_repository_format_version, ibuf);
556 if (extensions && nextensions) {
557 err = got_privsep_send_gitconfig_repository_extensions_req(
561 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
564 if (*nextensions > 0) {
566 *extensions = calloc(*nextensions, sizeof(char *));
567 if (*extensions == NULL) {
568 err = got_error_from_errno("calloc");
571 for (i = 0; i < *nextensions; i++) {
573 err = got_privsep_recv_gitconfig_str(&ext,
577 (*extensions)[i] = ext;
582 err = got_privsep_send_gitconfig_author_name_req(ibuf);
586 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
590 err = got_privsep_send_gitconfig_author_email_req(ibuf);
594 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
598 if (remotes && nremotes) {
599 err = got_privsep_send_gitconfig_remotes_req(ibuf);
603 err = got_privsep_recv_gitconfig_remotes(remotes,
609 if (gitconfig_owner) {
610 err = got_privsep_send_gitconfig_owner_req(ibuf);
613 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
618 err = got_privsep_send_stop(imsg_fds[0]);
619 child_err = got_privsep_wait_for_child(pid);
620 if (child_err && err == NULL)
623 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
624 err = got_error_from_errno("close");
625 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
626 err = got_error_from_errno("close");
627 if (fd != -1 && close(fd) == -1 && err == NULL)
628 err = got_error_from_errno2("close", gitconfig_path);
633 static const struct got_error *
634 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
636 const struct got_error *err = NULL;
637 char *repo_gitconfig_path = NULL;
639 if (global_gitconfig_path) {
640 /* Read settings from ~/.gitconfig. */
641 int dummy_repo_version;
642 err = parse_gitconfig_file(&dummy_repo_version,
643 &repo->global_gitconfig_author_name,
644 &repo->global_gitconfig_author_email,
645 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
650 /* Read repository's .git/config file. */
651 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
652 if (repo_gitconfig_path == NULL)
653 return got_error_from_errno("got_repo_get_path_gitconfig");
655 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
656 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
657 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
658 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
659 repo_gitconfig_path);
663 free(repo_gitconfig_path);
667 static const struct got_error *
668 read_gotconfig(struct got_repository *repo)
670 const struct got_error *err = NULL;
671 char *gotconfig_path;
673 gotconfig_path = got_repo_get_path_gotconfig(repo);
674 if (gotconfig_path == NULL)
675 return got_error_from_errno("got_repo_get_path_gotconfig");
677 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
678 free(gotconfig_path);
682 /* Supported repository format extensions. */
683 static const char *const repo_extensions[] = {
684 "noop", /* Got supports repository format version 1. */
685 "preciousObjects", /* Supported by gotadmin cleanup. */
686 "worktreeConfig", /* Got does not care about Git work trees. */
689 const struct got_error *
690 got_repo_open(struct got_repository **repop, const char *path,
691 const char *global_gitconfig_path, int *pack_fds)
693 struct got_repository *repo = NULL;
694 const struct got_error *err = NULL;
695 char *repo_path = NULL;
701 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
702 return got_error_from_errno("getrlimit");
704 repo = calloc(1, sizeof(*repo));
706 return got_error_from_errno("calloc");
708 RB_INIT(&repo->packidx_bloom_filters);
709 TAILQ_INIT(&repo->packidx_paths);
711 for (i = 0; i < nitems(repo->privsep_children); i++) {
712 memset(&repo->privsep_children[i], 0,
713 sizeof(repo->privsep_children[0]));
714 repo->privsep_children[i].imsg_fd = -1;
717 err = got_object_cache_init(&repo->objcache,
718 GOT_OBJECT_CACHE_TYPE_OBJ);
721 err = got_object_cache_init(&repo->treecache,
722 GOT_OBJECT_CACHE_TYPE_TREE);
725 err = got_object_cache_init(&repo->commitcache,
726 GOT_OBJECT_CACHE_TYPE_COMMIT);
729 err = got_object_cache_init(&repo->tagcache,
730 GOT_OBJECT_CACHE_TYPE_TAG);
733 err = got_object_cache_init(&repo->rawcache,
734 GOT_OBJECT_CACHE_TYPE_RAW);
738 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
739 if (repo->pack_cache_size > rl.rlim_cur / 8)
740 repo->pack_cache_size = rl.rlim_cur / 8;
741 for (i = 0; i < nitems(repo->packs); i++) {
742 if (i < repo->pack_cache_size) {
743 repo->packs[i].basefd = pack_fds[j++];
744 repo->packs[i].accumfd = pack_fds[j++];
746 repo->packs[i].basefd = -1;
747 repo->packs[i].accumfd = -1;
750 repo->pinned_pack = -1;
751 repo->pinned_packidx = -1;
752 repo->pinned_pid = 0;
754 repo_path = realpath(path, NULL);
755 if (repo_path == NULL) {
756 err = got_error_from_errno2("realpath", path);
763 err = open_repo(repo, repo_path);
766 if (err->code != GOT_ERR_NOT_GIT_REPO)
768 if (repo_path[0] == '/' && repo_path[1] == '\0') {
769 err = got_error(GOT_ERR_NOT_GIT_REPO);
772 err = got_path_dirname(&parent_path, repo_path);
776 repo_path = parent_path;
779 err = read_gotconfig(repo);
783 err = read_gitconfig(repo, global_gitconfig_path);
786 if (repo->gitconfig_repository_format_version != 0)
787 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
788 for (i = 0; i < repo->nextensions; i++) {
789 char *ext = repo->extensions[i];
790 int j, supported = 0;
791 for (j = 0; j < nitems(repo_extensions); j++) {
792 if (strcmp(ext, repo_extensions[j]) == 0) {
798 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
803 err = got_repo_list_packidx(&repo->packidx_paths, repo);
806 got_repo_close(repo);
813 const struct got_error *
814 got_repo_close(struct got_repository *repo)
816 const struct got_error *err = NULL, *child_err;
817 struct got_packidx_bloom_filter *bf;
818 struct got_pathlist_entry *pe;
821 for (i = 0; i < repo->pack_cache_size; i++) {
822 if (repo->packidx_cache[i] == NULL)
824 got_packidx_close(repo->packidx_cache[i]);
827 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
828 &repo->packidx_bloom_filters))) {
829 RB_REMOVE(got_packidx_bloom_filter_tree,
830 &repo->packidx_bloom_filters, bf);
835 for (i = 0; i < repo->pack_cache_size; i++)
836 if (repo->packs[i].path_packfile)
837 if (repo->packs[i].path_packfile)
838 got_pack_close(&repo->packs[i]);
841 free(repo->path_git_dir);
843 got_object_cache_close(&repo->objcache);
844 got_object_cache_close(&repo->treecache);
845 got_object_cache_close(&repo->commitcache);
846 got_object_cache_close(&repo->tagcache);
847 got_object_cache_close(&repo->rawcache);
849 for (i = 0; i < nitems(repo->privsep_children); i++) {
850 if (repo->privsep_children[i].imsg_fd == -1)
852 imsg_clear(repo->privsep_children[i].ibuf);
853 free(repo->privsep_children[i].ibuf);
854 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
855 child_err = got_privsep_wait_for_child(
856 repo->privsep_children[i].pid);
857 if (child_err && err == NULL)
859 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
861 err = got_error_from_errno("close");
864 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
866 err = got_error_from_errno("close");
869 got_gotconfig_free(repo->gotconfig);
870 free(repo->gitconfig_author_name);
871 free(repo->gitconfig_author_email);
872 for (i = 0; i < repo->ngitconfig_remotes; i++)
873 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
874 free(repo->gitconfig_remotes);
875 for (i = 0; i < repo->nextensions; i++)
876 free(repo->extensions[i]);
877 free(repo->extensions);
879 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
880 free((void *)pe->path);
881 got_pathlist_free(&repo->packidx_paths);
888 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
894 free(repo->fetch_url);
895 repo->fetch_url = NULL;
896 free(repo->send_url);
897 repo->send_url = NULL;
898 for (i = 0; i < repo->nfetch_branches; i++)
899 free(repo->fetch_branches[i]);
900 free(repo->fetch_branches);
901 repo->fetch_branches = NULL;
902 repo->nfetch_branches = 0;
903 for (i = 0; i < repo->nsend_branches; i++)
904 free(repo->send_branches[i]);
905 free(repo->send_branches);
906 repo->send_branches = NULL;
907 repo->nsend_branches = 0;
910 const struct got_error *
911 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
912 const char *input_path)
914 const struct got_error *err = NULL;
915 const char *repo_abspath = NULL;
917 char *canonpath, *path = NULL;
919 *in_repo_path = NULL;
921 canonpath = strdup(input_path);
922 if (canonpath == NULL) {
923 err = got_error_from_errno("strdup");
926 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
930 repo_abspath = got_repo_get_path(repo);
932 if (canonpath[0] == '\0') {
933 path = strdup(canonpath);
935 err = got_error_from_errno("strdup");
939 path = realpath(canonpath, NULL);
941 if (errno != ENOENT) {
942 err = got_error_from_errno2("realpath",
947 * Path is not on disk.
948 * Assume it is already relative to repository root.
950 path = strdup(canonpath);
952 err = got_error_from_errno("strdup");
957 repolen = strlen(repo_abspath);
961 if (strcmp(path, repo_abspath) == 0) {
965 err = got_error_from_errno("strdup");
968 } else if (len > repolen &&
969 got_path_is_child(path, repo_abspath, repolen)) {
970 /* Matched an on-disk path inside repository. */
971 if (got_repo_is_bare(repo)) {
973 * Matched an on-disk path inside repository
974 * database. Treat input as repository-relative.
981 /* Strip common prefix with repository path. */
982 err = got_path_skip_common_ancestor(&child,
991 * Matched unrelated on-disk path.
992 * Treat input as repository-relative.
1000 /* Make in-repository path absolute */
1001 if (path[0] != '/') {
1003 if (asprintf(&abspath, "/%s", path) == -1) {
1004 err = got_error_from_errno("asprintf");
1016 *in_repo_path = path;
1020 static const struct got_error *
1021 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1022 const char *path_packidx)
1024 const struct got_error *err = NULL;
1027 for (i = 0; i < repo->pack_cache_size; i++) {
1028 if (repo->packidx_cache[i] == NULL)
1030 if (strcmp(repo->packidx_cache[i]->path_packidx,
1031 path_packidx) == 0) {
1032 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1035 if (i == repo->pack_cache_size) {
1038 } while (i > 0 && repo->pinned_packidx >= 0 &&
1039 i == repo->pinned_packidx);
1040 err = got_packidx_close(repo->packidx_cache[i]);
1045 repo->packidx_cache[i] = packidx;
1051 got_repo_is_packidx_filename(const char *name, size_t len)
1053 if (len != GOT_PACKIDX_NAMELEN)
1056 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1059 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1060 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1066 static struct got_packidx_bloom_filter *
1067 get_packidx_bloom_filter(struct got_repository *repo,
1068 const char *path, size_t path_len)
1070 struct got_packidx_bloom_filter key;
1072 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1073 return NULL; /* XXX */
1074 key.path_len = path_len;
1076 return RB_FIND(got_packidx_bloom_filter_tree,
1077 &repo->packidx_bloom_filters, &key);
1081 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1082 const char *path_packidx, struct got_object_id *id)
1084 struct got_packidx_bloom_filter *bf;
1086 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1088 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1090 /* No bloom filter means this pack index must be searched. */
1094 static const struct got_error *
1095 add_packidx_bloom_filter(struct got_repository *repo,
1096 struct got_packidx *packidx, const char *path_packidx)
1098 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1099 struct got_packidx_bloom_filter *bf;
1103 * Don't use bloom filters for very large pack index files.
1104 * Large pack files will contain a relatively large fraction
1105 * of our objects so we will likely need to visit them anyway.
1106 * The more objects a pack file contains the higher the probability
1107 * of a false-positive match from the bloom filter. And reading
1108 * all object IDs from a large pack index file can be expensive.
1110 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1113 /* Do we already have a filter for this pack index? */
1114 if (get_packidx_bloom_filter(repo, path_packidx,
1115 strlen(path_packidx)) != NULL)
1118 bf = calloc(1, sizeof(*bf));
1120 return got_error_from_errno("calloc");
1121 bf->bloom = calloc(1, sizeof(*bf->bloom));
1122 if (bf->bloom == NULL) {
1124 return got_error_from_errno("calloc");
1127 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1128 if (len >= sizeof(bf->path)) {
1131 return got_error(GOT_ERR_NO_SPACE);
1135 /* Minimum size supported by our bloom filter is 1000 entries. */
1136 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1137 for (i = 0; i < nobjects; i++) {
1138 struct got_packidx_object_id *id;
1139 id = &packidx->hdr.sorted_ids[i];
1140 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1143 RB_INSERT(got_packidx_bloom_filter_tree,
1144 &repo->packidx_bloom_filters, bf);
1148 const struct got_error *
1149 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1150 struct got_repository *repo, struct got_object_id *id)
1152 const struct got_error *err;
1153 struct got_pathlist_entry *pe;
1156 /* Search pack index cache. */
1157 for (i = 0; i < repo->pack_cache_size; i++) {
1158 if (repo->packidx_cache[i] == NULL)
1160 if (!got_repo_check_packidx_bloom_filter(repo,
1161 repo->packidx_cache[i]->path_packidx, id))
1162 continue; /* object will not be found in this index */
1163 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1165 *packidx = repo->packidx_cache[i];
1167 * Move this cache entry to the front. Repeatedly
1168 * searching a wrong pack index can be expensive.
1171 memmove(&repo->packidx_cache[1],
1172 &repo->packidx_cache[0],
1173 i * sizeof(repo->packidx_cache[0]));
1174 repo->packidx_cache[0] = *packidx;
1175 if (repo->pinned_packidx >= 0 &&
1176 repo->pinned_packidx < i)
1177 repo->pinned_packidx++;
1178 else if (repo->pinned_packidx == i)
1179 repo->pinned_packidx = 0;
1184 /* No luck. Search the filesystem. */
1186 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1187 const char *path_packidx = pe->path;
1190 if (!got_repo_check_packidx_bloom_filter(repo,
1192 continue; /* object will not be found in this index */
1194 for (i = 0; i < repo->pack_cache_size; i++) {
1195 if (repo->packidx_cache[i] == NULL)
1197 if (strcmp(repo->packidx_cache[i]->path_packidx,
1198 path_packidx) == 0) {
1204 continue; /* already searched */
1206 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1211 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1215 err = cache_packidx(repo, *packidx, path_packidx);
1219 *idx = got_packidx_get_object_idx(*packidx, id);
1221 err = NULL; /* found the object */
1226 err = got_error_no_obj(id);
1231 const struct got_error *
1232 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1233 struct got_repository *repo)
1235 const struct got_error *err = NULL;
1236 DIR *packdir = NULL;
1237 struct dirent *dent;
1238 char *path_packidx = NULL;
1241 packdir_fd = openat(got_repo_get_fd(repo),
1242 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1243 if (packdir_fd == -1) {
1244 return got_error_from_errno_fmt("openat: %s/%s",
1245 got_repo_get_path_git_dir(repo),
1246 GOT_OBJECTS_PACK_DIR);
1249 packdir = fdopendir(packdir_fd);
1250 if (packdir == NULL) {
1251 err = got_error_from_errno("fdopendir");
1255 while ((dent = readdir(packdir)) != NULL) {
1256 if (!got_repo_is_packidx_filename(dent->d_name,
1257 strlen(dent->d_name)))
1260 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1261 dent->d_name) == -1) {
1262 err = got_error_from_errno("asprintf");
1263 path_packidx = NULL;
1267 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1274 if (packdir && closedir(packdir) != 0 && err == NULL)
1275 err = got_error_from_errno("closedir");
1279 const struct got_error *
1280 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1281 struct got_repository *repo)
1283 const struct got_error *err;
1288 /* Search pack index cache. */
1289 for (i = 0; i < repo->pack_cache_size; i++) {
1290 if (repo->packidx_cache[i] == NULL)
1292 if (strcmp(repo->packidx_cache[i]->path_packidx,
1293 path_packidx) == 0) {
1294 *packidx = repo->packidx_cache[i];
1298 /* No luck. Search the filesystem. */
1300 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1305 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1309 err = cache_packidx(repo, *packidx, path_packidx);
1312 got_packidx_close(*packidx);
1318 static const struct got_error *
1319 read_packfile_hdr(int fd, struct got_packidx *packidx)
1321 const struct got_error *err = NULL;
1322 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1323 struct got_packfile_hdr hdr;
1326 n = read(fd, &hdr, sizeof(hdr));
1328 return got_error_from_errno("read");
1329 if (n != sizeof(hdr))
1330 return got_error(GOT_ERR_BAD_PACKFILE);
1332 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1333 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1334 be32toh(hdr.nobjects) != totobj)
1335 err = got_error(GOT_ERR_BAD_PACKFILE);
1340 static const struct got_error *
1341 open_packfile(int *fd, struct got_repository *repo,
1342 const char *relpath, struct got_packidx *packidx)
1344 const struct got_error *err = NULL;
1346 *fd = openat(got_repo_get_fd(repo), relpath,
1347 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1349 return got_error_from_errno_fmt("openat: %s/%s",
1350 got_repo_get_path_git_dir(repo), relpath);
1353 err = read_packfile_hdr(*fd, packidx);
1363 const struct got_error *
1364 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1365 const char *path_packfile, struct got_packidx *packidx)
1367 const struct got_error *err = NULL;
1368 struct got_pack *pack = NULL;
1375 for (i = 0; i < repo->pack_cache_size; i++) {
1376 pack = &repo->packs[i];
1377 if (pack->path_packfile == NULL)
1379 if (strcmp(pack->path_packfile, path_packfile) == 0)
1380 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1383 if (i == repo->pack_cache_size) {
1384 struct got_pack tmp;
1387 } while (i > 0 && repo->pinned_pack >= 0 &&
1388 i == repo->pinned_pack);
1389 err = got_pack_close(&repo->packs[i]);
1392 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1393 return got_error_from_errno("ftruncate");
1394 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1395 return got_error_from_errno("ftruncate");
1396 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1397 memcpy(&repo->packs[i], &repo->packs[0],
1398 sizeof(repo->packs[i]));
1399 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1400 if (repo->pinned_pack == 0)
1401 repo->pinned_pack = i;
1402 else if (repo->pinned_pack == i)
1403 repo->pinned_pack = 0;
1407 pack = &repo->packs[i];
1409 pack->path_packfile = strdup(path_packfile);
1410 if (pack->path_packfile == NULL) {
1411 err = got_error_from_errno("strdup");
1415 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1419 if (fstat(pack->fd, &sb) != 0) {
1420 err = got_error_from_errno("fstat");
1423 pack->filesize = sb.st_size;
1425 pack->privsep_child = NULL;
1427 #ifndef GOT_PACK_NO_MMAP
1428 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1430 if (pack->map == MAP_FAILED) {
1431 if (errno != ENOMEM) {
1432 err = got_error_from_errno("mmap");
1435 pack->map = NULL; /* fall back to read(2) */
1441 free(pack->path_packfile);
1442 memset(pack, 0, sizeof(*pack));
1450 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1452 struct got_pack *pack = NULL;
1455 for (i = 0; i < repo->pack_cache_size; i++) {
1456 pack = &repo->packs[i];
1457 if (pack->path_packfile == NULL)
1459 if (strcmp(pack->path_packfile, path_packfile) == 0)
1466 const struct got_error *
1467 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1468 struct got_pack *pack)
1471 int pinned_pack = -1, pinned_packidx = -1;
1473 for (i = 0; i < repo->pack_cache_size; i++) {
1474 if (repo->packidx_cache[i] &&
1475 strcmp(repo->packidx_cache[i]->path_packidx,
1476 packidx->path_packidx) == 0)
1478 if (repo->packs[i].path_packfile &&
1479 strcmp(repo->packs[i].path_packfile,
1480 pack->path_packfile) == 0)
1484 if (pinned_packidx == -1 || pinned_pack == -1)
1485 return got_error(GOT_ERR_PIN_PACK);
1487 repo->pinned_pack = pinned_pack;
1488 repo->pinned_packidx = pinned_packidx;
1489 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1494 got_repo_get_pinned_pack(struct got_repository *repo)
1496 if (repo->pinned_pack >= 0 &&
1497 repo->pinned_pack < repo->pack_cache_size)
1498 return &repo->packs[repo->pinned_pack];
1504 got_repo_unpin_pack(struct got_repository *repo)
1506 repo->pinned_packidx = -1;
1507 repo->pinned_pack = -1;
1508 repo->pinned_pid = 0;
1511 const struct got_error *
1512 got_repo_init(const char *repo_path)
1514 const struct got_error *err = NULL;
1515 const char *dirnames[] = {
1517 GOT_OBJECTS_PACK_DIR,
1520 const char *description_str = "Unnamed repository; "
1521 "edit this file 'description' to name the repository.";
1522 const char *headref_str = "ref: refs/heads/main";
1523 const char *gitconfig_str = "[core]\n"
1524 "\trepositoryformatversion = 0\n"
1525 "\tfilemode = true\n"
1530 if (!got_path_dir_is_empty(repo_path))
1531 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1533 for (i = 0; i < nitems(dirnames); i++) {
1534 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1535 return got_error_from_errno("asprintf");
1537 err = got_path_mkdir(path);
1543 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1544 return got_error_from_errno("asprintf");
1545 err = got_path_create_file(path, description_str);
1550 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1551 return got_error_from_errno("asprintf");
1552 err = got_path_create_file(path, headref_str);
1557 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1558 return got_error_from_errno("asprintf");
1559 err = got_path_create_file(path, gitconfig_str);
1567 static const struct got_error *
1568 match_packed_object(struct got_object_id **unique_id,
1569 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1571 const struct got_error *err = NULL;
1572 struct got_object_id_queue matched_ids;
1573 struct got_pathlist_entry *pe;
1575 STAILQ_INIT(&matched_ids);
1577 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1578 const char *path_packidx = pe->path;
1579 struct got_packidx *packidx;
1580 struct got_object_qid *qid;
1582 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1587 err = got_packidx_match_id_str_prefix(&matched_ids,
1588 packidx, id_str_prefix);
1590 got_packidx_close(packidx);
1593 err = got_packidx_close(packidx);
1597 STAILQ_FOREACH(qid, &matched_ids, entry) {
1598 if (obj_type != GOT_OBJ_TYPE_ANY) {
1600 err = got_object_get_type(&matched_type, repo,
1604 if (matched_type != obj_type)
1607 if (*unique_id == NULL) {
1608 *unique_id = got_object_id_dup(&qid->id);
1609 if (*unique_id == NULL) {
1610 err = got_error_from_errno("malloc");
1614 if (got_object_id_cmp(*unique_id,
1616 continue; /* packed multiple times */
1617 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1623 got_object_id_queue_free(&matched_ids);
1631 static const struct got_error *
1632 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1633 const char *object_dir, const char *id_str_prefix, int obj_type,
1634 struct got_repository *repo)
1636 const struct got_error *err = NULL;
1639 struct dirent *dent;
1640 struct got_object_id id;
1642 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1643 err = got_error_from_errno("asprintf");
1647 dir = opendir(path);
1649 if (errno == ENOENT) {
1653 err = got_error_from_errno2("opendir", path);
1656 while ((dent = readdir(dir)) != NULL) {
1660 if (strcmp(dent->d_name, ".") == 0 ||
1661 strcmp(dent->d_name, "..") == 0)
1664 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1665 err = got_error_from_errno("asprintf");
1669 if (!got_parse_sha1_digest(id.sha1, id_str))
1673 * Directory entries do not necessarily appear in
1674 * sorted order, so we must iterate over all of them.
1676 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1682 if (*unique_id == NULL) {
1683 if (obj_type != GOT_OBJ_TYPE_ANY) {
1685 err = got_object_get_type(&matched_type, repo,
1689 if (matched_type != obj_type)
1692 *unique_id = got_object_id_dup(&id);
1693 if (*unique_id == NULL) {
1694 err = got_error_from_errno("got_object_id_dup");
1699 if (got_object_id_cmp(*unique_id, &id) == 0)
1700 continue; /* both packed and loose */
1701 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1707 if (dir && closedir(dir) != 0 && err == NULL)
1708 err = got_error_from_errno("closedir");
1717 const struct got_error *
1718 got_repo_match_object_id_prefix(struct got_object_id **id,
1719 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1721 const struct got_error *err = NULL;
1722 char *path_objects = got_repo_get_path_objects(repo);
1723 char *object_dir = NULL;
1729 len = strlen(id_str_prefix);
1730 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1731 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1733 for (i = 0; i < len; i++) {
1734 if (isxdigit((unsigned char)id_str_prefix[i]))
1736 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1740 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1743 object_dir = strndup(id_str_prefix, 2);
1744 if (object_dir == NULL) {
1745 err = got_error_from_errno("strdup");
1748 err = match_loose_object(id, path_objects, object_dir,
1749 id_str_prefix, obj_type, repo);
1750 } else if (len == 1) {
1752 for (i = 0; i < 0xf; i++) {
1753 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1755 err = got_error_from_errno("asprintf");
1758 err = match_packed_object(id, repo, object_dir,
1762 err = match_loose_object(id, path_objects, object_dir,
1763 id_str_prefix, obj_type, repo);
1768 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1776 } else if (*id == NULL) {
1778 case GOT_OBJ_TYPE_BLOB:
1779 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1780 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1782 case GOT_OBJ_TYPE_TREE:
1783 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1784 GOT_OBJ_LABEL_TREE, id_str_prefix);
1786 case GOT_OBJ_TYPE_COMMIT:
1787 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1788 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1790 case GOT_OBJ_TYPE_TAG:
1791 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1792 GOT_OBJ_LABEL_TAG, id_str_prefix);
1795 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1803 const struct got_error *
1804 got_repo_match_object_id(struct got_object_id **id, char **label,
1805 const char *id_str, int obj_type, struct got_reflist_head *refs,
1806 struct got_repository *repo)
1808 const struct got_error *err;
1809 struct got_tag_object *tag;
1810 struct got_reference *ref = NULL;
1817 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1820 *id = got_object_id_dup(
1821 got_object_tag_get_object_id(tag));
1823 err = got_error_from_errno("got_object_id_dup");
1824 else if (label && asprintf(label, "refs/tags/%s",
1825 got_object_tag_get_name(tag)) == -1) {
1826 err = got_error_from_errno("asprintf");
1830 got_object_tag_close(tag);
1832 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1833 err->code != GOT_ERR_NO_OBJ)
1837 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1839 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1841 err = got_ref_open(&ref, repo, id_str, 0);
1845 *label = strdup(got_ref_get_name(ref));
1846 if (*label == NULL) {
1847 err = got_error_from_errno("strdup");
1851 err = got_ref_resolve(id, repo, ref);
1853 err = got_object_id_str(label, *id);
1854 if (*label == NULL) {
1855 err = got_error_from_errno("strdup");
1865 const struct got_error *
1866 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1867 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1869 const struct got_error *err = NULL;
1870 struct got_reflist_entry *re;
1871 struct got_object_id *tag_id;
1872 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1876 TAILQ_FOREACH(re, refs, entry) {
1877 const char *refname;
1878 refname = got_ref_get_name(re->ref);
1879 if (got_ref_is_symbolic(re->ref))
1881 if (strncmp(refname, "refs/tags/", 10) != 0)
1883 if (!name_is_absolute)
1884 refname += strlen("refs/tags/");
1885 if (strcmp(refname, name) != 0)
1887 err = got_ref_resolve(&tag_id, repo, re->ref);
1890 err = got_object_open_as_tag(tag, repo, tag_id);
1894 if (obj_type == GOT_OBJ_TYPE_ANY ||
1895 got_object_tag_get_object_type(*tag) == obj_type)
1897 got_object_tag_close(*tag);
1901 if (err == NULL && *tag == NULL)
1902 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1903 GOT_OBJ_LABEL_TAG, name);
1907 static const struct got_error *
1908 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1909 const char *name, mode_t mode, struct got_object_id *blob_id)
1911 const struct got_error *err = NULL;
1915 *new_te = calloc(1, sizeof(**new_te));
1916 if (*new_te == NULL)
1917 return got_error_from_errno("calloc");
1919 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1920 sizeof((*new_te)->name)) {
1921 err = got_error(GOT_ERR_NO_SPACE);
1925 if (S_ISLNK(mode)) {
1926 (*new_te)->mode = S_IFLNK;
1928 (*new_te)->mode = S_IFREG;
1929 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1931 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1933 if (err && *new_te) {
1940 static const struct got_error *
1941 import_file(struct got_tree_entry **new_te, struct dirent *de,
1942 const char *path, struct got_repository *repo)
1944 const struct got_error *err;
1945 struct got_object_id *blob_id = NULL;
1949 if (asprintf(&filepath, "%s%s%s", path,
1950 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1951 return got_error_from_errno("asprintf");
1953 if (lstat(filepath, &sb) != 0) {
1954 err = got_error_from_errno2("lstat", path);
1958 err = got_object_blob_create(&blob_id, filepath, repo);
1962 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1971 static const struct got_error *
1972 insert_tree_entry(struct got_tree_entry *new_te,
1973 struct got_pathlist_head *paths)
1975 const struct got_error *err = NULL;
1976 struct got_pathlist_entry *new_pe;
1978 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1982 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1986 static const struct got_error *write_tree(struct got_object_id **,
1987 const char *, struct got_pathlist_head *, struct got_repository *,
1988 got_repo_import_cb progress_cb, void *progress_arg);
1990 static const struct got_error *
1991 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1992 const char *path, struct got_pathlist_head *ignores,
1993 struct got_repository *repo,
1994 got_repo_import_cb progress_cb, void *progress_arg)
1996 const struct got_error *err;
1997 struct got_object_id *id = NULL;
2000 if (asprintf(&subdirpath, "%s%s%s", path,
2001 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2002 return got_error_from_errno("asprintf");
2004 (*new_te) = calloc(1, sizeof(**new_te));
2005 if (*new_te == NULL)
2006 return got_error_from_errno("calloc");
2007 (*new_te)->mode = S_IFDIR;
2008 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2009 sizeof((*new_te)->name)) {
2010 err = got_error(GOT_ERR_NO_SPACE);
2013 err = write_tree(&id, subdirpath, ignores, repo,
2014 progress_cb, progress_arg);
2017 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2029 static const struct got_error *
2030 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2031 struct got_pathlist_head *ignores, struct got_repository *repo,
2032 got_repo_import_cb progress_cb, void *progress_arg)
2034 const struct got_error *err = NULL;
2038 struct got_tree_entry *new_te = NULL;
2039 struct got_pathlist_head paths;
2040 struct got_pathlist_entry *pe;
2042 *new_tree_id = NULL;
2046 dir = opendir(path_dir);
2048 err = got_error_from_errno2("opendir", path_dir);
2053 while ((de = readdir(dir)) != NULL) {
2057 if (strcmp(de->d_name, ".") == 0 ||
2058 strcmp(de->d_name, "..") == 0)
2061 TAILQ_FOREACH(pe, ignores, entry) {
2062 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2070 err = got_path_dirent_type(&type, path_dir, de);
2074 if (type == DT_DIR) {
2075 err = import_subdir(&new_te, de, path_dir,
2076 ignores, repo, progress_cb, progress_arg);
2078 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2083 } else if (type == DT_REG || type == DT_LNK) {
2084 err = import_file(&new_te, de, path_dir, repo);
2090 err = insert_tree_entry(new_te, &paths);
2096 if (TAILQ_EMPTY(&paths)) {
2097 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2098 "cannot create tree without any entries");
2102 TAILQ_FOREACH(pe, &paths, entry) {
2103 struct got_tree_entry *te = pe->data;
2105 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2107 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2108 err = got_error_from_errno("asprintf");
2111 err = (*progress_cb)(progress_arg, path);
2117 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2121 got_pathlist_free(&paths);
2125 const struct got_error *
2126 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2127 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2128 struct got_repository *repo, got_repo_import_cb progress_cb,
2131 const struct got_error *err;
2132 struct got_object_id *new_tree_id;
2134 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2135 progress_cb, progress_arg);
2139 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2140 author, time(NULL), author, time(NULL), logmsg, repo);
2145 const struct got_error *
2146 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2147 struct got_repository *repo)
2149 const struct got_error *err = NULL;
2150 char *path_objects = NULL, *path = NULL;
2152 struct got_object_id id;
2158 path_objects = got_repo_get_path_objects(repo);
2159 if (path_objects == NULL)
2160 return got_error_from_errno("got_repo_get_path_objects");
2162 for (i = 0; i <= 0xff; i++) {
2163 struct dirent *dent;
2165 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2166 err = got_error_from_errno("asprintf");
2170 dir = opendir(path);
2172 if (errno == ENOENT) {
2176 err = got_error_from_errno2("opendir", path);
2180 while ((dent = readdir(dir)) != NULL) {
2185 if (strcmp(dent->d_name, ".") == 0 ||
2186 strcmp(dent->d_name, "..") == 0)
2189 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2190 err = got_error_from_errno("asprintf");
2194 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2200 err = got_object_open_loose_fd(&fd, &id, repo);
2204 if (fstat(fd, &sb) == -1) {
2205 err = got_error_from_errno("fstat");
2210 (*ondisk_size) += sb.st_size;
2212 if (close(fd) == -1) {
2213 err = got_error_from_errno("close");
2218 if (closedir(dir) != 0) {
2219 err = got_error_from_errno("closedir");
2228 if (dir && closedir(dir) != 0 && err == NULL)
2229 err = got_error_from_errno("closedir");
2240 const struct got_error *
2241 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2242 off_t *total_packsize, struct got_repository *repo)
2244 const struct got_error *err = NULL;
2245 DIR *packdir = NULL;
2246 struct dirent *dent;
2247 struct got_packidx *packidx = NULL;
2249 char *path_packfile;
2255 *total_packsize = 0;
2257 packdir_fd = openat(got_repo_get_fd(repo),
2258 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2259 if (packdir_fd == -1) {
2260 return got_error_from_errno_fmt("openat: %s/%s",
2261 got_repo_get_path_git_dir(repo),
2262 GOT_OBJECTS_PACK_DIR);
2265 packdir = fdopendir(packdir_fd);
2266 if (packdir == NULL) {
2267 err = got_error_from_errno("fdopendir");
2271 while ((dent = readdir(packdir)) != NULL) {
2272 if (!got_repo_is_packidx_filename(dent->d_name,
2273 strlen(dent->d_name)))
2276 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2277 dent->d_name) == -1) {
2278 err = got_error_from_errno("asprintf");
2282 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2288 if (fstat(packidx->fd, &sb) == -1)
2290 *total_packsize += sb.st_size;
2292 err = got_packidx_get_packfile_path(&path_packfile,
2293 packidx->path_packidx);
2297 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2299 free(path_packfile);
2302 free(path_packfile);
2303 *total_packsize += sb.st_size;
2305 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2309 got_packidx_close(packidx);
2314 got_packidx_close(packidx);
2315 if (packdir && closedir(packdir) != 0 && err == NULL)
2316 err = got_error_from_errno("closedir");
2320 *total_packsize = 0;
2325 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2326 got_packidx_bloom_filter_cmp);