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[GOT_PACK_NUM_TEMPFILES];
257 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
258 if (*pack_fds == NULL)
259 return got_error_from_errno("calloc");
261 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
262 pack_fds_tmp[i] = got_opentempfd();
263 if (pack_fds_tmp[i] == -1) {
264 err = got_error_from_errno("got_opentempfd");
265 got_repo_pack_fds_close(pack_fds_tmp);
269 memcpy(*pack_fds, pack_fds_tmp, sizeof(pack_fds_tmp));
273 const struct got_error *
274 got_repo_pack_fds_close(int *pack_fds)
276 const struct got_error *err = NULL;
279 for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
280 if (pack_fds[i] == -1)
282 if (close(pack_fds[i]) == -1) {
283 err = got_error_from_errno("close");
291 const struct got_error *
292 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
293 struct got_object *obj)
295 #ifndef GOT_NO_OBJ_CACHE
296 const struct got_error *err = NULL;
297 err = got_object_cache_add(&repo->objcache, id, obj);
299 if (err->code == GOT_ERR_OBJ_EXISTS ||
300 err->code == GOT_ERR_OBJ_TOO_LARGE)
310 got_repo_get_cached_object(struct got_repository *repo,
311 struct got_object_id *id)
313 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
316 const struct got_error *
317 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
318 struct got_tree_object *tree)
320 #ifndef GOT_NO_OBJ_CACHE
321 const struct got_error *err = NULL;
322 err = got_object_cache_add(&repo->treecache, id, tree);
324 if (err->code == GOT_ERR_OBJ_EXISTS ||
325 err->code == GOT_ERR_OBJ_TOO_LARGE)
334 struct got_tree_object *
335 got_repo_get_cached_tree(struct got_repository *repo,
336 struct got_object_id *id)
338 return (struct got_tree_object *)got_object_cache_get(
339 &repo->treecache, id);
342 const struct got_error *
343 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
344 struct got_commit_object *commit)
346 #ifndef GOT_NO_OBJ_CACHE
347 const struct got_error *err = NULL;
348 err = got_object_cache_add(&repo->commitcache, id, commit);
350 if (err->code == GOT_ERR_OBJ_EXISTS ||
351 err->code == GOT_ERR_OBJ_TOO_LARGE)
360 struct got_commit_object *
361 got_repo_get_cached_commit(struct got_repository *repo,
362 struct got_object_id *id)
364 return (struct got_commit_object *)got_object_cache_get(
365 &repo->commitcache, id);
368 const struct got_error *
369 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
370 struct got_tag_object *tag)
372 #ifndef GOT_NO_OBJ_CACHE
373 const struct got_error *err = NULL;
374 err = got_object_cache_add(&repo->tagcache, id, tag);
376 if (err->code == GOT_ERR_OBJ_EXISTS ||
377 err->code == GOT_ERR_OBJ_TOO_LARGE)
386 struct got_tag_object *
387 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
389 return (struct got_tag_object *)got_object_cache_get(
390 &repo->tagcache, id);
393 const struct got_error *
394 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
395 struct got_raw_object *raw)
397 #ifndef GOT_NO_OBJ_CACHE
398 const struct got_error *err = NULL;
399 err = got_object_cache_add(&repo->rawcache, id, raw);
401 if (err->code == GOT_ERR_OBJ_EXISTS ||
402 err->code == GOT_ERR_OBJ_TOO_LARGE)
412 struct got_raw_object *
413 got_repo_get_cached_raw_object(struct got_repository *repo,
414 struct got_object_id *id)
416 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
420 static const struct got_error *
421 open_repo(struct got_repository *repo, const char *path)
423 const struct got_error *err = NULL;
425 repo->gitdir_fd = -1;
427 /* bare git repository? */
428 repo->path_git_dir = strdup(path);
429 if (repo->path_git_dir == NULL)
430 return got_error_from_errno("strdup");
431 if (is_git_repo(repo)) {
432 repo->path = strdup(repo->path_git_dir);
433 if (repo->path == NULL) {
434 err = got_error_from_errno("strdup");
437 repo->gitdir_fd = open(repo->path_git_dir,
438 O_DIRECTORY | O_CLOEXEC);
439 if (repo->gitdir_fd == -1) {
440 err = got_error_from_errno2("open",
447 /* git repository with working tree? */
448 free(repo->path_git_dir);
449 repo->path_git_dir = NULL;
450 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
451 err = got_error_from_errno("asprintf");
454 if (is_git_repo(repo)) {
455 repo->path = strdup(path);
456 if (repo->path == NULL) {
457 err = got_error_from_errno("strdup");
460 repo->gitdir_fd = open(repo->path_git_dir,
461 O_DIRECTORY | O_CLOEXEC);
462 if (repo->gitdir_fd == -1) {
463 err = got_error_from_errno2("open",
470 err = got_error(GOT_ERR_NOT_GIT_REPO);
475 free(repo->path_git_dir);
476 repo->path_git_dir = NULL;
477 if (repo->gitdir_fd != -1)
478 close(repo->gitdir_fd);
479 repo->gitdir_fd = -1;
485 static const struct got_error *
486 parse_gitconfig_file(int *gitconfig_repository_format_version,
487 char **gitconfig_author_name, char **gitconfig_author_email,
488 struct got_remote_repo **remotes, int *nremotes,
489 char **gitconfig_owner, char ***extensions, int *nextensions,
490 const char *gitconfig_path)
492 const struct got_error *err = NULL, *child_err = NULL;
494 int imsg_fds[2] = { -1, -1 };
496 struct imsgbuf *ibuf;
498 *gitconfig_repository_format_version = 0;
503 *gitconfig_author_name = NULL;
504 *gitconfig_author_email = NULL;
510 *gitconfig_owner = NULL;
512 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
516 return got_error_from_errno2("open", gitconfig_path);
519 ibuf = calloc(1, sizeof(*ibuf));
521 err = got_error_from_errno("calloc");
525 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
526 err = got_error_from_errno("socketpair");
532 err = got_error_from_errno("fork");
534 } else if (pid == 0) {
535 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
540 if (close(imsg_fds[1]) == -1) {
541 err = got_error_from_errno("close");
545 imsg_init(ibuf, imsg_fds[0]);
547 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
552 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
556 err = got_privsep_recv_gitconfig_int(
557 gitconfig_repository_format_version, ibuf);
561 if (extensions && nextensions) {
562 err = got_privsep_send_gitconfig_repository_extensions_req(
566 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
569 if (*nextensions > 0) {
571 *extensions = calloc(*nextensions, sizeof(char *));
572 if (*extensions == NULL) {
573 err = got_error_from_errno("calloc");
576 for (i = 0; i < *nextensions; i++) {
578 err = got_privsep_recv_gitconfig_str(&ext,
582 (*extensions)[i] = ext;
587 err = got_privsep_send_gitconfig_author_name_req(ibuf);
591 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
595 err = got_privsep_send_gitconfig_author_email_req(ibuf);
599 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
603 if (remotes && nremotes) {
604 err = got_privsep_send_gitconfig_remotes_req(ibuf);
608 err = got_privsep_recv_gitconfig_remotes(remotes,
614 if (gitconfig_owner) {
615 err = got_privsep_send_gitconfig_owner_req(ibuf);
618 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
623 err = got_privsep_send_stop(imsg_fds[0]);
624 child_err = got_privsep_wait_for_child(pid);
625 if (child_err && err == NULL)
628 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
629 err = got_error_from_errno("close");
630 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
631 err = got_error_from_errno("close");
632 if (fd != -1 && close(fd) == -1 && err == NULL)
633 err = got_error_from_errno2("close", gitconfig_path);
638 static const struct got_error *
639 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
641 const struct got_error *err = NULL;
642 char *repo_gitconfig_path = NULL;
644 if (global_gitconfig_path) {
645 /* Read settings from ~/.gitconfig. */
646 int dummy_repo_version;
647 err = parse_gitconfig_file(&dummy_repo_version,
648 &repo->global_gitconfig_author_name,
649 &repo->global_gitconfig_author_email,
650 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
655 /* Read repository's .git/config file. */
656 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
657 if (repo_gitconfig_path == NULL)
658 return got_error_from_errno("got_repo_get_path_gitconfig");
660 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
661 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
662 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
663 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
664 repo_gitconfig_path);
668 free(repo_gitconfig_path);
672 static const struct got_error *
673 read_gotconfig(struct got_repository *repo)
675 const struct got_error *err = NULL;
676 char *gotconfig_path;
678 gotconfig_path = got_repo_get_path_gotconfig(repo);
679 if (gotconfig_path == NULL)
680 return got_error_from_errno("got_repo_get_path_gotconfig");
682 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
683 free(gotconfig_path);
687 /* Supported repository format extensions. */
688 static const char *const repo_extensions[] = {
689 "noop", /* Got supports repository format version 1. */
690 "preciousObjects", /* Supported by gotadmin cleanup. */
691 "worktreeConfig", /* Got does not care about Git work trees. */
694 const struct got_error *
695 got_repo_open(struct got_repository **repop, const char *path,
696 const char *global_gitconfig_path, int *pack_fds)
698 struct got_repository *repo = NULL;
699 const struct got_error *err = NULL;
700 char *repo_path = NULL;
706 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
707 return got_error_from_errno("getrlimit");
709 repo = calloc(1, sizeof(*repo));
711 return got_error_from_errno("calloc");
713 RB_INIT(&repo->packidx_bloom_filters);
714 TAILQ_INIT(&repo->packidx_paths);
716 for (i = 0; i < nitems(repo->privsep_children); i++) {
717 memset(&repo->privsep_children[i], 0,
718 sizeof(repo->privsep_children[0]));
719 repo->privsep_children[i].imsg_fd = -1;
722 err = got_object_cache_init(&repo->objcache,
723 GOT_OBJECT_CACHE_TYPE_OBJ);
726 err = got_object_cache_init(&repo->treecache,
727 GOT_OBJECT_CACHE_TYPE_TREE);
730 err = got_object_cache_init(&repo->commitcache,
731 GOT_OBJECT_CACHE_TYPE_COMMIT);
734 err = got_object_cache_init(&repo->tagcache,
735 GOT_OBJECT_CACHE_TYPE_TAG);
738 err = got_object_cache_init(&repo->rawcache,
739 GOT_OBJECT_CACHE_TYPE_RAW);
743 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
744 if (repo->pack_cache_size > rl.rlim_cur / 8)
745 repo->pack_cache_size = rl.rlim_cur / 8;
746 for (i = 0; i < nitems(repo->packs); i++) {
747 if (i < repo->pack_cache_size) {
748 repo->packs[i].basefd = pack_fds[j++];
749 repo->packs[i].accumfd = pack_fds[j++];
751 repo->packs[i].basefd = -1;
752 repo->packs[i].accumfd = -1;
755 repo->pinned_pack = -1;
756 repo->pinned_packidx = -1;
757 repo->pinned_pid = 0;
759 repo_path = realpath(path, NULL);
760 if (repo_path == NULL) {
761 err = got_error_from_errno2("realpath", path);
768 err = open_repo(repo, repo_path);
771 if (err->code != GOT_ERR_NOT_GIT_REPO)
773 if (repo_path[0] == '/' && repo_path[1] == '\0') {
774 err = got_error(GOT_ERR_NOT_GIT_REPO);
777 err = got_path_dirname(&parent_path, repo_path);
781 repo_path = parent_path;
784 err = read_gotconfig(repo);
788 err = read_gitconfig(repo, global_gitconfig_path);
791 if (repo->gitconfig_repository_format_version != 0) {
792 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
795 for (i = 0; i < repo->nextensions; i++) {
796 char *ext = repo->extensions[i];
797 int j, supported = 0;
798 for (j = 0; j < nitems(repo_extensions); j++) {
799 if (strcmp(ext, repo_extensions[j]) == 0) {
805 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
810 err = got_repo_list_packidx(&repo->packidx_paths, repo);
813 got_repo_close(repo);
820 const struct got_error *
821 got_repo_close(struct got_repository *repo)
823 const struct got_error *err = NULL, *child_err;
824 struct got_packidx_bloom_filter *bf;
825 struct got_pathlist_entry *pe;
828 for (i = 0; i < repo->pack_cache_size; i++) {
829 if (repo->packidx_cache[i] == NULL)
831 got_packidx_close(repo->packidx_cache[i]);
834 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
835 &repo->packidx_bloom_filters))) {
836 RB_REMOVE(got_packidx_bloom_filter_tree,
837 &repo->packidx_bloom_filters, bf);
842 for (i = 0; i < repo->pack_cache_size; i++)
843 if (repo->packs[i].path_packfile)
844 if (repo->packs[i].path_packfile)
845 got_pack_close(&repo->packs[i]);
848 free(repo->path_git_dir);
850 got_object_cache_close(&repo->objcache);
851 got_object_cache_close(&repo->treecache);
852 got_object_cache_close(&repo->commitcache);
853 got_object_cache_close(&repo->tagcache);
854 got_object_cache_close(&repo->rawcache);
856 for (i = 0; i < nitems(repo->privsep_children); i++) {
857 if (repo->privsep_children[i].imsg_fd == -1)
859 imsg_clear(repo->privsep_children[i].ibuf);
860 free(repo->privsep_children[i].ibuf);
861 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
862 child_err = got_privsep_wait_for_child(
863 repo->privsep_children[i].pid);
864 if (child_err && err == NULL)
866 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
868 err = got_error_from_errno("close");
871 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
873 err = got_error_from_errno("close");
876 got_gotconfig_free(repo->gotconfig);
877 free(repo->gitconfig_author_name);
878 free(repo->gitconfig_author_email);
879 for (i = 0; i < repo->ngitconfig_remotes; i++)
880 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
881 free(repo->gitconfig_remotes);
882 for (i = 0; i < repo->nextensions; i++)
883 free(repo->extensions[i]);
884 free(repo->extensions);
886 TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
887 free((void *)pe->path);
888 got_pathlist_free(&repo->packidx_paths);
895 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
901 free(repo->fetch_url);
902 repo->fetch_url = NULL;
903 free(repo->send_url);
904 repo->send_url = NULL;
905 for (i = 0; i < repo->nfetch_branches; i++)
906 free(repo->fetch_branches[i]);
907 free(repo->fetch_branches);
908 repo->fetch_branches = NULL;
909 repo->nfetch_branches = 0;
910 for (i = 0; i < repo->nsend_branches; i++)
911 free(repo->send_branches[i]);
912 free(repo->send_branches);
913 repo->send_branches = NULL;
914 repo->nsend_branches = 0;
917 const struct got_error *
918 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
919 const char *input_path)
921 const struct got_error *err = NULL;
922 const char *repo_abspath = NULL;
924 char *canonpath, *path = NULL;
926 *in_repo_path = NULL;
928 canonpath = strdup(input_path);
929 if (canonpath == NULL) {
930 err = got_error_from_errno("strdup");
933 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
937 repo_abspath = got_repo_get_path(repo);
939 if (canonpath[0] == '\0') {
940 path = strdup(canonpath);
942 err = got_error_from_errno("strdup");
946 path = realpath(canonpath, NULL);
948 if (errno != ENOENT) {
949 err = got_error_from_errno2("realpath",
954 * Path is not on disk.
955 * Assume it is already relative to repository root.
957 path = strdup(canonpath);
959 err = got_error_from_errno("strdup");
964 repolen = strlen(repo_abspath);
968 if (strcmp(path, repo_abspath) == 0) {
972 err = got_error_from_errno("strdup");
975 } else if (len > repolen &&
976 got_path_is_child(path, repo_abspath, repolen)) {
977 /* Matched an on-disk path inside repository. */
978 if (got_repo_is_bare(repo)) {
980 * Matched an on-disk path inside repository
981 * database. Treat input as repository-relative.
988 /* Strip common prefix with repository path. */
989 err = got_path_skip_common_ancestor(&child,
998 * Matched unrelated on-disk path.
999 * Treat input as repository-relative.
1007 /* Make in-repository path absolute */
1008 if (path[0] != '/') {
1010 if (asprintf(&abspath, "/%s", path) == -1) {
1011 err = got_error_from_errno("asprintf");
1023 *in_repo_path = path;
1027 static const struct got_error *
1028 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1029 const char *path_packidx)
1031 const struct got_error *err = NULL;
1034 for (i = 0; i < repo->pack_cache_size; i++) {
1035 if (repo->packidx_cache[i] == NULL)
1037 if (strcmp(repo->packidx_cache[i]->path_packidx,
1038 path_packidx) == 0) {
1039 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1042 if (i == repo->pack_cache_size) {
1045 } while (i > 0 && repo->pinned_packidx >= 0 &&
1046 i == repo->pinned_packidx);
1047 err = got_packidx_close(repo->packidx_cache[i]);
1052 repo->packidx_cache[i] = packidx;
1058 got_repo_is_packidx_filename(const char *name, size_t len)
1060 if (len != GOT_PACKIDX_NAMELEN)
1063 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1066 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1067 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1073 static struct got_packidx_bloom_filter *
1074 get_packidx_bloom_filter(struct got_repository *repo,
1075 const char *path, size_t path_len)
1077 struct got_packidx_bloom_filter key;
1079 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1080 return NULL; /* XXX */
1081 key.path_len = path_len;
1083 return RB_FIND(got_packidx_bloom_filter_tree,
1084 &repo->packidx_bloom_filters, &key);
1088 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1089 const char *path_packidx, struct got_object_id *id)
1091 struct got_packidx_bloom_filter *bf;
1093 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1095 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1097 /* No bloom filter means this pack index must be searched. */
1101 static const struct got_error *
1102 add_packidx_bloom_filter(struct got_repository *repo,
1103 struct got_packidx *packidx, const char *path_packidx)
1105 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1106 struct got_packidx_bloom_filter *bf;
1110 * Don't use bloom filters for very large pack index files.
1111 * Large pack files will contain a relatively large fraction
1112 * of our objects so we will likely need to visit them anyway.
1113 * The more objects a pack file contains the higher the probability
1114 * of a false-positive match from the bloom filter. And reading
1115 * all object IDs from a large pack index file can be expensive.
1117 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1120 /* Do we already have a filter for this pack index? */
1121 if (get_packidx_bloom_filter(repo, path_packidx,
1122 strlen(path_packidx)) != NULL)
1125 bf = calloc(1, sizeof(*bf));
1127 return got_error_from_errno("calloc");
1128 bf->bloom = calloc(1, sizeof(*bf->bloom));
1129 if (bf->bloom == NULL) {
1131 return got_error_from_errno("calloc");
1134 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1135 if (len >= sizeof(bf->path)) {
1138 return got_error(GOT_ERR_NO_SPACE);
1142 /* Minimum size supported by our bloom filter is 1000 entries. */
1143 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1144 for (i = 0; i < nobjects; i++) {
1145 struct got_packidx_object_id *id;
1146 id = &packidx->hdr.sorted_ids[i];
1147 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1150 RB_INSERT(got_packidx_bloom_filter_tree,
1151 &repo->packidx_bloom_filters, bf);
1155 const struct got_error *
1156 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1157 struct got_repository *repo, struct got_object_id *id)
1159 const struct got_error *err;
1160 struct got_pathlist_entry *pe;
1163 /* Search pack index cache. */
1164 for (i = 0; i < repo->pack_cache_size; i++) {
1165 if (repo->packidx_cache[i] == NULL)
1167 if (!got_repo_check_packidx_bloom_filter(repo,
1168 repo->packidx_cache[i]->path_packidx, id))
1169 continue; /* object will not be found in this index */
1170 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1172 *packidx = repo->packidx_cache[i];
1174 * Move this cache entry to the front. Repeatedly
1175 * searching a wrong pack index can be expensive.
1178 memmove(&repo->packidx_cache[1],
1179 &repo->packidx_cache[0],
1180 i * sizeof(repo->packidx_cache[0]));
1181 repo->packidx_cache[0] = *packidx;
1182 if (repo->pinned_packidx >= 0 &&
1183 repo->pinned_packidx < i)
1184 repo->pinned_packidx++;
1185 else if (repo->pinned_packidx == i)
1186 repo->pinned_packidx = 0;
1191 /* No luck. Search the filesystem. */
1193 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1194 const char *path_packidx = pe->path;
1197 if (!got_repo_check_packidx_bloom_filter(repo,
1199 continue; /* object will not be found in this index */
1201 for (i = 0; i < repo->pack_cache_size; i++) {
1202 if (repo->packidx_cache[i] == NULL)
1204 if (strcmp(repo->packidx_cache[i]->path_packidx,
1205 path_packidx) == 0) {
1211 continue; /* already searched */
1213 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1218 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1222 err = cache_packidx(repo, *packidx, path_packidx);
1226 *idx = got_packidx_get_object_idx(*packidx, id);
1228 err = NULL; /* found the object */
1233 err = got_error_no_obj(id);
1238 const struct got_error *
1239 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1240 struct got_repository *repo)
1242 const struct got_error *err = NULL;
1243 DIR *packdir = NULL;
1244 struct dirent *dent;
1245 char *path_packidx = NULL;
1248 packdir_fd = openat(got_repo_get_fd(repo),
1249 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1250 if (packdir_fd == -1) {
1251 return got_error_from_errno_fmt("openat: %s/%s",
1252 got_repo_get_path_git_dir(repo),
1253 GOT_OBJECTS_PACK_DIR);
1256 packdir = fdopendir(packdir_fd);
1257 if (packdir == NULL) {
1258 err = got_error_from_errno("fdopendir");
1262 while ((dent = readdir(packdir)) != NULL) {
1263 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1266 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1267 dent->d_name) == -1) {
1268 err = got_error_from_errno("asprintf");
1269 path_packidx = NULL;
1273 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1280 if (packdir && closedir(packdir) != 0 && err == NULL)
1281 err = got_error_from_errno("closedir");
1285 const struct got_error *
1286 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1287 struct got_repository *repo)
1289 const struct got_error *err;
1294 /* Search pack index cache. */
1295 for (i = 0; i < repo->pack_cache_size; i++) {
1296 if (repo->packidx_cache[i] == NULL)
1298 if (strcmp(repo->packidx_cache[i]->path_packidx,
1299 path_packidx) == 0) {
1300 *packidx = repo->packidx_cache[i];
1304 /* No luck. Search the filesystem. */
1306 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1311 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1315 err = cache_packidx(repo, *packidx, path_packidx);
1318 got_packidx_close(*packidx);
1324 static const struct got_error *
1325 read_packfile_hdr(int fd, struct got_packidx *packidx)
1327 const struct got_error *err = NULL;
1328 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1329 struct got_packfile_hdr hdr;
1332 n = read(fd, &hdr, sizeof(hdr));
1334 return got_error_from_errno("read");
1335 if (n != sizeof(hdr))
1336 return got_error(GOT_ERR_BAD_PACKFILE);
1338 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1339 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1340 be32toh(hdr.nobjects) != totobj)
1341 err = got_error(GOT_ERR_BAD_PACKFILE);
1346 static const struct got_error *
1347 open_packfile(int *fd, struct got_repository *repo,
1348 const char *relpath, struct got_packidx *packidx)
1350 const struct got_error *err = NULL;
1352 *fd = openat(got_repo_get_fd(repo), relpath,
1353 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1355 return got_error_from_errno_fmt("openat: %s/%s",
1356 got_repo_get_path_git_dir(repo), relpath);
1359 err = read_packfile_hdr(*fd, packidx);
1369 const struct got_error *
1370 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1371 const char *path_packfile, struct got_packidx *packidx)
1373 const struct got_error *err = NULL;
1374 struct got_pack *pack = NULL;
1381 for (i = 0; i < repo->pack_cache_size; i++) {
1382 pack = &repo->packs[i];
1383 if (pack->path_packfile == NULL)
1385 if (strcmp(pack->path_packfile, path_packfile) == 0)
1386 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1389 if (i == repo->pack_cache_size) {
1390 struct got_pack tmp;
1393 } while (i > 0 && repo->pinned_pack >= 0 &&
1394 i == repo->pinned_pack);
1395 err = got_pack_close(&repo->packs[i]);
1398 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1399 return got_error_from_errno("ftruncate");
1400 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1401 return got_error_from_errno("ftruncate");
1402 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1403 memcpy(&repo->packs[i], &repo->packs[0],
1404 sizeof(repo->packs[i]));
1405 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1406 if (repo->pinned_pack == 0)
1407 repo->pinned_pack = i;
1408 else if (repo->pinned_pack == i)
1409 repo->pinned_pack = 0;
1413 pack = &repo->packs[i];
1415 pack->path_packfile = strdup(path_packfile);
1416 if (pack->path_packfile == NULL) {
1417 err = got_error_from_errno("strdup");
1421 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1425 if (fstat(pack->fd, &sb) != 0) {
1426 err = got_error_from_errno("fstat");
1429 pack->filesize = sb.st_size;
1431 pack->privsep_child = NULL;
1433 #ifndef GOT_PACK_NO_MMAP
1434 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1436 if (pack->map == MAP_FAILED) {
1437 if (errno != ENOMEM) {
1438 err = got_error_from_errno("mmap");
1441 pack->map = NULL; /* fall back to read(2) */
1447 free(pack->path_packfile);
1448 memset(pack, 0, sizeof(*pack));
1456 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1458 struct got_pack *pack = NULL;
1461 for (i = 0; i < repo->pack_cache_size; i++) {
1462 pack = &repo->packs[i];
1463 if (pack->path_packfile == NULL)
1465 if (strcmp(pack->path_packfile, path_packfile) == 0)
1472 const struct got_error *
1473 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1474 struct got_pack *pack)
1477 int pinned_pack = -1, pinned_packidx = -1;
1479 for (i = 0; i < repo->pack_cache_size; i++) {
1480 if (repo->packidx_cache[i] &&
1481 strcmp(repo->packidx_cache[i]->path_packidx,
1482 packidx->path_packidx) == 0)
1484 if (repo->packs[i].path_packfile &&
1485 strcmp(repo->packs[i].path_packfile,
1486 pack->path_packfile) == 0)
1490 if (pinned_packidx == -1 || pinned_pack == -1)
1491 return got_error(GOT_ERR_PIN_PACK);
1493 repo->pinned_pack = pinned_pack;
1494 repo->pinned_packidx = pinned_packidx;
1495 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1500 got_repo_get_pinned_pack(struct got_repository *repo)
1502 if (repo->pinned_pack >= 0 &&
1503 repo->pinned_pack < repo->pack_cache_size)
1504 return &repo->packs[repo->pinned_pack];
1510 got_repo_unpin_pack(struct got_repository *repo)
1512 repo->pinned_packidx = -1;
1513 repo->pinned_pack = -1;
1514 repo->pinned_pid = 0;
1517 const struct got_error *
1518 got_repo_init(const char *repo_path)
1520 const struct got_error *err = NULL;
1521 const char *dirnames[] = {
1523 GOT_OBJECTS_PACK_DIR,
1526 const char *description_str = "Unnamed repository; "
1527 "edit this file 'description' to name the repository.";
1528 const char *headref_str = "ref: refs/heads/main";
1529 const char *gitconfig_str = "[core]\n"
1530 "\trepositoryformatversion = 0\n"
1531 "\tfilemode = true\n"
1536 if (!got_path_dir_is_empty(repo_path))
1537 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1539 for (i = 0; i < nitems(dirnames); i++) {
1540 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1541 return got_error_from_errno("asprintf");
1543 err = got_path_mkdir(path);
1549 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1550 return got_error_from_errno("asprintf");
1551 err = got_path_create_file(path, description_str);
1556 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1557 return got_error_from_errno("asprintf");
1558 err = got_path_create_file(path, headref_str);
1563 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1564 return got_error_from_errno("asprintf");
1565 err = got_path_create_file(path, gitconfig_str);
1573 static const struct got_error *
1574 match_packed_object(struct got_object_id **unique_id,
1575 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1577 const struct got_error *err = NULL;
1578 struct got_object_id_queue matched_ids;
1579 struct got_pathlist_entry *pe;
1581 STAILQ_INIT(&matched_ids);
1583 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1584 const char *path_packidx = pe->path;
1585 struct got_packidx *packidx;
1586 struct got_object_qid *qid;
1588 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1593 err = got_packidx_match_id_str_prefix(&matched_ids,
1594 packidx, id_str_prefix);
1596 got_packidx_close(packidx);
1599 err = got_packidx_close(packidx);
1603 STAILQ_FOREACH(qid, &matched_ids, entry) {
1604 if (obj_type != GOT_OBJ_TYPE_ANY) {
1606 err = got_object_get_type(&matched_type, repo,
1610 if (matched_type != obj_type)
1613 if (*unique_id == NULL) {
1614 *unique_id = got_object_id_dup(&qid->id);
1615 if (*unique_id == NULL) {
1616 err = got_error_from_errno("malloc");
1620 if (got_object_id_cmp(*unique_id,
1622 continue; /* packed multiple times */
1623 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1629 got_object_id_queue_free(&matched_ids);
1637 static const struct got_error *
1638 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1639 const char *object_dir, const char *id_str_prefix, int obj_type,
1640 struct got_repository *repo)
1642 const struct got_error *err = NULL;
1645 struct dirent *dent;
1646 struct got_object_id id;
1648 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1649 err = got_error_from_errno("asprintf");
1653 dir = opendir(path);
1655 if (errno == ENOENT) {
1659 err = got_error_from_errno2("opendir", path);
1662 while ((dent = readdir(dir)) != NULL) {
1666 if (strcmp(dent->d_name, ".") == 0 ||
1667 strcmp(dent->d_name, "..") == 0)
1670 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1671 err = got_error_from_errno("asprintf");
1675 if (!got_parse_sha1_digest(id.sha1, id_str))
1679 * Directory entries do not necessarily appear in
1680 * sorted order, so we must iterate over all of them.
1682 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1688 if (*unique_id == NULL) {
1689 if (obj_type != GOT_OBJ_TYPE_ANY) {
1691 err = got_object_get_type(&matched_type, repo,
1695 if (matched_type != obj_type)
1698 *unique_id = got_object_id_dup(&id);
1699 if (*unique_id == NULL) {
1700 err = got_error_from_errno("got_object_id_dup");
1705 if (got_object_id_cmp(*unique_id, &id) == 0)
1706 continue; /* both packed and loose */
1707 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1713 if (dir && closedir(dir) != 0 && err == NULL)
1714 err = got_error_from_errno("closedir");
1723 const struct got_error *
1724 got_repo_match_object_id_prefix(struct got_object_id **id,
1725 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1727 const struct got_error *err = NULL;
1728 char *path_objects = got_repo_get_path_objects(repo);
1729 char *object_dir = NULL;
1735 len = strlen(id_str_prefix);
1736 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1737 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1739 for (i = 0; i < len; i++) {
1740 if (isxdigit((unsigned char)id_str_prefix[i]))
1742 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1746 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1749 object_dir = strndup(id_str_prefix, 2);
1750 if (object_dir == NULL) {
1751 err = got_error_from_errno("strdup");
1754 err = match_loose_object(id, path_objects, object_dir,
1755 id_str_prefix, obj_type, repo);
1756 } else if (len == 1) {
1758 for (i = 0; i < 0xf; i++) {
1759 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1761 err = got_error_from_errno("asprintf");
1764 err = match_packed_object(id, repo, object_dir,
1768 err = match_loose_object(id, path_objects, object_dir,
1769 id_str_prefix, obj_type, repo);
1774 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1782 } else if (*id == NULL) {
1784 case GOT_OBJ_TYPE_BLOB:
1785 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1786 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1788 case GOT_OBJ_TYPE_TREE:
1789 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1790 GOT_OBJ_LABEL_TREE, id_str_prefix);
1792 case GOT_OBJ_TYPE_COMMIT:
1793 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1794 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1796 case GOT_OBJ_TYPE_TAG:
1797 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1798 GOT_OBJ_LABEL_TAG, id_str_prefix);
1801 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1809 const struct got_error *
1810 got_repo_match_object_id(struct got_object_id **id, char **label,
1811 const char *id_str, int obj_type, struct got_reflist_head *refs,
1812 struct got_repository *repo)
1814 const struct got_error *err;
1815 struct got_tag_object *tag;
1816 struct got_reference *ref = NULL;
1823 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1826 *id = got_object_id_dup(
1827 got_object_tag_get_object_id(tag));
1829 err = got_error_from_errno("got_object_id_dup");
1830 else if (label && asprintf(label, "refs/tags/%s",
1831 got_object_tag_get_name(tag)) == -1) {
1832 err = got_error_from_errno("asprintf");
1836 got_object_tag_close(tag);
1838 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1839 err->code != GOT_ERR_NO_OBJ)
1843 err = got_ref_open(&ref, repo, id_str, 0);
1845 err = got_ref_resolve(id, repo, ref);
1849 *label = strdup(got_ref_get_name(ref));
1850 if (*label == NULL) {
1851 err = got_error_from_errno("strdup");
1856 if (err->code != GOT_ERR_NOT_REF &&
1857 err->code != GOT_ERR_BAD_REF_NAME)
1859 err = got_repo_match_object_id_prefix(id, id_str,
1862 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1863 err = got_error_not_ref(id_str);
1867 err = got_object_id_str(label, *id);
1868 if (*label == NULL) {
1869 err = got_error_from_errno("strdup");
1880 const struct got_error *
1881 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1882 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1884 const struct got_error *err = NULL;
1885 struct got_reflist_entry *re;
1886 struct got_object_id *tag_id;
1887 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1891 TAILQ_FOREACH(re, refs, entry) {
1892 const char *refname;
1893 refname = got_ref_get_name(re->ref);
1894 if (got_ref_is_symbolic(re->ref))
1896 if (strncmp(refname, "refs/tags/", 10) != 0)
1898 if (!name_is_absolute)
1899 refname += strlen("refs/tags/");
1900 if (strcmp(refname, name) != 0)
1902 err = got_ref_resolve(&tag_id, repo, re->ref);
1905 err = got_object_open_as_tag(tag, repo, tag_id);
1909 if (obj_type == GOT_OBJ_TYPE_ANY ||
1910 got_object_tag_get_object_type(*tag) == obj_type)
1912 got_object_tag_close(*tag);
1916 if (err == NULL && *tag == NULL)
1917 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1918 GOT_OBJ_LABEL_TAG, name);
1922 static const struct got_error *
1923 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1924 const char *name, mode_t mode, struct got_object_id *blob_id)
1926 const struct got_error *err = NULL;
1930 *new_te = calloc(1, sizeof(**new_te));
1931 if (*new_te == NULL)
1932 return got_error_from_errno("calloc");
1934 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1935 sizeof((*new_te)->name)) {
1936 err = got_error(GOT_ERR_NO_SPACE);
1940 if (S_ISLNK(mode)) {
1941 (*new_te)->mode = S_IFLNK;
1943 (*new_te)->mode = S_IFREG;
1944 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1946 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1948 if (err && *new_te) {
1955 static const struct got_error *
1956 import_file(struct got_tree_entry **new_te, struct dirent *de,
1957 const char *path, struct got_repository *repo)
1959 const struct got_error *err;
1960 struct got_object_id *blob_id = NULL;
1964 if (asprintf(&filepath, "%s%s%s", path,
1965 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1966 return got_error_from_errno("asprintf");
1968 if (lstat(filepath, &sb) != 0) {
1969 err = got_error_from_errno2("lstat", path);
1973 err = got_object_blob_create(&blob_id, filepath, repo);
1977 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1986 static const struct got_error *
1987 insert_tree_entry(struct got_tree_entry *new_te,
1988 struct got_pathlist_head *paths)
1990 const struct got_error *err = NULL;
1991 struct got_pathlist_entry *new_pe;
1993 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1997 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2001 static const struct got_error *write_tree(struct got_object_id **,
2002 const char *, struct got_pathlist_head *, struct got_repository *,
2003 got_repo_import_cb progress_cb, void *progress_arg);
2005 static const struct got_error *
2006 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2007 const char *path, struct got_pathlist_head *ignores,
2008 struct got_repository *repo,
2009 got_repo_import_cb progress_cb, void *progress_arg)
2011 const struct got_error *err;
2012 struct got_object_id *id = NULL;
2015 if (asprintf(&subdirpath, "%s%s%s", path,
2016 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2017 return got_error_from_errno("asprintf");
2019 (*new_te) = calloc(1, sizeof(**new_te));
2020 if (*new_te == NULL)
2021 return got_error_from_errno("calloc");
2022 (*new_te)->mode = S_IFDIR;
2023 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2024 sizeof((*new_te)->name)) {
2025 err = got_error(GOT_ERR_NO_SPACE);
2028 err = write_tree(&id, subdirpath, ignores, repo,
2029 progress_cb, progress_arg);
2032 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2044 static const struct got_error *
2045 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2046 struct got_pathlist_head *ignores, struct got_repository *repo,
2047 got_repo_import_cb progress_cb, void *progress_arg)
2049 const struct got_error *err = NULL;
2053 struct got_tree_entry *new_te = NULL;
2054 struct got_pathlist_head paths;
2055 struct got_pathlist_entry *pe;
2057 *new_tree_id = NULL;
2061 dir = opendir(path_dir);
2063 err = got_error_from_errno2("opendir", path_dir);
2068 while ((de = readdir(dir)) != NULL) {
2072 if (strcmp(de->d_name, ".") == 0 ||
2073 strcmp(de->d_name, "..") == 0)
2076 TAILQ_FOREACH(pe, ignores, entry) {
2077 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2085 err = got_path_dirent_type(&type, path_dir, de);
2089 if (type == DT_DIR) {
2090 err = import_subdir(&new_te, de, path_dir,
2091 ignores, repo, progress_cb, progress_arg);
2093 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2098 } else if (type == DT_REG || type == DT_LNK) {
2099 err = import_file(&new_te, de, path_dir, repo);
2105 err = insert_tree_entry(new_te, &paths);
2111 if (TAILQ_EMPTY(&paths)) {
2112 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2113 "cannot create tree without any entries");
2117 TAILQ_FOREACH(pe, &paths, entry) {
2118 struct got_tree_entry *te = pe->data;
2120 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2122 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2123 err = got_error_from_errno("asprintf");
2126 err = (*progress_cb)(progress_arg, path);
2132 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2136 got_pathlist_free(&paths);
2140 const struct got_error *
2141 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2142 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2143 struct got_repository *repo, got_repo_import_cb progress_cb,
2146 const struct got_error *err;
2147 struct got_object_id *new_tree_id;
2149 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2150 progress_cb, progress_arg);
2154 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2155 author, time(NULL), author, time(NULL), logmsg, repo);
2160 const struct got_error *
2161 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2162 struct got_repository *repo)
2164 const struct got_error *err = NULL;
2165 char *path_objects = NULL, *path = NULL;
2167 struct got_object_id id;
2173 path_objects = got_repo_get_path_objects(repo);
2174 if (path_objects == NULL)
2175 return got_error_from_errno("got_repo_get_path_objects");
2177 for (i = 0; i <= 0xff; i++) {
2178 struct dirent *dent;
2180 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2181 err = got_error_from_errno("asprintf");
2185 dir = opendir(path);
2187 if (errno == ENOENT) {
2191 err = got_error_from_errno2("opendir", path);
2195 while ((dent = readdir(dir)) != NULL) {
2200 if (strcmp(dent->d_name, ".") == 0 ||
2201 strcmp(dent->d_name, "..") == 0)
2204 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2205 err = got_error_from_errno("asprintf");
2209 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2215 err = got_object_open_loose_fd(&fd, &id, repo);
2219 if (fstat(fd, &sb) == -1) {
2220 err = got_error_from_errno("fstat");
2225 (*ondisk_size) += sb.st_size;
2227 if (close(fd) == -1) {
2228 err = got_error_from_errno("close");
2233 if (closedir(dir) != 0) {
2234 err = got_error_from_errno("closedir");
2243 if (dir && closedir(dir) != 0 && err == NULL)
2244 err = got_error_from_errno("closedir");
2255 const struct got_error *
2256 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2257 off_t *total_packsize, struct got_repository *repo)
2259 const struct got_error *err = NULL;
2260 DIR *packdir = NULL;
2261 struct dirent *dent;
2262 struct got_packidx *packidx = NULL;
2264 char *path_packfile;
2270 *total_packsize = 0;
2272 packdir_fd = openat(got_repo_get_fd(repo),
2273 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2274 if (packdir_fd == -1) {
2275 return got_error_from_errno_fmt("openat: %s/%s",
2276 got_repo_get_path_git_dir(repo),
2277 GOT_OBJECTS_PACK_DIR);
2280 packdir = fdopendir(packdir_fd);
2281 if (packdir == NULL) {
2282 err = got_error_from_errno("fdopendir");
2286 while ((dent = readdir(packdir)) != NULL) {
2287 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2290 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2291 dent->d_name) == -1) {
2292 err = got_error_from_errno("asprintf");
2296 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2302 if (fstat(packidx->fd, &sb) == -1)
2304 *total_packsize += sb.st_size;
2306 err = got_packidx_get_packfile_path(&path_packfile,
2307 packidx->path_packidx);
2311 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2313 free(path_packfile);
2316 free(path_packfile);
2317 *total_packsize += sb.st_size;
2319 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2323 got_packidx_close(packidx);
2328 got_packidx_close(packidx);
2329 if (packdir && closedir(packdir) != 0 && err == NULL)
2330 err = got_error_from_errno("closedir");
2334 *total_packsize = 0;
2339 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2340 got_packidx_bloom_filter_cmp);