2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <sys/resource.h>
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_gotconfig.h"
68 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 got_packidx_bloom_filter_cmp);
77 got_repo_get_path(struct got_repository *repo)
83 got_repo_get_path_git_dir(struct got_repository *repo)
85 return repo->path_git_dir;
89 got_repo_get_fd(struct got_repository *repo)
91 return repo->gitdir_fd;
95 got_repo_get_gitconfig_author_name(struct got_repository *repo)
97 return repo->gitconfig_author_name;
101 got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 return repo->gitconfig_author_email;
107 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 return repo->global_gitconfig_author_name;
113 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 return repo->global_gitconfig_author_email;
119 got_repo_get_gitconfig_owner(struct got_repository *repo)
121 return repo->gitconfig_owner;
125 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 struct got_repository *repo)
128 *extensions = repo->extensions;
129 *nextensions = repo->nextensions;
133 got_repo_is_bare(struct got_repository *repo)
135 return (strcmp(repo->path, repo->path_git_dir) == 0);
139 get_path_git_child(struct got_repository *repo, const char *basename)
143 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
151 got_repo_get_path_objects(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_OBJECTS_DIR);
157 got_repo_get_path_objects_pack(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
163 got_repo_get_path_refs(struct got_repository *repo)
165 return get_path_git_child(repo, GOT_REFS_DIR);
169 got_repo_get_path_packed_refs(struct got_repository *repo)
171 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
175 get_path_head(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_HEAD_FILE);
181 got_repo_get_path_gitconfig(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_GITCONFIG);
187 got_repo_get_path_gotconfig(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 const struct got_gotconfig *
193 got_repo_get_gotconfig(struct got_repository *repo)
195 return repo->gotconfig;
199 got_repo_get_gitconfig_remotes(int *nremotes,
200 const struct got_remote_repo **remotes, struct got_repository *repo)
202 *nremotes = repo->ngitconfig_remotes;
203 *remotes = repo->gitconfig_remotes;
207 is_git_repo(struct got_repository *repo)
209 const char *path_git = got_repo_get_path_git_dir(repo);
210 char *path_objects = got_repo_get_path_objects(repo);
211 char *path_refs = got_repo_get_path_refs(repo);
212 char *path_head = get_path_head(repo);
215 struct got_reference *head_ref;
217 if (lstat(path_git, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_objects, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_refs, &sb) == -1)
229 if (!S_ISDIR(sb.st_mode))
232 if (lstat(path_head, &sb) == -1)
234 if (!S_ISREG(sb.st_mode))
237 /* Check if the HEAD reference can be opened. */
238 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
240 got_ref_close(head_ref);
251 const struct got_error *
252 got_repo_pack_fds_open(int **pack_fds)
254 const struct got_error *err = NULL;
255 int i, *pack_fds_tmp;
257 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
258 if (pack_fds_tmp == NULL)
259 return got_error_from_errno("calloc");
260 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
261 if (*pack_fds == NULL) {
263 return got_error_from_errno("calloc");
267 * got_repo_pack_fds_close will try to close all of the
268 * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
269 * a value from got_opentempfd(), resulting in a close(0).
271 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
272 pack_fds_tmp[i] = -1;
274 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
275 pack_fds_tmp[i] = got_opentempfd();
276 if (pack_fds_tmp[i] == -1) {
277 err = got_error_from_errno("got_opentempfd");
278 got_repo_pack_fds_close(pack_fds_tmp);
282 memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
286 const struct got_error *
287 got_repo_pack_fds_close(int *pack_fds)
289 const struct got_error *err = NULL;
292 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
293 if (pack_fds[i] == -1)
295 if (close(pack_fds[i]) == -1) {
296 err = got_error_from_errno("close");
304 const struct got_error *
305 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
306 struct got_object *obj)
308 #ifndef GOT_NO_OBJ_CACHE
309 const struct got_error *err = NULL;
310 err = got_object_cache_add(&repo->objcache, id, obj);
312 if (err->code == GOT_ERR_OBJ_EXISTS ||
313 err->code == GOT_ERR_OBJ_TOO_LARGE)
323 got_repo_get_cached_object(struct got_repository *repo,
324 struct got_object_id *id)
326 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
329 const struct got_error *
330 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
331 struct got_tree_object *tree)
333 #ifndef GOT_NO_OBJ_CACHE
334 const struct got_error *err = NULL;
335 err = got_object_cache_add(&repo->treecache, id, tree);
337 if (err->code == GOT_ERR_OBJ_EXISTS ||
338 err->code == GOT_ERR_OBJ_TOO_LARGE)
347 struct got_tree_object *
348 got_repo_get_cached_tree(struct got_repository *repo,
349 struct got_object_id *id)
351 return (struct got_tree_object *)got_object_cache_get(
352 &repo->treecache, id);
355 const struct got_error *
356 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
357 struct got_commit_object *commit)
359 #ifndef GOT_NO_OBJ_CACHE
360 const struct got_error *err = NULL;
361 err = got_object_cache_add(&repo->commitcache, id, commit);
363 if (err->code == GOT_ERR_OBJ_EXISTS ||
364 err->code == GOT_ERR_OBJ_TOO_LARGE)
373 struct got_commit_object *
374 got_repo_get_cached_commit(struct got_repository *repo,
375 struct got_object_id *id)
377 return (struct got_commit_object *)got_object_cache_get(
378 &repo->commitcache, id);
381 const struct got_error *
382 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
383 struct got_tag_object *tag)
385 #ifndef GOT_NO_OBJ_CACHE
386 const struct got_error *err = NULL;
387 err = got_object_cache_add(&repo->tagcache, id, tag);
389 if (err->code == GOT_ERR_OBJ_EXISTS ||
390 err->code == GOT_ERR_OBJ_TOO_LARGE)
399 struct got_tag_object *
400 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
402 return (struct got_tag_object *)got_object_cache_get(
403 &repo->tagcache, id);
406 const struct got_error *
407 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
408 struct got_raw_object *raw)
410 #ifndef GOT_NO_OBJ_CACHE
411 const struct got_error *err = NULL;
412 err = got_object_cache_add(&repo->rawcache, id, raw);
414 if (err->code == GOT_ERR_OBJ_EXISTS ||
415 err->code == GOT_ERR_OBJ_TOO_LARGE)
425 struct got_raw_object *
426 got_repo_get_cached_raw_object(struct got_repository *repo,
427 struct got_object_id *id)
429 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
433 static const struct got_error *
434 open_repo(struct got_repository *repo, const char *path)
436 const struct got_error *err = NULL;
438 repo->gitdir_fd = -1;
440 /* bare git repository? */
441 repo->path_git_dir = strdup(path);
442 if (repo->path_git_dir == NULL)
443 return got_error_from_errno("strdup");
444 if (is_git_repo(repo)) {
445 repo->path = strdup(repo->path_git_dir);
446 if (repo->path == NULL) {
447 err = got_error_from_errno("strdup");
450 repo->gitdir_fd = open(repo->path_git_dir,
451 O_DIRECTORY | O_CLOEXEC);
452 if (repo->gitdir_fd == -1) {
453 err = got_error_from_errno2("open",
460 /* git repository with working tree? */
461 free(repo->path_git_dir);
462 repo->path_git_dir = NULL;
463 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
464 err = got_error_from_errno("asprintf");
467 if (is_git_repo(repo)) {
468 repo->path = strdup(path);
469 if (repo->path == NULL) {
470 err = got_error_from_errno("strdup");
473 repo->gitdir_fd = open(repo->path_git_dir,
474 O_DIRECTORY | O_CLOEXEC);
475 if (repo->gitdir_fd == -1) {
476 err = got_error_from_errno2("open",
483 err = got_error(GOT_ERR_NOT_GIT_REPO);
488 free(repo->path_git_dir);
489 repo->path_git_dir = NULL;
490 if (repo->gitdir_fd != -1)
491 close(repo->gitdir_fd);
492 repo->gitdir_fd = -1;
498 static const struct got_error *
499 parse_gitconfig_file(int *gitconfig_repository_format_version,
500 char **gitconfig_author_name, char **gitconfig_author_email,
501 struct got_remote_repo **remotes, int *nremotes,
502 char **gitconfig_owner, char ***extensions, int *nextensions,
503 const char *gitconfig_path)
505 const struct got_error *err = NULL, *child_err = NULL;
507 int imsg_fds[2] = { -1, -1 };
509 struct imsgbuf *ibuf;
511 *gitconfig_repository_format_version = 0;
516 *gitconfig_author_name = NULL;
517 *gitconfig_author_email = NULL;
523 *gitconfig_owner = NULL;
525 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
529 return got_error_from_errno2("open", gitconfig_path);
532 ibuf = calloc(1, sizeof(*ibuf));
534 err = got_error_from_errno("calloc");
538 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
539 err = got_error_from_errno("socketpair");
545 err = got_error_from_errno("fork");
547 } else if (pid == 0) {
548 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
553 if (close(imsg_fds[1]) == -1) {
554 err = got_error_from_errno("close");
558 imsg_init(ibuf, imsg_fds[0]);
560 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
565 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
569 err = got_privsep_recv_gitconfig_int(
570 gitconfig_repository_format_version, ibuf);
574 if (extensions && nextensions) {
575 err = got_privsep_send_gitconfig_repository_extensions_req(
579 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
582 if (*nextensions > 0) {
584 *extensions = calloc(*nextensions, sizeof(char *));
585 if (*extensions == NULL) {
586 err = got_error_from_errno("calloc");
589 for (i = 0; i < *nextensions; i++) {
591 err = got_privsep_recv_gitconfig_str(&ext,
595 (*extensions)[i] = ext;
600 err = got_privsep_send_gitconfig_author_name_req(ibuf);
604 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
608 err = got_privsep_send_gitconfig_author_email_req(ibuf);
612 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
616 if (remotes && nremotes) {
617 err = got_privsep_send_gitconfig_remotes_req(ibuf);
621 err = got_privsep_recv_gitconfig_remotes(remotes,
627 if (gitconfig_owner) {
628 err = got_privsep_send_gitconfig_owner_req(ibuf);
631 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
636 err = got_privsep_send_stop(imsg_fds[0]);
637 child_err = got_privsep_wait_for_child(pid);
638 if (child_err && err == NULL)
641 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
642 err = got_error_from_errno("close");
643 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
644 err = got_error_from_errno("close");
645 if (fd != -1 && close(fd) == -1 && err == NULL)
646 err = got_error_from_errno2("close", gitconfig_path);
651 static const struct got_error *
652 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
654 const struct got_error *err = NULL;
655 char *repo_gitconfig_path = NULL;
657 if (global_gitconfig_path) {
658 /* Read settings from ~/.gitconfig. */
659 int dummy_repo_version;
660 err = parse_gitconfig_file(&dummy_repo_version,
661 &repo->global_gitconfig_author_name,
662 &repo->global_gitconfig_author_email,
663 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
668 /* Read repository's .git/config file. */
669 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
670 if (repo_gitconfig_path == NULL)
671 return got_error_from_errno("got_repo_get_path_gitconfig");
673 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
674 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
675 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
676 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
677 repo_gitconfig_path);
681 free(repo_gitconfig_path);
685 static const struct got_error *
686 read_gotconfig(struct got_repository *repo)
688 const struct got_error *err = NULL;
689 char *gotconfig_path;
691 gotconfig_path = got_repo_get_path_gotconfig(repo);
692 if (gotconfig_path == NULL)
693 return got_error_from_errno("got_repo_get_path_gotconfig");
695 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
696 free(gotconfig_path);
700 /* Supported repository format extensions. */
701 static const char *const repo_extensions[] = {
702 "noop", /* Got supports repository format version 1. */
703 "preciousObjects", /* Supported by gotadmin cleanup. */
704 "worktreeConfig", /* Got does not care about Git work trees. */
707 const struct got_error *
708 got_repo_open(struct got_repository **repop, const char *path,
709 const char *global_gitconfig_path, int *pack_fds)
711 struct got_repository *repo = NULL;
712 const struct got_error *err = NULL;
713 char *repo_path = NULL;
719 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
720 return got_error_from_errno("getrlimit");
722 repo = calloc(1, sizeof(*repo));
724 return got_error_from_errno("calloc");
726 RB_INIT(&repo->packidx_bloom_filters);
727 TAILQ_INIT(&repo->packidx_paths);
729 for (i = 0; i < nitems(repo->privsep_children); i++) {
730 memset(&repo->privsep_children[i], 0,
731 sizeof(repo->privsep_children[0]));
732 repo->privsep_children[i].imsg_fd = -1;
735 err = got_object_cache_init(&repo->objcache,
736 GOT_OBJECT_CACHE_TYPE_OBJ);
739 err = got_object_cache_init(&repo->treecache,
740 GOT_OBJECT_CACHE_TYPE_TREE);
743 err = got_object_cache_init(&repo->commitcache,
744 GOT_OBJECT_CACHE_TYPE_COMMIT);
747 err = got_object_cache_init(&repo->tagcache,
748 GOT_OBJECT_CACHE_TYPE_TAG);
751 err = got_object_cache_init(&repo->rawcache,
752 GOT_OBJECT_CACHE_TYPE_RAW);
756 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
757 if (repo->pack_cache_size > rl.rlim_cur / 8)
758 repo->pack_cache_size = rl.rlim_cur / 8;
759 for (i = 0; i < nitems(repo->packs); i++) {
760 if (i < repo->pack_cache_size) {
761 repo->packs[i].basefd = pack_fds[j++];
762 repo->packs[i].accumfd = pack_fds[j++];
764 repo->packs[i].basefd = -1;
765 repo->packs[i].accumfd = -1;
768 repo->pinned_pack = -1;
769 repo->pinned_packidx = -1;
770 repo->pinned_pid = 0;
772 repo_path = realpath(path, NULL);
773 if (repo_path == NULL) {
774 err = got_error_from_errno2("realpath", path);
781 err = open_repo(repo, repo_path);
784 if (err->code != GOT_ERR_NOT_GIT_REPO)
786 if (repo_path[0] == '/' && repo_path[1] == '\0') {
787 err = got_error(GOT_ERR_NOT_GIT_REPO);
790 err = got_path_dirname(&parent_path, repo_path);
794 repo_path = parent_path;
797 err = read_gotconfig(repo);
801 err = read_gitconfig(repo, global_gitconfig_path);
804 if (repo->gitconfig_repository_format_version != 0) {
805 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
808 for (i = 0; i < repo->nextensions; i++) {
809 char *ext = repo->extensions[i];
810 int j, supported = 0;
811 for (j = 0; j < nitems(repo_extensions); j++) {
812 if (strcmp(ext, repo_extensions[j]) == 0) {
818 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
823 err = got_repo_list_packidx(&repo->packidx_paths, repo);
826 got_repo_close(repo);
833 const struct got_error *
834 got_repo_close(struct got_repository *repo)
836 const struct got_error *err = NULL, *child_err;
837 struct got_packidx_bloom_filter *bf;
838 struct got_pathlist_entry *pe;
841 for (i = 0; i < repo->pack_cache_size; i++) {
842 if (repo->packidx_cache[i] == NULL)
844 got_packidx_close(repo->packidx_cache[i]);
847 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
848 &repo->packidx_bloom_filters))) {
849 RB_REMOVE(got_packidx_bloom_filter_tree,
850 &repo->packidx_bloom_filters, bf);
855 for (i = 0; i < repo->pack_cache_size; i++)
856 if (repo->packs[i].path_packfile)
857 if (repo->packs[i].path_packfile)
858 got_pack_close(&repo->packs[i]);
861 free(repo->path_git_dir);
863 got_object_cache_close(&repo->objcache);
864 got_object_cache_close(&repo->treecache);
865 got_object_cache_close(&repo->commitcache);
866 got_object_cache_close(&repo->tagcache);
867 got_object_cache_close(&repo->rawcache);
869 for (i = 0; i < nitems(repo->privsep_children); i++) {
870 if (repo->privsep_children[i].imsg_fd == -1)
872 imsg_clear(repo->privsep_children[i].ibuf);
873 free(repo->privsep_children[i].ibuf);
874 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
875 child_err = got_privsep_wait_for_child(
876 repo->privsep_children[i].pid);
877 if (child_err && err == NULL)
879 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
881 err = got_error_from_errno("close");
884 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
886 err = got_error_from_errno("close");
889 got_gotconfig_free(repo->gotconfig);
890 free(repo->gitconfig_author_name);
891 free(repo->gitconfig_author_email);
892 for (i = 0; i < repo->ngitconfig_remotes; i++)
893 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
894 free(repo->gitconfig_remotes);
895 for (i = 0; i < repo->nextensions; i++)
896 free(repo->extensions[i]);
897 free(repo->extensions);
899 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
900 free((void *)pe->path);
901 got_pathlist_free(&repo->packidx_paths);
908 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
914 free(repo->fetch_url);
915 repo->fetch_url = NULL;
916 free(repo->send_url);
917 repo->send_url = NULL;
918 for (i = 0; i < repo->nfetch_branches; i++)
919 free(repo->fetch_branches[i]);
920 free(repo->fetch_branches);
921 repo->fetch_branches = NULL;
922 repo->nfetch_branches = 0;
923 for (i = 0; i < repo->nsend_branches; i++)
924 free(repo->send_branches[i]);
925 free(repo->send_branches);
926 repo->send_branches = NULL;
927 repo->nsend_branches = 0;
930 const struct got_error *
931 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
932 const char *input_path)
934 const struct got_error *err = NULL;
935 const char *repo_abspath = NULL;
937 char *canonpath, *path = NULL;
939 *in_repo_path = NULL;
941 canonpath = strdup(input_path);
942 if (canonpath == NULL) {
943 err = got_error_from_errno("strdup");
946 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
950 repo_abspath = got_repo_get_path(repo);
952 if (canonpath[0] == '\0') {
953 path = strdup(canonpath);
955 err = got_error_from_errno("strdup");
959 path = realpath(canonpath, NULL);
961 if (errno != ENOENT) {
962 err = got_error_from_errno2("realpath",
967 * Path is not on disk.
968 * Assume it is already relative to repository root.
970 path = strdup(canonpath);
972 err = got_error_from_errno("strdup");
977 repolen = strlen(repo_abspath);
981 if (strcmp(path, repo_abspath) == 0) {
985 err = got_error_from_errno("strdup");
988 } else if (len > repolen &&
989 got_path_is_child(path, repo_abspath, repolen)) {
990 /* Matched an on-disk path inside repository. */
991 if (got_repo_is_bare(repo)) {
993 * Matched an on-disk path inside repository
994 * database. Treat input as repository-relative.
1001 /* Strip common prefix with repository path. */
1002 err = got_path_skip_common_ancestor(&child,
1003 repo_abspath, path);
1011 * Matched unrelated on-disk path.
1012 * Treat input as repository-relative.
1020 /* Make in-repository path absolute */
1021 if (path[0] != '/') {
1023 if (asprintf(&abspath, "/%s", path) == -1) {
1024 err = got_error_from_errno("asprintf");
1036 *in_repo_path = path;
1040 static const struct got_error *
1041 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1042 const char *path_packidx)
1044 const struct got_error *err = NULL;
1047 for (i = 0; i < repo->pack_cache_size; i++) {
1048 if (repo->packidx_cache[i] == NULL)
1050 if (strcmp(repo->packidx_cache[i]->path_packidx,
1051 path_packidx) == 0) {
1052 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1055 if (i == repo->pack_cache_size) {
1058 } while (i > 0 && repo->pinned_packidx >= 0 &&
1059 i == repo->pinned_packidx);
1060 err = got_packidx_close(repo->packidx_cache[i]);
1065 repo->packidx_cache[i] = packidx;
1071 got_repo_is_packidx_filename(const char *name, size_t len)
1073 if (len != GOT_PACKIDX_NAMELEN)
1076 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1079 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1080 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1086 static struct got_packidx_bloom_filter *
1087 get_packidx_bloom_filter(struct got_repository *repo,
1088 const char *path, size_t path_len)
1090 struct got_packidx_bloom_filter key;
1092 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1093 return NULL; /* XXX */
1094 key.path_len = path_len;
1096 return RB_FIND(got_packidx_bloom_filter_tree,
1097 &repo->packidx_bloom_filters, &key);
1101 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1102 const char *path_packidx, struct got_object_id *id)
1104 struct got_packidx_bloom_filter *bf;
1106 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1108 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1110 /* No bloom filter means this pack index must be searched. */
1114 static const struct got_error *
1115 add_packidx_bloom_filter(struct got_repository *repo,
1116 struct got_packidx *packidx, const char *path_packidx)
1118 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1119 struct got_packidx_bloom_filter *bf;
1123 * Don't use bloom filters for very large pack index files.
1124 * Large pack files will contain a relatively large fraction
1125 * of our objects so we will likely need to visit them anyway.
1126 * The more objects a pack file contains the higher the probability
1127 * of a false-positive match from the bloom filter. And reading
1128 * all object IDs from a large pack index file can be expensive.
1130 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1133 /* Do we already have a filter for this pack index? */
1134 if (get_packidx_bloom_filter(repo, path_packidx,
1135 strlen(path_packidx)) != NULL)
1138 bf = calloc(1, sizeof(*bf));
1140 return got_error_from_errno("calloc");
1141 bf->bloom = calloc(1, sizeof(*bf->bloom));
1142 if (bf->bloom == NULL) {
1144 return got_error_from_errno("calloc");
1147 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1148 if (len >= sizeof(bf->path)) {
1151 return got_error(GOT_ERR_NO_SPACE);
1155 /* Minimum size supported by our bloom filter is 1000 entries. */
1156 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1157 for (i = 0; i < nobjects; i++) {
1158 struct got_packidx_object_id *id;
1159 id = &packidx->hdr.sorted_ids[i];
1160 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1163 RB_INSERT(got_packidx_bloom_filter_tree,
1164 &repo->packidx_bloom_filters, bf);
1168 const struct got_error *
1169 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1170 struct got_repository *repo, struct got_object_id *id)
1172 const struct got_error *err;
1173 struct got_pathlist_entry *pe;
1176 /* Search pack index cache. */
1177 for (i = 0; i < repo->pack_cache_size; i++) {
1178 if (repo->packidx_cache[i] == NULL)
1180 if (!got_repo_check_packidx_bloom_filter(repo,
1181 repo->packidx_cache[i]->path_packidx, id))
1182 continue; /* object will not be found in this index */
1183 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1185 *packidx = repo->packidx_cache[i];
1187 * Move this cache entry to the front. Repeatedly
1188 * searching a wrong pack index can be expensive.
1191 memmove(&repo->packidx_cache[1],
1192 &repo->packidx_cache[0],
1193 i * sizeof(repo->packidx_cache[0]));
1194 repo->packidx_cache[0] = *packidx;
1195 if (repo->pinned_packidx >= 0 &&
1196 repo->pinned_packidx < i)
1197 repo->pinned_packidx++;
1198 else if (repo->pinned_packidx == i)
1199 repo->pinned_packidx = 0;
1204 /* No luck. Search the filesystem. */
1206 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1207 const char *path_packidx = pe->path;
1210 if (!got_repo_check_packidx_bloom_filter(repo,
1212 continue; /* object will not be found in this index */
1214 for (i = 0; i < repo->pack_cache_size; i++) {
1215 if (repo->packidx_cache[i] == NULL)
1217 if (strcmp(repo->packidx_cache[i]->path_packidx,
1218 path_packidx) == 0) {
1224 continue; /* already searched */
1226 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1231 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1235 err = cache_packidx(repo, *packidx, path_packidx);
1239 *idx = got_packidx_get_object_idx(*packidx, id);
1241 err = NULL; /* found the object */
1246 err = got_error_no_obj(id);
1251 const struct got_error *
1252 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1253 struct got_repository *repo)
1255 const struct got_error *err = NULL;
1256 DIR *packdir = NULL;
1257 struct dirent *dent;
1258 char *path_packidx = NULL;
1261 packdir_fd = openat(got_repo_get_fd(repo),
1262 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1263 if (packdir_fd == -1) {
1264 return got_error_from_errno_fmt("openat: %s/%s",
1265 got_repo_get_path_git_dir(repo),
1266 GOT_OBJECTS_PACK_DIR);
1269 packdir = fdopendir(packdir_fd);
1270 if (packdir == NULL) {
1271 err = got_error_from_errno("fdopendir");
1275 while ((dent = readdir(packdir)) != NULL) {
1276 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1279 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1280 dent->d_name) == -1) {
1281 err = got_error_from_errno("asprintf");
1282 path_packidx = NULL;
1286 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1293 if (packdir && closedir(packdir) != 0 && err == NULL)
1294 err = got_error_from_errno("closedir");
1298 const struct got_error *
1299 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1300 struct got_repository *repo)
1302 const struct got_error *err;
1307 /* Search pack index cache. */
1308 for (i = 0; i < repo->pack_cache_size; i++) {
1309 if (repo->packidx_cache[i] == NULL)
1311 if (strcmp(repo->packidx_cache[i]->path_packidx,
1312 path_packidx) == 0) {
1313 *packidx = repo->packidx_cache[i];
1317 /* No luck. Search the filesystem. */
1319 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1324 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1328 err = cache_packidx(repo, *packidx, path_packidx);
1331 got_packidx_close(*packidx);
1337 static const struct got_error *
1338 read_packfile_hdr(int fd, struct got_packidx *packidx)
1340 const struct got_error *err = NULL;
1341 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1342 struct got_packfile_hdr hdr;
1345 n = read(fd, &hdr, sizeof(hdr));
1347 return got_error_from_errno("read");
1348 if (n != sizeof(hdr))
1349 return got_error(GOT_ERR_BAD_PACKFILE);
1351 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1352 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1353 be32toh(hdr.nobjects) != totobj)
1354 err = got_error(GOT_ERR_BAD_PACKFILE);
1359 static const struct got_error *
1360 open_packfile(int *fd, struct got_repository *repo,
1361 const char *relpath, struct got_packidx *packidx)
1363 const struct got_error *err = NULL;
1365 *fd = openat(got_repo_get_fd(repo), relpath,
1366 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1368 return got_error_from_errno_fmt("openat: %s/%s",
1369 got_repo_get_path_git_dir(repo), relpath);
1372 err = read_packfile_hdr(*fd, packidx);
1382 const struct got_error *
1383 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1384 const char *path_packfile, struct got_packidx *packidx)
1386 const struct got_error *err = NULL;
1387 struct got_pack *pack = NULL;
1394 for (i = 0; i < repo->pack_cache_size; i++) {
1395 pack = &repo->packs[i];
1396 if (pack->path_packfile == NULL)
1398 if (strcmp(pack->path_packfile, path_packfile) == 0)
1399 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1402 if (i == repo->pack_cache_size) {
1403 struct got_pack tmp;
1406 } while (i > 0 && repo->pinned_pack >= 0 &&
1407 i == repo->pinned_pack);
1408 err = got_pack_close(&repo->packs[i]);
1411 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1412 return got_error_from_errno("ftruncate");
1413 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1414 return got_error_from_errno("ftruncate");
1415 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1416 memcpy(&repo->packs[i], &repo->packs[0],
1417 sizeof(repo->packs[i]));
1418 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1419 if (repo->pinned_pack == 0)
1420 repo->pinned_pack = i;
1421 else if (repo->pinned_pack == i)
1422 repo->pinned_pack = 0;
1426 pack = &repo->packs[i];
1428 pack->path_packfile = strdup(path_packfile);
1429 if (pack->path_packfile == NULL) {
1430 err = got_error_from_errno("strdup");
1434 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1438 if (fstat(pack->fd, &sb) != 0) {
1439 err = got_error_from_errno("fstat");
1442 pack->filesize = sb.st_size;
1444 pack->privsep_child = NULL;
1446 #ifndef GOT_PACK_NO_MMAP
1447 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1449 if (pack->map == MAP_FAILED) {
1450 if (errno != ENOMEM) {
1451 err = got_error_from_errno("mmap");
1454 pack->map = NULL; /* fall back to read(2) */
1460 free(pack->path_packfile);
1461 memset(pack, 0, sizeof(*pack));
1469 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1471 struct got_pack *pack = NULL;
1474 for (i = 0; i < repo->pack_cache_size; i++) {
1475 pack = &repo->packs[i];
1476 if (pack->path_packfile == NULL)
1478 if (strcmp(pack->path_packfile, path_packfile) == 0)
1485 const struct got_error *
1486 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1487 struct got_pack *pack)
1490 int pinned_pack = -1, pinned_packidx = -1;
1492 for (i = 0; i < repo->pack_cache_size; i++) {
1493 if (repo->packidx_cache[i] &&
1494 strcmp(repo->packidx_cache[i]->path_packidx,
1495 packidx->path_packidx) == 0)
1497 if (repo->packs[i].path_packfile &&
1498 strcmp(repo->packs[i].path_packfile,
1499 pack->path_packfile) == 0)
1503 if (pinned_packidx == -1 || pinned_pack == -1)
1504 return got_error(GOT_ERR_PIN_PACK);
1506 repo->pinned_pack = pinned_pack;
1507 repo->pinned_packidx = pinned_packidx;
1508 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1513 got_repo_get_pinned_pack(struct got_repository *repo)
1515 if (repo->pinned_pack >= 0 &&
1516 repo->pinned_pack < repo->pack_cache_size)
1517 return &repo->packs[repo->pinned_pack];
1523 got_repo_unpin_pack(struct got_repository *repo)
1525 repo->pinned_packidx = -1;
1526 repo->pinned_pack = -1;
1527 repo->pinned_pid = 0;
1530 const struct got_error *
1531 got_repo_init(const char *repo_path)
1533 const struct got_error *err = NULL;
1534 const char *dirnames[] = {
1536 GOT_OBJECTS_PACK_DIR,
1539 const char *description_str = "Unnamed repository; "
1540 "edit this file 'description' to name the repository.";
1541 const char *headref_str = "ref: refs/heads/main";
1542 const char *gitconfig_str = "[core]\n"
1543 "\trepositoryformatversion = 0\n"
1544 "\tfilemode = true\n"
1549 if (!got_path_dir_is_empty(repo_path))
1550 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1552 for (i = 0; i < nitems(dirnames); i++) {
1553 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1554 return got_error_from_errno("asprintf");
1556 err = got_path_mkdir(path);
1562 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1563 return got_error_from_errno("asprintf");
1564 err = got_path_create_file(path, description_str);
1569 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1570 return got_error_from_errno("asprintf");
1571 err = got_path_create_file(path, headref_str);
1576 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1577 return got_error_from_errno("asprintf");
1578 err = got_path_create_file(path, gitconfig_str);
1586 static const struct got_error *
1587 match_packed_object(struct got_object_id **unique_id,
1588 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1590 const struct got_error *err = NULL;
1591 struct got_object_id_queue matched_ids;
1592 struct got_pathlist_entry *pe;
1594 STAILQ_INIT(&matched_ids);
1596 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1597 const char *path_packidx = pe->path;
1598 struct got_packidx *packidx;
1599 struct got_object_qid *qid;
1601 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1606 err = got_packidx_match_id_str_prefix(&matched_ids,
1607 packidx, id_str_prefix);
1609 got_packidx_close(packidx);
1612 err = got_packidx_close(packidx);
1616 STAILQ_FOREACH(qid, &matched_ids, entry) {
1617 if (obj_type != GOT_OBJ_TYPE_ANY) {
1619 err = got_object_get_type(&matched_type, repo,
1623 if (matched_type != obj_type)
1626 if (*unique_id == NULL) {
1627 *unique_id = got_object_id_dup(&qid->id);
1628 if (*unique_id == NULL) {
1629 err = got_error_from_errno("malloc");
1633 if (got_object_id_cmp(*unique_id,
1635 continue; /* packed multiple times */
1636 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1642 got_object_id_queue_free(&matched_ids);
1650 static const struct got_error *
1651 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1652 const char *object_dir, const char *id_str_prefix, int obj_type,
1653 struct got_repository *repo)
1655 const struct got_error *err = NULL;
1658 struct dirent *dent;
1659 struct got_object_id id;
1661 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1662 err = got_error_from_errno("asprintf");
1666 dir = opendir(path);
1668 if (errno == ENOENT) {
1672 err = got_error_from_errno2("opendir", path);
1675 while ((dent = readdir(dir)) != NULL) {
1679 if (strcmp(dent->d_name, ".") == 0 ||
1680 strcmp(dent->d_name, "..") == 0)
1683 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1684 err = got_error_from_errno("asprintf");
1688 if (!got_parse_sha1_digest(id.sha1, id_str))
1692 * Directory entries do not necessarily appear in
1693 * sorted order, so we must iterate over all of them.
1695 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1701 if (*unique_id == NULL) {
1702 if (obj_type != GOT_OBJ_TYPE_ANY) {
1704 err = got_object_get_type(&matched_type, repo,
1708 if (matched_type != obj_type)
1711 *unique_id = got_object_id_dup(&id);
1712 if (*unique_id == NULL) {
1713 err = got_error_from_errno("got_object_id_dup");
1718 if (got_object_id_cmp(*unique_id, &id) == 0)
1719 continue; /* both packed and loose */
1720 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1726 if (dir && closedir(dir) != 0 && err == NULL)
1727 err = got_error_from_errno("closedir");
1736 const struct got_error *
1737 got_repo_match_object_id_prefix(struct got_object_id **id,
1738 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1740 const struct got_error *err = NULL;
1741 char *path_objects = got_repo_get_path_objects(repo);
1742 char *object_dir = NULL;
1748 len = strlen(id_str_prefix);
1749 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1750 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1752 for (i = 0; i < len; i++) {
1753 if (isxdigit((unsigned char)id_str_prefix[i]))
1755 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1759 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1762 object_dir = strndup(id_str_prefix, 2);
1763 if (object_dir == NULL) {
1764 err = got_error_from_errno("strdup");
1767 err = match_loose_object(id, path_objects, object_dir,
1768 id_str_prefix, obj_type, repo);
1769 } else if (len == 1) {
1771 for (i = 0; i < 0xf; i++) {
1772 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1774 err = got_error_from_errno("asprintf");
1777 err = match_packed_object(id, repo, object_dir,
1781 err = match_loose_object(id, path_objects, object_dir,
1782 id_str_prefix, obj_type, repo);
1787 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1795 } else if (*id == NULL) {
1797 case GOT_OBJ_TYPE_BLOB:
1798 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1799 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1801 case GOT_OBJ_TYPE_TREE:
1802 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1803 GOT_OBJ_LABEL_TREE, id_str_prefix);
1805 case GOT_OBJ_TYPE_COMMIT:
1806 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1807 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1809 case GOT_OBJ_TYPE_TAG:
1810 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1811 GOT_OBJ_LABEL_TAG, id_str_prefix);
1814 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1822 const struct got_error *
1823 got_repo_match_object_id(struct got_object_id **id, char **label,
1824 const char *id_str, int obj_type, struct got_reflist_head *refs,
1825 struct got_repository *repo)
1827 const struct got_error *err;
1828 struct got_tag_object *tag;
1829 struct got_reference *ref = NULL;
1836 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1839 *id = got_object_id_dup(
1840 got_object_tag_get_object_id(tag));
1842 err = got_error_from_errno("got_object_id_dup");
1843 else if (label && asprintf(label, "refs/tags/%s",
1844 got_object_tag_get_name(tag)) == -1) {
1845 err = got_error_from_errno("asprintf");
1849 got_object_tag_close(tag);
1851 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1852 err->code != GOT_ERR_NO_OBJ)
1856 err = got_ref_open(&ref, repo, id_str, 0);
1858 err = got_ref_resolve(id, repo, ref);
1862 *label = strdup(got_ref_get_name(ref));
1863 if (*label == NULL) {
1864 err = got_error_from_errno("strdup");
1869 if (err->code != GOT_ERR_NOT_REF &&
1870 err->code != GOT_ERR_BAD_REF_NAME)
1872 err = got_repo_match_object_id_prefix(id, id_str,
1875 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1876 err = got_error_not_ref(id_str);
1880 err = got_object_id_str(label, *id);
1881 if (*label == NULL) {
1882 err = got_error_from_errno("strdup");
1893 const struct got_error *
1894 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1895 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1897 const struct got_error *err = NULL;
1898 struct got_reflist_entry *re;
1899 struct got_object_id *tag_id;
1900 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1904 TAILQ_FOREACH(re, refs, entry) {
1905 const char *refname;
1906 refname = got_ref_get_name(re->ref);
1907 if (got_ref_is_symbolic(re->ref))
1909 if (strncmp(refname, "refs/tags/", 10) != 0)
1911 if (!name_is_absolute)
1912 refname += strlen("refs/tags/");
1913 if (strcmp(refname, name) != 0)
1915 err = got_ref_resolve(&tag_id, repo, re->ref);
1918 err = got_object_open_as_tag(tag, repo, tag_id);
1922 if (obj_type == GOT_OBJ_TYPE_ANY ||
1923 got_object_tag_get_object_type(*tag) == obj_type)
1925 got_object_tag_close(*tag);
1929 if (err == NULL && *tag == NULL)
1930 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1931 GOT_OBJ_LABEL_TAG, name);
1935 static const struct got_error *
1936 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1937 const char *name, mode_t mode, struct got_object_id *blob_id)
1939 const struct got_error *err = NULL;
1943 *new_te = calloc(1, sizeof(**new_te));
1944 if (*new_te == NULL)
1945 return got_error_from_errno("calloc");
1947 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1948 sizeof((*new_te)->name)) {
1949 err = got_error(GOT_ERR_NO_SPACE);
1953 if (S_ISLNK(mode)) {
1954 (*new_te)->mode = S_IFLNK;
1956 (*new_te)->mode = S_IFREG;
1957 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1959 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1961 if (err && *new_te) {
1968 static const struct got_error *
1969 import_file(struct got_tree_entry **new_te, struct dirent *de,
1970 const char *path, struct got_repository *repo)
1972 const struct got_error *err;
1973 struct got_object_id *blob_id = NULL;
1977 if (asprintf(&filepath, "%s%s%s", path,
1978 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1979 return got_error_from_errno("asprintf");
1981 if (lstat(filepath, &sb) != 0) {
1982 err = got_error_from_errno2("lstat", path);
1986 err = got_object_blob_create(&blob_id, filepath, repo);
1990 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1999 static const struct got_error *
2000 insert_tree_entry(struct got_tree_entry *new_te,
2001 struct got_pathlist_head *paths)
2003 const struct got_error *err = NULL;
2004 struct got_pathlist_entry *new_pe;
2006 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2010 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2014 static const struct got_error *write_tree(struct got_object_id **,
2015 const char *, struct got_pathlist_head *, struct got_repository *,
2016 got_repo_import_cb progress_cb, void *progress_arg);
2018 static const struct got_error *
2019 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2020 const char *path, struct got_pathlist_head *ignores,
2021 struct got_repository *repo,
2022 got_repo_import_cb progress_cb, void *progress_arg)
2024 const struct got_error *err;
2025 struct got_object_id *id = NULL;
2028 if (asprintf(&subdirpath, "%s%s%s", path,
2029 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2030 return got_error_from_errno("asprintf");
2032 (*new_te) = calloc(1, sizeof(**new_te));
2033 if (*new_te == NULL)
2034 return got_error_from_errno("calloc");
2035 (*new_te)->mode = S_IFDIR;
2036 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2037 sizeof((*new_te)->name)) {
2038 err = got_error(GOT_ERR_NO_SPACE);
2041 err = write_tree(&id, subdirpath, ignores, repo,
2042 progress_cb, progress_arg);
2045 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2057 static const struct got_error *
2058 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2059 struct got_pathlist_head *ignores, struct got_repository *repo,
2060 got_repo_import_cb progress_cb, void *progress_arg)
2062 const struct got_error *err = NULL;
2066 struct got_tree_entry *new_te = NULL;
2067 struct got_pathlist_head paths;
2068 struct got_pathlist_entry *pe;
2070 *new_tree_id = NULL;
2074 dir = opendir(path_dir);
2076 err = got_error_from_errno2("opendir", path_dir);
2081 while ((de = readdir(dir)) != NULL) {
2085 if (strcmp(de->d_name, ".") == 0 ||
2086 strcmp(de->d_name, "..") == 0)
2089 TAILQ_FOREACH(pe, ignores, entry) {
2090 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2098 err = got_path_dirent_type(&type, path_dir, de);
2102 if (type == DT_DIR) {
2103 err = import_subdir(&new_te, de, path_dir,
2104 ignores, repo, progress_cb, progress_arg);
2106 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2111 } else if (type == DT_REG || type == DT_LNK) {
2112 err = import_file(&new_te, de, path_dir, repo);
2118 err = insert_tree_entry(new_te, &paths);
2124 if (TAILQ_EMPTY(&paths)) {
2125 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2126 "cannot create tree without any entries");
2130 TAILQ_FOREACH(pe, &paths, entry) {
2131 struct got_tree_entry *te = pe->data;
2133 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2135 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2136 err = got_error_from_errno("asprintf");
2139 err = (*progress_cb)(progress_arg, path);
2145 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2149 got_pathlist_free(&paths);
2153 const struct got_error *
2154 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2155 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2156 struct got_repository *repo, got_repo_import_cb progress_cb,
2159 const struct got_error *err;
2160 struct got_object_id *new_tree_id;
2162 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2163 progress_cb, progress_arg);
2167 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2168 author, time(NULL), author, time(NULL), logmsg, repo);
2173 const struct got_error *
2174 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2175 struct got_repository *repo)
2177 const struct got_error *err = NULL;
2178 char *path_objects = NULL, *path = NULL;
2180 struct got_object_id id;
2186 path_objects = got_repo_get_path_objects(repo);
2187 if (path_objects == NULL)
2188 return got_error_from_errno("got_repo_get_path_objects");
2190 for (i = 0; i <= 0xff; i++) {
2191 struct dirent *dent;
2193 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2194 err = got_error_from_errno("asprintf");
2198 dir = opendir(path);
2200 if (errno == ENOENT) {
2204 err = got_error_from_errno2("opendir", path);
2208 while ((dent = readdir(dir)) != NULL) {
2213 if (strcmp(dent->d_name, ".") == 0 ||
2214 strcmp(dent->d_name, "..") == 0)
2217 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2218 err = got_error_from_errno("asprintf");
2222 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2228 err = got_object_open_loose_fd(&fd, &id, repo);
2232 if (fstat(fd, &sb) == -1) {
2233 err = got_error_from_errno("fstat");
2238 (*ondisk_size) += sb.st_size;
2240 if (close(fd) == -1) {
2241 err = got_error_from_errno("close");
2246 if (closedir(dir) != 0) {
2247 err = got_error_from_errno("closedir");
2256 if (dir && closedir(dir) != 0 && err == NULL)
2257 err = got_error_from_errno("closedir");
2268 const struct got_error *
2269 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2270 off_t *total_packsize, struct got_repository *repo)
2272 const struct got_error *err = NULL;
2273 DIR *packdir = NULL;
2274 struct dirent *dent;
2275 struct got_packidx *packidx = NULL;
2277 char *path_packfile;
2283 *total_packsize = 0;
2285 packdir_fd = openat(got_repo_get_fd(repo),
2286 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2287 if (packdir_fd == -1) {
2288 return got_error_from_errno_fmt("openat: %s/%s",
2289 got_repo_get_path_git_dir(repo),
2290 GOT_OBJECTS_PACK_DIR);
2293 packdir = fdopendir(packdir_fd);
2294 if (packdir == NULL) {
2295 err = got_error_from_errno("fdopendir");
2299 while ((dent = readdir(packdir)) != NULL) {
2300 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2303 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2304 dent->d_name) == -1) {
2305 err = got_error_from_errno("asprintf");
2309 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2315 if (fstat(packidx->fd, &sb) == -1)
2317 *total_packsize += sb.st_size;
2319 err = got_packidx_get_packfile_path(&path_packfile,
2320 packidx->path_packidx);
2324 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2326 free(path_packfile);
2329 free(path_packfile);
2330 *total_packsize += sb.st_size;
2332 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2336 got_packidx_close(packidx);
2341 got_packidx_close(packidx);
2342 if (packdir && closedir(packdir) != 0 && err == NULL)
2343 err = got_error_from_errno("closedir");
2347 *total_packsize = 0;
2352 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2353 got_packidx_bloom_filter_cmp);