2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
48 #include "got_opentemp.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
60 #include "got_lib_gotconfig.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
69 got_packidx_bloom_filter_cmp);
72 got_repo_get_path(struct got_repository *repo)
78 got_repo_get_path_git_dir(struct got_repository *repo)
80 return repo->path_git_dir;
84 got_repo_get_fd(struct got_repository *repo)
86 return repo->gitdir_fd;
90 got_repo_get_gitconfig_author_name(struct got_repository *repo)
92 return repo->gitconfig_author_name;
96 got_repo_get_gitconfig_author_email(struct got_repository *repo)
98 return repo->gitconfig_author_email;
102 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
104 return repo->global_gitconfig_author_name;
108 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
110 return repo->global_gitconfig_author_email;
114 got_repo_get_gitconfig_owner(struct got_repository *repo)
116 return repo->gitconfig_owner;
120 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
121 struct got_repository *repo)
123 *extensions = repo->extensions;
124 *nextensions = repo->nextensions;
128 got_repo_is_bare(struct got_repository *repo)
130 return (strcmp(repo->path, repo->path_git_dir) == 0);
134 get_path_git_child(struct got_repository *repo, const char *basename)
138 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
146 got_repo_get_path_objects(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_DIR);
152 got_repo_get_path_objects_pack(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
158 got_repo_get_path_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_REFS_DIR);
164 got_repo_get_path_packed_refs(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
170 get_path_head(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_HEAD_FILE);
176 got_repo_get_path_gitconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GITCONFIG);
182 got_repo_get_path_gotconfig(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
187 const struct got_gotconfig *
188 got_repo_get_gotconfig(struct got_repository *repo)
190 return repo->gotconfig;
194 got_repo_get_gitconfig_remotes(int *nremotes,
195 const struct got_remote_repo **remotes, struct got_repository *repo)
197 *nremotes = repo->ngitconfig_remotes;
198 *remotes = repo->gitconfig_remotes;
202 is_git_repo(struct got_repository *repo)
204 const char *path_git = got_repo_get_path_git_dir(repo);
205 char *path_objects = got_repo_get_path_objects(repo);
206 char *path_refs = got_repo_get_path_refs(repo);
207 char *path_head = get_path_head(repo);
210 struct got_reference *head_ref;
212 if (lstat(path_git, &sb) == -1)
214 if (!S_ISDIR(sb.st_mode))
217 if (lstat(path_objects, &sb) == -1)
219 if (!S_ISDIR(sb.st_mode))
222 if (lstat(path_refs, &sb) == -1)
224 if (!S_ISDIR(sb.st_mode))
227 if (lstat(path_head, &sb) == -1)
229 if (!S_ISREG(sb.st_mode))
232 /* Check if the HEAD reference can be opened. */
233 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
235 got_ref_close(head_ref);
246 const struct got_error *
247 got_repo_pack_fds_open(int **pack_fds)
249 const struct got_error *err = NULL;
250 int i, *pack_fds_tmp;
252 pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
253 if (pack_fds_tmp == NULL)
254 return got_error_from_errno("calloc");
255 *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
256 if (*pack_fds == NULL) {
258 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, GOT_PACK_NUM_TEMPFILES * sizeof(int));
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,
1264 strlen(dent->d_name)))
1267 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1268 dent->d_name) == -1) {
1269 err = got_error_from_errno("asprintf");
1270 path_packidx = NULL;
1274 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1281 if (packdir && closedir(packdir) != 0 && err == NULL)
1282 err = got_error_from_errno("closedir");
1286 const struct got_error *
1287 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1288 struct got_repository *repo)
1290 const struct got_error *err;
1295 /* Search pack index cache. */
1296 for (i = 0; i < repo->pack_cache_size; i++) {
1297 if (repo->packidx_cache[i] == NULL)
1299 if (strcmp(repo->packidx_cache[i]->path_packidx,
1300 path_packidx) == 0) {
1301 *packidx = repo->packidx_cache[i];
1305 /* No luck. Search the filesystem. */
1307 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1312 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1316 err = cache_packidx(repo, *packidx, path_packidx);
1319 got_packidx_close(*packidx);
1325 static const struct got_error *
1326 read_packfile_hdr(int fd, struct got_packidx *packidx)
1328 const struct got_error *err = NULL;
1329 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1330 struct got_packfile_hdr hdr;
1333 n = read(fd, &hdr, sizeof(hdr));
1335 return got_error_from_errno("read");
1336 if (n != sizeof(hdr))
1337 return got_error(GOT_ERR_BAD_PACKFILE);
1339 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1340 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1341 be32toh(hdr.nobjects) != totobj)
1342 err = got_error(GOT_ERR_BAD_PACKFILE);
1347 static const struct got_error *
1348 open_packfile(int *fd, struct got_repository *repo,
1349 const char *relpath, struct got_packidx *packidx)
1351 const struct got_error *err = NULL;
1353 *fd = openat(got_repo_get_fd(repo), relpath,
1354 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1356 return got_error_from_errno_fmt("openat: %s/%s",
1357 got_repo_get_path_git_dir(repo), relpath);
1360 err = read_packfile_hdr(*fd, packidx);
1370 const struct got_error *
1371 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1372 const char *path_packfile, struct got_packidx *packidx)
1374 const struct got_error *err = NULL;
1375 struct got_pack *pack = NULL;
1382 for (i = 0; i < repo->pack_cache_size; i++) {
1383 pack = &repo->packs[i];
1384 if (pack->path_packfile == NULL)
1386 if (strcmp(pack->path_packfile, path_packfile) == 0)
1387 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1390 if (i == repo->pack_cache_size) {
1391 struct got_pack tmp;
1394 } while (i > 0 && repo->pinned_pack >= 0 &&
1395 i == repo->pinned_pack);
1396 err = got_pack_close(&repo->packs[i]);
1399 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1400 return got_error_from_errno("ftruncate");
1401 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1402 return got_error_from_errno("ftruncate");
1403 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1404 memcpy(&repo->packs[i], &repo->packs[0],
1405 sizeof(repo->packs[i]));
1406 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1407 if (repo->pinned_pack == 0)
1408 repo->pinned_pack = i;
1409 else if (repo->pinned_pack == i)
1410 repo->pinned_pack = 0;
1414 pack = &repo->packs[i];
1416 pack->path_packfile = strdup(path_packfile);
1417 if (pack->path_packfile == NULL) {
1418 err = got_error_from_errno("strdup");
1422 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1426 if (fstat(pack->fd, &sb) != 0) {
1427 err = got_error_from_errno("fstat");
1430 pack->filesize = sb.st_size;
1432 pack->privsep_child = NULL;
1434 #ifndef GOT_PACK_NO_MMAP
1435 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1437 if (pack->map == MAP_FAILED) {
1438 if (errno != ENOMEM) {
1439 err = got_error_from_errno("mmap");
1442 pack->map = NULL; /* fall back to read(2) */
1448 free(pack->path_packfile);
1449 memset(pack, 0, sizeof(*pack));
1457 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1459 struct got_pack *pack = NULL;
1462 for (i = 0; i < repo->pack_cache_size; i++) {
1463 pack = &repo->packs[i];
1464 if (pack->path_packfile == NULL)
1466 if (strcmp(pack->path_packfile, path_packfile) == 0)
1473 const struct got_error *
1474 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1475 struct got_pack *pack)
1478 int pinned_pack = -1, pinned_packidx = -1;
1480 for (i = 0; i < repo->pack_cache_size; i++) {
1481 if (repo->packidx_cache[i] &&
1482 strcmp(repo->packidx_cache[i]->path_packidx,
1483 packidx->path_packidx) == 0)
1485 if (repo->packs[i].path_packfile &&
1486 strcmp(repo->packs[i].path_packfile,
1487 pack->path_packfile) == 0)
1491 if (pinned_packidx == -1 || pinned_pack == -1)
1492 return got_error(GOT_ERR_PIN_PACK);
1494 repo->pinned_pack = pinned_pack;
1495 repo->pinned_packidx = pinned_packidx;
1496 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1501 got_repo_get_pinned_pack(struct got_repository *repo)
1503 if (repo->pinned_pack >= 0 &&
1504 repo->pinned_pack < repo->pack_cache_size)
1505 return &repo->packs[repo->pinned_pack];
1511 got_repo_unpin_pack(struct got_repository *repo)
1513 repo->pinned_packidx = -1;
1514 repo->pinned_pack = -1;
1515 repo->pinned_pid = 0;
1518 const struct got_error *
1519 got_repo_init(const char *repo_path)
1521 const struct got_error *err = NULL;
1522 const char *dirnames[] = {
1524 GOT_OBJECTS_PACK_DIR,
1527 const char *description_str = "Unnamed repository; "
1528 "edit this file 'description' to name the repository.";
1529 const char *headref_str = "ref: refs/heads/main";
1530 const char *gitconfig_str = "[core]\n"
1531 "\trepositoryformatversion = 0\n"
1532 "\tfilemode = true\n"
1537 if (!got_path_dir_is_empty(repo_path))
1538 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1540 for (i = 0; i < nitems(dirnames); i++) {
1541 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1542 return got_error_from_errno("asprintf");
1544 err = got_path_mkdir(path);
1550 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1551 return got_error_from_errno("asprintf");
1552 err = got_path_create_file(path, description_str);
1557 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1558 return got_error_from_errno("asprintf");
1559 err = got_path_create_file(path, headref_str);
1564 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1565 return got_error_from_errno("asprintf");
1566 err = got_path_create_file(path, gitconfig_str);
1574 static const struct got_error *
1575 match_packed_object(struct got_object_id **unique_id,
1576 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1578 const struct got_error *err = NULL;
1579 struct got_object_id_queue matched_ids;
1580 struct got_pathlist_entry *pe;
1582 STAILQ_INIT(&matched_ids);
1584 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1585 const char *path_packidx = pe->path;
1586 struct got_packidx *packidx;
1587 struct got_object_qid *qid;
1589 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1594 err = got_packidx_match_id_str_prefix(&matched_ids,
1595 packidx, id_str_prefix);
1597 got_packidx_close(packidx);
1600 err = got_packidx_close(packidx);
1604 STAILQ_FOREACH(qid, &matched_ids, entry) {
1605 if (obj_type != GOT_OBJ_TYPE_ANY) {
1607 err = got_object_get_type(&matched_type, repo,
1611 if (matched_type != obj_type)
1614 if (*unique_id == NULL) {
1615 *unique_id = got_object_id_dup(&qid->id);
1616 if (*unique_id == NULL) {
1617 err = got_error_from_errno("malloc");
1621 if (got_object_id_cmp(*unique_id,
1623 continue; /* packed multiple times */
1624 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1630 got_object_id_queue_free(&matched_ids);
1638 static const struct got_error *
1639 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1640 const char *object_dir, const char *id_str_prefix, int obj_type,
1641 struct got_repository *repo)
1643 const struct got_error *err = NULL;
1646 struct dirent *dent;
1647 struct got_object_id id;
1649 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1650 err = got_error_from_errno("asprintf");
1654 dir = opendir(path);
1656 if (errno == ENOENT) {
1660 err = got_error_from_errno2("opendir", path);
1663 while ((dent = readdir(dir)) != NULL) {
1667 if (strcmp(dent->d_name, ".") == 0 ||
1668 strcmp(dent->d_name, "..") == 0)
1671 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1672 err = got_error_from_errno("asprintf");
1676 if (!got_parse_sha1_digest(id.sha1, id_str))
1680 * Directory entries do not necessarily appear in
1681 * sorted order, so we must iterate over all of them.
1683 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1689 if (*unique_id == NULL) {
1690 if (obj_type != GOT_OBJ_TYPE_ANY) {
1692 err = got_object_get_type(&matched_type, repo,
1696 if (matched_type != obj_type)
1699 *unique_id = got_object_id_dup(&id);
1700 if (*unique_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1706 if (got_object_id_cmp(*unique_id, &id) == 0)
1707 continue; /* both packed and loose */
1708 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1714 if (dir && closedir(dir) != 0 && err == NULL)
1715 err = got_error_from_errno("closedir");
1724 const struct got_error *
1725 got_repo_match_object_id_prefix(struct got_object_id **id,
1726 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1728 const struct got_error *err = NULL;
1729 char *path_objects = got_repo_get_path_objects(repo);
1730 char *object_dir = NULL;
1736 len = strlen(id_str_prefix);
1737 if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1738 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1740 for (i = 0; i < len; i++) {
1741 if (isxdigit((unsigned char)id_str_prefix[i]))
1743 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1747 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1750 object_dir = strndup(id_str_prefix, 2);
1751 if (object_dir == NULL) {
1752 err = got_error_from_errno("strdup");
1755 err = match_loose_object(id, path_objects, object_dir,
1756 id_str_prefix, obj_type, repo);
1757 } else if (len == 1) {
1759 for (i = 0; i < 0xf; i++) {
1760 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1762 err = got_error_from_errno("asprintf");
1765 err = match_packed_object(id, repo, object_dir,
1769 err = match_loose_object(id, path_objects, object_dir,
1770 id_str_prefix, obj_type, repo);
1775 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1783 } else if (*id == NULL) {
1785 case GOT_OBJ_TYPE_BLOB:
1786 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1787 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1789 case GOT_OBJ_TYPE_TREE:
1790 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1791 GOT_OBJ_LABEL_TREE, id_str_prefix);
1793 case GOT_OBJ_TYPE_COMMIT:
1794 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1795 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1797 case GOT_OBJ_TYPE_TAG:
1798 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1799 GOT_OBJ_LABEL_TAG, id_str_prefix);
1802 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1810 const struct got_error *
1811 got_repo_match_object_id(struct got_object_id **id, char **label,
1812 const char *id_str, int obj_type, struct got_reflist_head *refs,
1813 struct got_repository *repo)
1815 const struct got_error *err;
1816 struct got_tag_object *tag;
1817 struct got_reference *ref = NULL;
1824 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1827 *id = got_object_id_dup(
1828 got_object_tag_get_object_id(tag));
1830 err = got_error_from_errno("got_object_id_dup");
1831 else if (label && asprintf(label, "refs/tags/%s",
1832 got_object_tag_get_name(tag)) == -1) {
1833 err = got_error_from_errno("asprintf");
1837 got_object_tag_close(tag);
1839 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1840 err->code != GOT_ERR_NO_OBJ)
1844 err = got_ref_open(&ref, repo, id_str, 0);
1846 err = got_ref_resolve(id, repo, ref);
1850 *label = strdup(got_ref_get_name(ref));
1851 if (*label == NULL) {
1852 err = got_error_from_errno("strdup");
1857 if (err->code != GOT_ERR_NOT_REF &&
1858 err->code != GOT_ERR_BAD_REF_NAME)
1860 err = got_repo_match_object_id_prefix(id, id_str,
1863 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1864 err = got_error_not_ref(id_str);
1868 err = got_object_id_str(label, *id);
1869 if (*label == NULL) {
1870 err = got_error_from_errno("strdup");
1881 const struct got_error *
1882 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1883 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1885 const struct got_error *err = NULL;
1886 struct got_reflist_entry *re;
1887 struct got_object_id *tag_id;
1888 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1892 TAILQ_FOREACH(re, refs, entry) {
1893 const char *refname;
1894 refname = got_ref_get_name(re->ref);
1895 if (got_ref_is_symbolic(re->ref))
1897 if (strncmp(refname, "refs/tags/", 10) != 0)
1899 if (!name_is_absolute)
1900 refname += strlen("refs/tags/");
1901 if (strcmp(refname, name) != 0)
1903 err = got_ref_resolve(&tag_id, repo, re->ref);
1906 err = got_object_open_as_tag(tag, repo, tag_id);
1910 if (obj_type == GOT_OBJ_TYPE_ANY ||
1911 got_object_tag_get_object_type(*tag) == obj_type)
1913 got_object_tag_close(*tag);
1917 if (err == NULL && *tag == NULL)
1918 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1919 GOT_OBJ_LABEL_TAG, name);
1923 static const struct got_error *
1924 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1925 const char *name, mode_t mode, struct got_object_id *blob_id)
1927 const struct got_error *err = NULL;
1931 *new_te = calloc(1, sizeof(**new_te));
1932 if (*new_te == NULL)
1933 return got_error_from_errno("calloc");
1935 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1936 sizeof((*new_te)->name)) {
1937 err = got_error(GOT_ERR_NO_SPACE);
1941 if (S_ISLNK(mode)) {
1942 (*new_te)->mode = S_IFLNK;
1944 (*new_te)->mode = S_IFREG;
1945 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1947 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1949 if (err && *new_te) {
1956 static const struct got_error *
1957 import_file(struct got_tree_entry **new_te, struct dirent *de,
1958 const char *path, struct got_repository *repo)
1960 const struct got_error *err;
1961 struct got_object_id *blob_id = NULL;
1965 if (asprintf(&filepath, "%s%s%s", path,
1966 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1967 return got_error_from_errno("asprintf");
1969 if (lstat(filepath, &sb) != 0) {
1970 err = got_error_from_errno2("lstat", path);
1974 err = got_object_blob_create(&blob_id, filepath, repo);
1978 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1987 static const struct got_error *
1988 insert_tree_entry(struct got_tree_entry *new_te,
1989 struct got_pathlist_head *paths)
1991 const struct got_error *err = NULL;
1992 struct got_pathlist_entry *new_pe;
1994 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1998 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2002 static const struct got_error *write_tree(struct got_object_id **,
2003 const char *, struct got_pathlist_head *, struct got_repository *,
2004 got_repo_import_cb progress_cb, void *progress_arg);
2006 static const struct got_error *
2007 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2008 const char *path, struct got_pathlist_head *ignores,
2009 struct got_repository *repo,
2010 got_repo_import_cb progress_cb, void *progress_arg)
2012 const struct got_error *err;
2013 struct got_object_id *id = NULL;
2016 if (asprintf(&subdirpath, "%s%s%s", path,
2017 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2018 return got_error_from_errno("asprintf");
2020 (*new_te) = calloc(1, sizeof(**new_te));
2021 if (*new_te == NULL)
2022 return got_error_from_errno("calloc");
2023 (*new_te)->mode = S_IFDIR;
2024 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2025 sizeof((*new_te)->name)) {
2026 err = got_error(GOT_ERR_NO_SPACE);
2029 err = write_tree(&id, subdirpath, ignores, repo,
2030 progress_cb, progress_arg);
2033 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2045 static const struct got_error *
2046 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2047 struct got_pathlist_head *ignores, struct got_repository *repo,
2048 got_repo_import_cb progress_cb, void *progress_arg)
2050 const struct got_error *err = NULL;
2054 struct got_tree_entry *new_te = NULL;
2055 struct got_pathlist_head paths;
2056 struct got_pathlist_entry *pe;
2058 *new_tree_id = NULL;
2062 dir = opendir(path_dir);
2064 err = got_error_from_errno2("opendir", path_dir);
2069 while ((de = readdir(dir)) != NULL) {
2073 if (strcmp(de->d_name, ".") == 0 ||
2074 strcmp(de->d_name, "..") == 0)
2077 TAILQ_FOREACH(pe, ignores, entry) {
2078 if (fnmatch(pe->path, de->d_name, 0) == 0) {
2086 err = got_path_dirent_type(&type, path_dir, de);
2090 if (type == DT_DIR) {
2091 err = import_subdir(&new_te, de, path_dir,
2092 ignores, repo, progress_cb, progress_arg);
2094 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2099 } else if (type == DT_REG || type == DT_LNK) {
2100 err = import_file(&new_te, de, path_dir, repo);
2106 err = insert_tree_entry(new_te, &paths);
2112 if (TAILQ_EMPTY(&paths)) {
2113 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2114 "cannot create tree without any entries");
2118 TAILQ_FOREACH(pe, &paths, entry) {
2119 struct got_tree_entry *te = pe->data;
2121 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2123 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2124 err = got_error_from_errno("asprintf");
2127 err = (*progress_cb)(progress_arg, path);
2133 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2137 got_pathlist_free(&paths);
2141 const struct got_error *
2142 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2143 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2144 struct got_repository *repo, got_repo_import_cb progress_cb,
2147 const struct got_error *err;
2148 struct got_object_id *new_tree_id;
2150 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2151 progress_cb, progress_arg);
2155 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2156 author, time(NULL), author, time(NULL), logmsg, repo);
2161 const struct got_error *
2162 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2163 struct got_repository *repo)
2165 const struct got_error *err = NULL;
2166 char *path_objects = NULL, *path = NULL;
2168 struct got_object_id id;
2174 path_objects = got_repo_get_path_objects(repo);
2175 if (path_objects == NULL)
2176 return got_error_from_errno("got_repo_get_path_objects");
2178 for (i = 0; i <= 0xff; i++) {
2179 struct dirent *dent;
2181 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2182 err = got_error_from_errno("asprintf");
2186 dir = opendir(path);
2188 if (errno == ENOENT) {
2192 err = got_error_from_errno2("opendir", path);
2196 while ((dent = readdir(dir)) != NULL) {
2201 if (strcmp(dent->d_name, ".") == 0 ||
2202 strcmp(dent->d_name, "..") == 0)
2205 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2206 err = got_error_from_errno("asprintf");
2210 if (!got_parse_sha1_digest(id.sha1, id_str)) {
2216 err = got_object_open_loose_fd(&fd, &id, repo);
2220 if (fstat(fd, &sb) == -1) {
2221 err = got_error_from_errno("fstat");
2226 (*ondisk_size) += sb.st_size;
2228 if (close(fd) == -1) {
2229 err = got_error_from_errno("close");
2234 if (closedir(dir) != 0) {
2235 err = got_error_from_errno("closedir");
2244 if (dir && closedir(dir) != 0 && err == NULL)
2245 err = got_error_from_errno("closedir");
2256 const struct got_error *
2257 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2258 off_t *total_packsize, struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 DIR *packdir = NULL;
2262 struct dirent *dent;
2263 struct got_packidx *packidx = NULL;
2265 char *path_packfile;
2271 *total_packsize = 0;
2273 packdir_fd = openat(got_repo_get_fd(repo),
2274 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2275 if (packdir_fd == -1) {
2276 return got_error_from_errno_fmt("openat: %s/%s",
2277 got_repo_get_path_git_dir(repo),
2278 GOT_OBJECTS_PACK_DIR);
2281 packdir = fdopendir(packdir_fd);
2282 if (packdir == NULL) {
2283 err = got_error_from_errno("fdopendir");
2287 while ((dent = readdir(packdir)) != NULL) {
2288 if (!got_repo_is_packidx_filename(dent->d_name,
2289 strlen(dent->d_name)))
2292 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2293 dent->d_name) == -1) {
2294 err = got_error_from_errno("asprintf");
2298 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2304 if (fstat(packidx->fd, &sb) == -1)
2306 *total_packsize += sb.st_size;
2308 err = got_packidx_get_packfile_path(&path_packfile,
2309 packidx->path_packidx);
2313 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2315 free(path_packfile);
2318 free(path_packfile);
2319 *total_packsize += sb.st_size;
2321 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2325 got_packidx_close(packidx);
2330 got_packidx_close(packidx);
2331 if (packdir && closedir(packdir) != 0 && err == NULL)
2332 err = got_error_from_errno("closedir");
2336 *total_packsize = 0;
2341 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2342 got_packidx_bloom_filter_cmp);