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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
22 #include <sys/socket.h>
25 #include <sys/resource.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_delta_cache.h"
54 #include "got_lib_inflate.h"
55 #include "got_lib_object.h"
56 #include "got_lib_object_parse.h"
57 #include "got_lib_object_create.h"
58 #include "got_lib_pack.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_hash.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
63 #include "got_lib_gotconfig.h"
66 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
71 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
72 got_packidx_bloom_filter_cmp);
75 is_boolean_val(const char *val)
77 return (strcasecmp(val, "true") == 0 ||
78 strcasecmp(val, "false") == 0 ||
79 strcasecmp(val, "on") == 0 ||
80 strcasecmp(val, "off") == 0 ||
81 strcasecmp(val, "yes") == 0 ||
82 strcasecmp(val, "no") == 0 ||
83 strcasecmp(val, "1") == 0 ||
84 strcasecmp(val, "0") == 0);
88 get_boolean_val(const char *val)
90 return (strcasecmp(val, "true") == 0 ||
91 strcasecmp(val, "on") == 0 ||
92 strcasecmp(val, "yes") == 0 ||
93 strcasecmp(val, "1") == 0);
97 got_repo_get_path(struct got_repository *repo)
103 got_repo_get_path_git_dir(struct got_repository *repo)
105 return repo->path_git_dir;
109 got_repo_get_fd(struct got_repository *repo)
111 return repo->gitdir_fd;
114 enum got_hash_algorithm
115 got_repo_get_object_format(struct got_repository *repo)
121 got_repo_get_gitconfig_author_name(struct got_repository *repo)
123 return repo->gitconfig_author_name;
127 got_repo_get_gitconfig_author_email(struct got_repository *repo)
129 return repo->gitconfig_author_email;
133 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
135 return repo->global_gitconfig_author_name;
139 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
141 return repo->global_gitconfig_author_email;
145 got_repo_get_gitconfig_owner(struct got_repository *repo)
147 return repo->gitconfig_owner;
151 got_repo_has_extension(struct got_repository *repo, const char *ext)
155 for (i = 0; i < repo->nextensions; ++i) {
156 if (!strcasecmp(ext, repo->extnames[i]))
157 return get_boolean_val(repo->extvals[i]);
164 got_repo_is_bare(struct got_repository *repo)
166 return (strcmp(repo->path, repo->path_git_dir) == 0);
170 get_path_git_child(struct got_repository *repo, const char *basename)
174 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
182 got_repo_get_path_objects(struct got_repository *repo)
184 return get_path_git_child(repo, GOT_OBJECTS_DIR);
188 got_repo_get_path_objects_pack(struct got_repository *repo)
190 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
194 got_repo_get_path_refs(struct got_repository *repo)
196 return get_path_git_child(repo, GOT_REFS_DIR);
200 got_repo_get_path_packed_refs(struct got_repository *repo)
202 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
206 get_path_head(struct got_repository *repo)
208 return get_path_git_child(repo, GOT_HEAD_FILE);
212 got_repo_get_path_gitconfig(struct got_repository *repo)
214 return get_path_git_child(repo, GOT_GITCONFIG);
218 got_repo_get_path_gotconfig(struct got_repository *repo)
220 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
223 const struct got_gotconfig *
224 got_repo_get_gotconfig(struct got_repository *repo)
226 return repo->gotconfig;
230 got_repo_get_gitconfig_remotes(int *nremotes,
231 const struct got_remote_repo **remotes, struct got_repository *repo)
233 *nremotes = repo->ngitconfig_remotes;
234 *remotes = repo->gitconfig_remotes;
238 is_git_repo(struct got_repository *repo)
240 const char *path_git = got_repo_get_path_git_dir(repo);
241 char *path_objects = got_repo_get_path_objects(repo);
242 char *path_refs = got_repo_get_path_refs(repo);
243 char *path_head = get_path_head(repo);
246 struct got_reference *head_ref;
248 if (lstat(path_git, &sb) == -1)
250 if (!S_ISDIR(sb.st_mode))
253 if (lstat(path_objects, &sb) == -1)
255 if (!S_ISDIR(sb.st_mode))
258 if (lstat(path_refs, &sb) == -1)
260 if (!S_ISDIR(sb.st_mode))
263 if (lstat(path_head, &sb) == -1)
265 if (!S_ISREG(sb.st_mode))
268 /* Check if the HEAD reference can be opened. */
269 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
271 got_ref_close(head_ref);
282 static const struct got_error *
283 close_tempfiles(int *fds, size_t nfds)
285 const struct got_error *err = NULL;
288 for (i = 0; i < nfds; i++) {
291 if (close(fds[i]) == -1) {
292 err = got_error_from_errno("close");
300 static const struct got_error *
301 open_tempfiles(int **fds, size_t array_size, size_t nfds)
303 const struct got_error *err = NULL;
306 *fds = calloc(array_size, sizeof(**fds));
308 return got_error_from_errno("calloc");
310 for (i = 0; i < array_size; i++)
313 for (i = 0; i < nfds; i++) {
314 (*fds)[i] = got_opentempfd();
315 if ((*fds)[i] == -1) {
316 err = got_error_from_errno("got_opentempfd");
317 close_tempfiles(*fds, nfds);
326 static const struct got_error *
327 get_pack_cache_size(int *pack_cache_size)
331 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
332 return got_error_from_errno("getrlimit");
334 *pack_cache_size = GOT_PACK_CACHE_SIZE;
335 if (*pack_cache_size > rl.rlim_cur / 8)
336 *pack_cache_size = rl.rlim_cur / 8;
341 const struct got_error *
342 got_repo_pack_fds_open(int **pack_fds)
344 const struct got_error *err;
347 err = get_pack_cache_size(&nfds);
352 * We need one basefd and one accumfd per cached pack.
353 * Our constants should be set up in a way such that
354 * this error never triggers.
356 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
357 return got_error(GOT_ERR_NO_SPACE);
359 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
362 const struct got_error *
363 got_repo_pack_fds_close(int *pack_fds)
365 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
368 const struct got_error *
369 got_repo_temp_fds_open(int **temp_fds)
371 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
372 GOT_REPO_NUM_TEMPFILES);
376 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
380 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
381 repo->tempfiles[i] = temp_fds[i];
384 const struct got_error *
385 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
392 for (i = 0; i < nitems(repo->tempfiles); i++) {
393 if (repo->tempfile_use_mask & (1 << i))
395 if (repo->tempfiles[i] != -1) {
396 if (ftruncate(repo->tempfiles[i], 0L) == -1)
397 return got_error_from_errno("ftruncate");
398 *fd = repo->tempfiles[i];
400 repo->tempfile_use_mask |= (1 << i);
405 return got_error(GOT_ERR_REPO_TEMPFILE);
409 got_repo_temp_fds_put(int idx, struct got_repository *repo)
411 repo->tempfile_use_mask &= ~(1 << idx);
414 const struct got_error *
415 got_repo_temp_fds_close(int *temp_fds)
417 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
420 const struct got_error *
421 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
422 struct got_object *obj)
424 #ifndef GOT_NO_OBJ_CACHE
425 const struct got_error *err = NULL;
426 err = got_object_cache_add(&repo->objcache, id, obj);
428 if (err->code == GOT_ERR_OBJ_EXISTS ||
429 err->code == GOT_ERR_OBJ_TOO_LARGE)
439 got_repo_get_cached_object(struct got_repository *repo,
440 struct got_object_id *id)
442 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
445 const struct got_error *
446 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
447 struct got_tree_object *tree)
449 #ifndef GOT_NO_OBJ_CACHE
450 const struct got_error *err = NULL;
451 err = got_object_cache_add(&repo->treecache, id, tree);
453 if (err->code == GOT_ERR_OBJ_EXISTS ||
454 err->code == GOT_ERR_OBJ_TOO_LARGE)
463 struct got_tree_object *
464 got_repo_get_cached_tree(struct got_repository *repo,
465 struct got_object_id *id)
467 return (struct got_tree_object *)got_object_cache_get(
468 &repo->treecache, id);
471 const struct got_error *
472 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
473 struct got_commit_object *commit)
475 #ifndef GOT_NO_OBJ_CACHE
476 const struct got_error *err = NULL;
477 err = got_object_cache_add(&repo->commitcache, id, commit);
479 if (err->code == GOT_ERR_OBJ_EXISTS ||
480 err->code == GOT_ERR_OBJ_TOO_LARGE)
489 struct got_commit_object *
490 got_repo_get_cached_commit(struct got_repository *repo,
491 struct got_object_id *id)
493 return (struct got_commit_object *)got_object_cache_get(
494 &repo->commitcache, id);
497 const struct got_error *
498 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
499 struct got_tag_object *tag)
501 #ifndef GOT_NO_OBJ_CACHE
502 const struct got_error *err = NULL;
503 err = got_object_cache_add(&repo->tagcache, id, tag);
505 if (err->code == GOT_ERR_OBJ_EXISTS ||
506 err->code == GOT_ERR_OBJ_TOO_LARGE)
515 struct got_tag_object *
516 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
518 return (struct got_tag_object *)got_object_cache_get(
519 &repo->tagcache, id);
522 const struct got_error *
523 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
524 struct got_raw_object *raw)
526 #ifndef GOT_NO_OBJ_CACHE
527 const struct got_error *err = NULL;
528 err = got_object_cache_add(&repo->rawcache, id, raw);
530 if (err->code == GOT_ERR_OBJ_EXISTS ||
531 err->code == GOT_ERR_OBJ_TOO_LARGE)
541 struct got_raw_object *
542 got_repo_get_cached_raw_object(struct got_repository *repo,
543 struct got_object_id *id)
545 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
549 static const struct got_error *
550 open_repo(struct got_repository *repo, const char *path)
552 const struct got_error *err = NULL;
554 repo->gitdir_fd = -1;
556 /* bare git repository? */
557 repo->path_git_dir = strdup(path);
558 if (repo->path_git_dir == NULL)
559 return got_error_from_errno("strdup");
560 if (is_git_repo(repo)) {
561 repo->path = strdup(repo->path_git_dir);
562 if (repo->path == NULL) {
563 err = got_error_from_errno("strdup");
566 repo->gitdir_fd = open(repo->path_git_dir,
567 O_DIRECTORY | O_CLOEXEC);
568 if (repo->gitdir_fd == -1) {
569 err = got_error_from_errno2("open",
576 /* git repository with working tree? */
577 free(repo->path_git_dir);
578 repo->path_git_dir = NULL;
579 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
580 err = got_error_from_errno("asprintf");
583 if (is_git_repo(repo)) {
584 repo->path = strdup(path);
585 if (repo->path == NULL) {
586 err = got_error_from_errno("strdup");
589 repo->gitdir_fd = open(repo->path_git_dir,
590 O_DIRECTORY | O_CLOEXEC);
591 if (repo->gitdir_fd == -1) {
592 err = got_error_from_errno2("open",
599 err = got_error(GOT_ERR_NOT_GIT_REPO);
604 free(repo->path_git_dir);
605 repo->path_git_dir = NULL;
606 if (repo->gitdir_fd != -1)
607 close(repo->gitdir_fd);
608 repo->gitdir_fd = -1;
614 static const struct got_error *
615 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
617 const struct got_error *err = NULL;
618 char *repo_gitconfig_path = NULL;
620 if (global_gitconfig_path) {
621 /* Read settings from ~/.gitconfig. */
622 int dummy_repo_version;
623 err = got_repo_read_gitconfig(&dummy_repo_version,
624 &repo->global_gitconfig_author_name,
625 &repo->global_gitconfig_author_email,
626 NULL, NULL, NULL, NULL, NULL, NULL,
627 global_gitconfig_path);
632 /* Read repository's .git/config file. */
633 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
634 if (repo_gitconfig_path == NULL)
635 return got_error_from_errno("got_repo_get_path_gitconfig");
637 err = got_repo_read_gitconfig(
638 &repo->gitconfig_repository_format_version,
639 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
640 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
641 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
642 &repo->nextensions, repo_gitconfig_path);
646 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
649 for (i = 0; i < repo->ngitconfig_remotes; i++) {
650 got_repo_free_remote_repo_data(
651 &repo->gitconfig_remotes[i]);
653 free(repo->gitconfig_remotes);
654 repo->gitconfig_remotes = NULL;
655 repo->ngitconfig_remotes = 0;
657 free(repo->gitconfig_author_name);
658 repo->gitconfig_author_name = NULL;
659 free(repo->gitconfig_author_email);
660 repo->gitconfig_author_email = NULL;
662 free(repo->global_gitconfig_author_name);
663 repo->global_gitconfig_author_name = NULL;
664 free(repo->global_gitconfig_author_email);
665 repo->global_gitconfig_author_email = NULL;
669 free(repo_gitconfig_path);
673 static const struct got_error *
674 read_gotconfig(struct got_repository *repo)
676 const struct got_error *err = NULL;
677 char *gotconfig_path;
679 gotconfig_path = got_repo_get_path_gotconfig(repo);
680 if (gotconfig_path == NULL)
681 return got_error_from_errno("got_repo_get_path_gotconfig");
683 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
684 free(gotconfig_path);
688 /* Supported repository format extensions. */
689 static const char *const repo_extensions[] = {
690 "noop", /* Got supports repository format version 1. */
691 "preciousObjects", /* Supported by gotadmin cleanup. */
692 "worktreeConfig", /* Got does not care about Git work trees. */
695 const struct got_error *
696 got_repo_open(struct got_repository **repop, const char *path,
697 const char *global_gitconfig_path, int *pack_fds)
699 struct got_repository *repo = NULL;
700 const struct got_error *err = NULL;
701 char *repo_path = NULL;
706 repo = calloc(1, sizeof(*repo));
708 return got_error_from_errno("calloc");
710 RB_INIT(&repo->packidx_bloom_filters);
711 TAILQ_INIT(&repo->packidx_paths);
713 for (i = 0; i < nitems(repo->privsep_children); i++) {
714 memset(&repo->privsep_children[i], 0,
715 sizeof(repo->privsep_children[0]));
716 repo->privsep_children[i].imsg_fd = -1;
719 err = got_object_cache_init(&repo->objcache,
720 GOT_OBJECT_CACHE_TYPE_OBJ);
723 err = got_object_cache_init(&repo->treecache,
724 GOT_OBJECT_CACHE_TYPE_TREE);
727 err = got_object_cache_init(&repo->commitcache,
728 GOT_OBJECT_CACHE_TYPE_COMMIT);
731 err = got_object_cache_init(&repo->tagcache,
732 GOT_OBJECT_CACHE_TYPE_TAG);
735 err = got_object_cache_init(&repo->rawcache,
736 GOT_OBJECT_CACHE_TYPE_RAW);
740 err = get_pack_cache_size(&repo->pack_cache_size);
743 for (i = 0; i < nitems(repo->packs); i++) {
744 if (pack_fds != NULL && i < repo->pack_cache_size) {
745 repo->packs[i].basefd = pack_fds[j++];
746 repo->packs[i].accumfd = pack_fds[j++];
748 repo->packs[i].basefd = -1;
749 repo->packs[i].accumfd = -1;
752 for (i = 0; i < nitems(repo->tempfiles); i++)
753 repo->tempfiles[i] = -1;
754 repo->pinned_pack = -1;
755 repo->pinned_packidx = -1;
756 repo->pinned_pid = 0;
758 repo_path = realpath(path, NULL);
759 if (repo_path == NULL) {
760 err = got_error_from_errno2("realpath", path);
767 err = open_repo(repo, repo_path);
770 if (err->code != GOT_ERR_NOT_GIT_REPO)
772 if (repo_path[0] == '/' && repo_path[1] == '\0') {
773 err = got_error(GOT_ERR_NOT_GIT_REPO);
776 err = got_path_dirname(&parent_path, repo_path);
780 repo_path = parent_path;
783 err = read_gotconfig(repo);
787 err = read_gitconfig(repo, global_gitconfig_path);
790 if (repo->gitconfig_repository_format_version != 0) {
791 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
794 for (i = 0; i < repo->nextensions; i++) {
795 char *ext = repo->extnames[i];
796 char *val = repo->extvals[i];
797 int j, supported = 0;
799 if (!is_boolean_val(val)) {
800 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
804 if (!get_boolean_val(val))
807 for (j = 0; j < nitems(repo_extensions); j++) {
808 if (strcmp(ext, repo_extensions[j]) == 0) {
814 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
819 err = got_repo_list_packidx(&repo->packidx_paths, repo);
822 got_repo_close(repo);
829 const struct got_error *
830 got_repo_close(struct got_repository *repo)
832 const struct got_error *err = NULL, *child_err;
833 struct got_packidx_bloom_filter *bf;
836 for (i = 0; i < repo->pack_cache_size; i++) {
837 if (repo->packidx_cache[i] == NULL)
839 got_packidx_close(repo->packidx_cache[i]);
842 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
843 &repo->packidx_bloom_filters))) {
844 RB_REMOVE(got_packidx_bloom_filter_tree,
845 &repo->packidx_bloom_filters, bf);
846 bloom_free(bf->bloom);
851 for (i = 0; i < repo->pack_cache_size; i++)
852 if (repo->packs[i].path_packfile)
853 if (repo->packs[i].path_packfile)
854 got_pack_close(&repo->packs[i]);
857 free(repo->path_git_dir);
859 got_object_cache_close(&repo->objcache);
860 got_object_cache_close(&repo->treecache);
861 got_object_cache_close(&repo->commitcache);
862 got_object_cache_close(&repo->tagcache);
863 got_object_cache_close(&repo->rawcache);
865 for (i = 0; i < nitems(repo->privsep_children); i++) {
866 if (repo->privsep_children[i].imsg_fd == -1)
868 imsg_clear(repo->privsep_children[i].ibuf);
869 free(repo->privsep_children[i].ibuf);
870 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
871 if (err && err->code == GOT_ERR_EOF)
873 child_err = got_privsep_wait_for_child(
874 repo->privsep_children[i].pid);
875 if (child_err && err == NULL)
877 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
879 err = got_error_from_errno("close");
882 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
884 err = got_error_from_errno("close");
887 got_gotconfig_free(repo->gotconfig);
888 free(repo->gitconfig_author_name);
889 free(repo->gitconfig_author_email);
890 for (i = 0; i < repo->ngitconfig_remotes; i++)
891 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
892 free(repo->gitconfig_remotes);
893 for (i = 0; i < repo->nextensions; i++) {
894 free(repo->extnames[i]);
895 free(repo->extvals[i]);
897 free(repo->extnames);
900 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
906 const struct got_error *
907 got_repo_remote_repo_dup(struct got_remote_repo **newp,
908 const struct got_remote_repo *repo)
910 const struct got_error *err = NULL;
911 struct got_remote_repo *new;
914 new = calloc(1, sizeof(*new));
916 return got_error_from_errno("calloc");
919 new->name = strdup(repo->name);
920 if (new->name == NULL) {
921 err = got_error_from_errno("strdup");
926 if (repo->fetch_url) {
927 new->fetch_url = strdup(repo->fetch_url);
928 if (new->fetch_url == NULL) {
929 err = got_error_from_errno("strdup");
934 if (repo->send_url) {
935 new->send_url = strdup(repo->send_url);
936 if (new->send_url == NULL) {
937 err = got_error_from_errno("strdup");
942 new->mirror_references = repo->mirror_references;
944 new->nfetch_branches = repo->nfetch_branches;
945 if (repo->fetch_branches) {
946 new->fetch_branches = calloc(repo->nfetch_branches,
948 if (new->fetch_branches == NULL) {
949 err = got_error_from_errno("calloc");
952 for (i = 0; i < repo->nfetch_branches; i++) {
953 new->fetch_branches[i] = strdup(
954 repo->fetch_branches[i]);
955 if (new->fetch_branches[i] == NULL) {
956 err = got_error_from_errno("strdup");
962 new->nsend_branches = repo->nsend_branches;
963 if (repo->send_branches) {
964 new->send_branches = calloc(repo->nsend_branches,
966 if (new->send_branches == NULL) {
967 err = got_error_from_errno("calloc");
970 for (i = 0; i < repo->nsend_branches; i++) {
971 new->send_branches[i] = strdup(
972 repo->send_branches[i]);
973 if (new->send_branches[i] == NULL) {
974 err = got_error_from_errno("strdup");
980 new->nfetch_refs = repo->nfetch_refs;
981 if (repo->fetch_refs) {
982 new->fetch_refs = calloc(repo->nfetch_refs,
984 if (new->fetch_refs == NULL) {
985 err = got_error_from_errno("calloc");
988 for (i = 0; i < repo->nfetch_refs; i++) {
989 new->fetch_refs[i] = strdup(
990 repo->fetch_refs[i]);
991 if (new->fetch_refs[i] == NULL) {
992 err = got_error_from_errno("strdup");
999 got_repo_free_remote_repo_data(new);
1008 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
1017 free(repo->fetch_url);
1018 repo->fetch_url = NULL;
1019 free(repo->send_url);
1020 repo->send_url = NULL;
1021 for (i = 0; i < repo->nfetch_branches; i++)
1022 free(repo->fetch_branches[i]);
1023 free(repo->fetch_branches);
1024 repo->fetch_branches = NULL;
1025 repo->nfetch_branches = 0;
1026 for (i = 0; i < repo->nsend_branches; i++)
1027 free(repo->send_branches[i]);
1028 free(repo->send_branches);
1029 repo->send_branches = NULL;
1030 repo->nsend_branches = 0;
1031 for (i = 0; i < repo->nfetch_refs; i++)
1032 free(repo->fetch_refs[i]);
1033 free(repo->fetch_refs);
1034 repo->fetch_refs = NULL;
1035 repo->nfetch_refs = 0;
1038 const struct got_error *
1039 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
1040 const char *input_path)
1042 const struct got_error *err = NULL;
1043 const char *repo_abspath = NULL;
1044 size_t repolen, len;
1045 char *canonpath, *path = NULL;
1047 *in_repo_path = NULL;
1049 canonpath = strdup(input_path);
1050 if (canonpath == NULL) {
1051 err = got_error_from_errno("strdup");
1054 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
1058 repo_abspath = got_repo_get_path(repo);
1060 if (canonpath[0] == '\0') {
1061 path = strdup(canonpath);
1063 err = got_error_from_errno("strdup");
1067 path = realpath(canonpath, NULL);
1069 if (errno != ENOENT) {
1070 err = got_error_from_errno2("realpath",
1075 * Path is not on disk.
1076 * Assume it is already relative to repository root.
1078 path = strdup(canonpath);
1080 err = got_error_from_errno("strdup");
1085 repolen = strlen(repo_abspath);
1089 if (strcmp(path, repo_abspath) == 0) {
1093 err = got_error_from_errno("strdup");
1096 } else if (len > repolen &&
1097 got_path_is_child(path, repo_abspath, repolen)) {
1098 /* Matched an on-disk path inside repository. */
1099 if (got_repo_is_bare(repo)) {
1101 * Matched an on-disk path inside repository
1102 * database. Treat input as repository-relative.
1109 /* Strip common prefix with repository path. */
1110 err = got_path_skip_common_ancestor(&child,
1111 repo_abspath, path);
1119 * Matched unrelated on-disk path.
1120 * Treat input as repository-relative.
1128 /* Make in-repository path absolute */
1129 if (path[0] != '/') {
1131 if (asprintf(&abspath, "/%s", path) == -1) {
1132 err = got_error_from_errno("asprintf");
1144 *in_repo_path = path;
1148 static const struct got_error *
1149 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1150 const char *path_packidx)
1152 const struct got_error *err = NULL;
1155 for (i = 0; i < repo->pack_cache_size; i++) {
1156 if (repo->packidx_cache[i] == NULL)
1158 if (strcmp(repo->packidx_cache[i]->path_packidx,
1159 path_packidx) == 0) {
1160 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1163 if (i == repo->pack_cache_size) {
1166 } while (i > 0 && repo->pinned_packidx >= 0 &&
1167 i == repo->pinned_packidx);
1168 err = got_packidx_close(repo->packidx_cache[i]);
1173 repo->packidx_cache[i] = packidx;
1179 got_repo_is_packidx_filename(const char *name, size_t len)
1181 if (len != GOT_PACKIDX_NAMELEN)
1184 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1187 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1188 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1194 static struct got_packidx_bloom_filter *
1195 get_packidx_bloom_filter(struct got_repository *repo,
1196 const char *path, size_t path_len)
1198 struct got_packidx_bloom_filter key;
1200 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1201 return NULL; /* XXX */
1202 key.path_len = path_len;
1204 return RB_FIND(got_packidx_bloom_filter_tree,
1205 &repo->packidx_bloom_filters, &key);
1209 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1210 const char *path_packidx, struct got_object_id *id)
1212 struct got_packidx_bloom_filter *bf;
1214 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1216 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1218 /* No bloom filter means this pack index must be searched. */
1222 static const struct got_error *
1223 add_packidx_bloom_filter(struct got_repository *repo,
1224 struct got_packidx *packidx, const char *path_packidx)
1226 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1227 struct got_packidx_bloom_filter *bf;
1231 * Don't use bloom filters for very large pack index files.
1232 * Large pack files will contain a relatively large fraction
1233 * of our objects so we will likely need to visit them anyway.
1234 * The more objects a pack file contains the higher the probability
1235 * of a false-positive match from the bloom filter. And reading
1236 * all object IDs from a large pack index file can be expensive.
1238 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1241 /* Do we already have a filter for this pack index? */
1242 if (get_packidx_bloom_filter(repo, path_packidx,
1243 strlen(path_packidx)) != NULL)
1246 bf = calloc(1, sizeof(*bf));
1248 return got_error_from_errno("calloc");
1249 bf->bloom = calloc(1, sizeof(*bf->bloom));
1250 if (bf->bloom == NULL) {
1252 return got_error_from_errno("calloc");
1255 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1256 if (len >= sizeof(bf->path)) {
1259 return got_error(GOT_ERR_NO_SPACE);
1263 /* Minimum size supported by our bloom filter is 1000 entries. */
1264 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1265 for (i = 0; i < nobjects; i++) {
1266 struct got_packidx_object_id *id;
1267 id = &packidx->hdr.sorted_ids[i];
1268 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1271 RB_INSERT(got_packidx_bloom_filter_tree,
1272 &repo->packidx_bloom_filters, bf);
1277 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1279 struct got_pathlist_entry *pe;
1281 while (!TAILQ_EMPTY(packidx_paths)) {
1282 pe = TAILQ_FIRST(packidx_paths);
1283 TAILQ_REMOVE(packidx_paths, pe, entry);
1284 free((char *)pe->path);
1289 static const struct got_error *
1290 refresh_packidx_paths(struct got_repository *repo)
1292 const struct got_error *err = NULL;
1293 char *objects_pack_dir = NULL;
1296 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1297 if (objects_pack_dir == NULL)
1298 return got_error_from_errno("got_repo_get_path_objects_pack");
1300 if (stat(objects_pack_dir, &sb) == -1) {
1301 if (errno != ENOENT) {
1302 err = got_error_from_errno2("stat", objects_pack_dir);
1305 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1306 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1307 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1308 purge_packidx_paths(&repo->packidx_paths);
1309 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1314 free(objects_pack_dir);
1318 const struct got_error *
1319 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1320 struct got_repository *repo, struct got_object_id *id)
1322 const struct got_error *err;
1323 struct got_pathlist_entry *pe;
1326 /* Search pack index cache. */
1327 for (i = 0; i < repo->pack_cache_size; i++) {
1328 if (repo->packidx_cache[i] == NULL)
1330 if (!got_repo_check_packidx_bloom_filter(repo,
1331 repo->packidx_cache[i]->path_packidx, id))
1332 continue; /* object will not be found in this index */
1333 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1335 *packidx = repo->packidx_cache[i];
1337 * Move this cache entry to the front. Repeatedly
1338 * searching a wrong pack index can be expensive.
1341 memmove(&repo->packidx_cache[1],
1342 &repo->packidx_cache[0],
1343 i * sizeof(repo->packidx_cache[0]));
1344 repo->packidx_cache[0] = *packidx;
1345 if (repo->pinned_packidx >= 0 &&
1346 repo->pinned_packidx < i)
1347 repo->pinned_packidx++;
1348 else if (repo->pinned_packidx == i)
1349 repo->pinned_packidx = 0;
1354 /* No luck. Search the filesystem. */
1356 err = refresh_packidx_paths(repo);
1360 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1361 const char *path_packidx = pe->path;
1364 if (!got_repo_check_packidx_bloom_filter(repo,
1366 continue; /* object will not be found in this index */
1368 for (i = 0; i < repo->pack_cache_size; i++) {
1369 if (repo->packidx_cache[i] == NULL)
1371 if (strcmp(repo->packidx_cache[i]->path_packidx,
1372 path_packidx) == 0) {
1378 continue; /* already searched */
1380 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1385 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1389 err = cache_packidx(repo, *packidx, path_packidx);
1393 *idx = got_packidx_get_object_idx(*packidx, id);
1395 err = NULL; /* found the object */
1400 err = got_error_no_obj(id);
1405 const struct got_error *
1406 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1407 struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 DIR *packdir = NULL;
1411 struct dirent *dent;
1412 char *path_packidx = NULL;
1416 packdir_fd = openat(got_repo_get_fd(repo),
1417 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1418 if (packdir_fd == -1) {
1419 return got_error_from_errno_fmt("openat: %s/%s",
1420 got_repo_get_path_git_dir(repo),
1421 GOT_OBJECTS_PACK_DIR);
1424 packdir = fdopendir(packdir_fd);
1425 if (packdir == NULL) {
1426 err = got_error_from_errno("fdopendir");
1430 if (fstat(packdir_fd, &sb) == -1) {
1431 err = got_error_from_errno("fstat");
1434 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1435 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1437 while ((dent = readdir(packdir)) != NULL) {
1438 if (!got_repo_is_packidx_filename(dent->d_name,
1439 strlen(dent->d_name)))
1442 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1443 dent->d_name) == -1) {
1444 err = got_error_from_errno("asprintf");
1445 path_packidx = NULL;
1449 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1456 if (packdir && closedir(packdir) != 0 && err == NULL)
1457 err = got_error_from_errno("closedir");
1461 const struct got_error *
1462 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1463 struct got_repository *repo)
1465 const struct got_error *err;
1470 /* Search pack index cache. */
1471 for (i = 0; i < repo->pack_cache_size; i++) {
1472 if (repo->packidx_cache[i] == NULL)
1474 if (strcmp(repo->packidx_cache[i]->path_packidx,
1475 path_packidx) == 0) {
1476 *packidx = repo->packidx_cache[i];
1480 /* No luck. Search the filesystem. */
1482 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1487 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1491 err = cache_packidx(repo, *packidx, path_packidx);
1494 got_packidx_close(*packidx);
1500 static const struct got_error *
1501 read_packfile_hdr(int fd, struct got_packidx *packidx)
1503 const struct got_error *err = NULL;
1504 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1505 struct got_packfile_hdr hdr;
1508 n = read(fd, &hdr, sizeof(hdr));
1510 return got_error_from_errno("read");
1511 if (n != sizeof(hdr))
1512 return got_error(GOT_ERR_BAD_PACKFILE);
1514 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1515 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1516 be32toh(hdr.nobjects) != totobj)
1517 err = got_error(GOT_ERR_BAD_PACKFILE);
1522 static const struct got_error *
1523 open_packfile(int *fd, struct got_repository *repo,
1524 const char *relpath, struct got_packidx *packidx)
1526 const struct got_error *err = NULL;
1528 *fd = openat(got_repo_get_fd(repo), relpath,
1529 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1531 return got_error_from_errno_fmt("openat: %s/%s",
1532 got_repo_get_path_git_dir(repo), relpath);
1535 err = read_packfile_hdr(*fd, packidx);
1545 const struct got_error *
1546 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1547 const char *path_packfile, struct got_packidx *packidx)
1549 const struct got_error *err = NULL;
1550 struct got_pack *pack = NULL;
1557 for (i = 0; i < repo->pack_cache_size; i++) {
1558 pack = &repo->packs[i];
1559 if (pack->path_packfile == NULL)
1561 if (strcmp(pack->path_packfile, path_packfile) == 0)
1562 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1565 if (i == repo->pack_cache_size) {
1566 struct got_pack tmp;
1569 } while (i > 0 && repo->pinned_pack >= 0 &&
1570 i == repo->pinned_pack);
1571 err = got_pack_close(&repo->packs[i]);
1574 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1575 return got_error_from_errno("ftruncate");
1576 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1577 return got_error_from_errno("ftruncate");
1578 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1579 memcpy(&repo->packs[i], &repo->packs[0],
1580 sizeof(repo->packs[i]));
1581 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1582 if (repo->pinned_pack == 0)
1583 repo->pinned_pack = i;
1584 else if (repo->pinned_pack == i)
1585 repo->pinned_pack = 0;
1589 pack = &repo->packs[i];
1591 pack->path_packfile = strdup(path_packfile);
1592 if (pack->path_packfile == NULL) {
1593 err = got_error_from_errno("strdup");
1597 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1601 if (fstat(pack->fd, &sb) != 0) {
1602 err = got_error_from_errno("fstat");
1605 pack->filesize = sb.st_size;
1607 pack->privsep_child = NULL;
1609 err = got_delta_cache_alloc(&pack->delta_cache);
1613 #ifndef GOT_PACK_NO_MMAP
1614 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1615 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1617 if (pack->map == MAP_FAILED) {
1618 if (errno != ENOMEM) {
1619 err = got_error_from_errno("mmap");
1622 pack->map = NULL; /* fall back to read(2) */
1629 got_pack_close(pack);
1636 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1638 struct got_pack *pack = NULL;
1641 for (i = 0; i < repo->pack_cache_size; i++) {
1642 pack = &repo->packs[i];
1643 if (pack->path_packfile == NULL)
1645 if (strcmp(pack->path_packfile, path_packfile) == 0)
1652 const struct got_error *
1653 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1654 struct got_pack *pack)
1657 int pinned_pack = -1, pinned_packidx = -1;
1659 for (i = 0; i < repo->pack_cache_size; i++) {
1660 if (repo->packidx_cache[i] &&
1661 strcmp(repo->packidx_cache[i]->path_packidx,
1662 packidx->path_packidx) == 0)
1664 if (repo->packs[i].path_packfile &&
1665 strcmp(repo->packs[i].path_packfile,
1666 pack->path_packfile) == 0)
1670 if (pinned_packidx == -1 || pinned_pack == -1)
1671 return got_error(GOT_ERR_PIN_PACK);
1673 repo->pinned_pack = pinned_pack;
1674 repo->pinned_packidx = pinned_packidx;
1675 if (repo->packs[pinned_pack].privsep_child)
1676 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1681 got_repo_get_pinned_pack(struct got_repository *repo)
1683 if (repo->pinned_pack >= 0 &&
1684 repo->pinned_pack < repo->pack_cache_size)
1685 return &repo->packs[repo->pinned_pack];
1691 got_repo_unpin_pack(struct got_repository *repo)
1693 repo->pinned_packidx = -1;
1694 repo->pinned_pack = -1;
1695 repo->pinned_pid = 0;
1698 const struct got_error *
1699 got_repo_init(const char *repo_path, const char *head_name)
1701 const struct got_error *err = NULL;
1702 const char *dirnames[] = {
1704 GOT_OBJECTS_PACK_DIR,
1707 const char *description_str = "Unnamed repository; "
1708 "edit this file 'description' to name the repository.";
1709 const char *headref = "ref: refs/heads/";
1710 const char *gitconfig_str = "[core]\n"
1711 "\trepositoryformatversion = 0\n"
1712 "\tfilemode = true\n"
1714 char *headref_str, *path;
1717 if (!got_path_dir_is_empty(repo_path))
1718 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1720 for (i = 0; i < nitems(dirnames); i++) {
1721 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1722 return got_error_from_errno("asprintf");
1724 err = got_path_mkdir(path);
1730 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1731 return got_error_from_errno("asprintf");
1732 err = got_path_create_file(path, description_str);
1737 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1738 return got_error_from_errno("asprintf");
1739 if (asprintf(&headref_str, "%s%s", headref,
1740 head_name ? head_name : "main") == -1) {
1742 return got_error_from_errno("asprintf");
1744 err = got_path_create_file(path, headref_str);
1750 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1751 return got_error_from_errno("asprintf");
1752 err = got_path_create_file(path, gitconfig_str);
1760 static const struct got_error *
1761 match_packed_object(struct got_object_id **unique_id,
1762 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1764 const struct got_error *err = NULL;
1765 struct got_object_id_queue matched_ids;
1766 struct got_pathlist_entry *pe;
1768 STAILQ_INIT(&matched_ids);
1770 err = refresh_packidx_paths(repo);
1774 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1775 const char *path_packidx = pe->path;
1776 struct got_packidx *packidx;
1777 struct got_object_qid *qid;
1779 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1784 err = got_packidx_match_id_str_prefix(&matched_ids,
1785 packidx, id_str_prefix);
1787 got_packidx_close(packidx);
1790 err = got_packidx_close(packidx);
1794 STAILQ_FOREACH(qid, &matched_ids, entry) {
1795 if (obj_type != GOT_OBJ_TYPE_ANY) {
1797 err = got_object_get_type(&matched_type, repo,
1801 if (matched_type != obj_type)
1804 if (*unique_id == NULL) {
1805 *unique_id = got_object_id_dup(&qid->id);
1806 if (*unique_id == NULL) {
1807 err = got_error_from_errno("malloc");
1811 if (got_object_id_cmp(*unique_id,
1813 continue; /* packed multiple times */
1814 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1820 got_object_id_queue_free(&matched_ids);
1828 static const struct got_error *
1829 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1830 const char *object_dir, const char *id_str_prefix, int obj_type,
1831 struct got_repository *repo)
1833 const struct got_error *err = NULL;
1834 char *path, *id_str = NULL;
1836 struct dirent *dent;
1837 struct got_object_id id;
1839 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1840 err = got_error_from_errno("asprintf");
1844 dir = opendir(path);
1846 if (errno == ENOENT) {
1850 err = got_error_from_errno2("opendir", path);
1853 while ((dent = readdir(dir)) != NULL) {
1859 if (strcmp(dent->d_name, ".") == 0 ||
1860 strcmp(dent->d_name, "..") == 0)
1863 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1864 err = got_error_from_errno("asprintf");
1868 if (!got_parse_object_id(&id, id_str, repo->algo))
1872 * Directory entries do not necessarily appear in
1873 * sorted order, so we must iterate over all of them.
1875 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1879 if (*unique_id == NULL) {
1880 if (obj_type != GOT_OBJ_TYPE_ANY) {
1882 err = got_object_get_type(&matched_type, repo,
1886 if (matched_type != obj_type)
1889 *unique_id = got_object_id_dup(&id);
1890 if (*unique_id == NULL) {
1891 err = got_error_from_errno("got_object_id_dup");
1895 if (got_object_id_cmp(*unique_id, &id) == 0)
1896 continue; /* both packed and loose */
1897 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1902 if (dir && closedir(dir) != 0 && err == NULL)
1903 err = got_error_from_errno("closedir");
1913 const struct got_error *
1914 got_repo_match_object_id_prefix(struct got_object_id **id,
1915 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1917 const struct got_error *err = NULL;
1918 char *path_objects = NULL, *object_dir = NULL;
1924 path_objects = got_repo_get_path_objects(repo);
1926 len = strlen(id_str_prefix);
1927 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1928 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1932 for (i = 0; i < len; i++) {
1933 if (isxdigit((unsigned char)id_str_prefix[i]))
1935 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1940 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1943 object_dir = strndup(id_str_prefix, 2);
1944 if (object_dir == NULL) {
1945 err = got_error_from_errno("strdup");
1948 err = match_loose_object(id, path_objects, object_dir,
1949 id_str_prefix, obj_type, repo);
1950 } else if (len == 1) {
1952 for (i = 0; i < 0xf; i++) {
1953 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1955 err = got_error_from_errno("asprintf");
1958 err = match_packed_object(id, repo, object_dir,
1962 err = match_loose_object(id, path_objects, object_dir,
1963 id_str_prefix, obj_type, repo);
1968 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1977 } else if (*id == NULL) {
1979 case GOT_OBJ_TYPE_BLOB:
1980 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1981 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1983 case GOT_OBJ_TYPE_TREE:
1984 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1985 GOT_OBJ_LABEL_TREE, id_str_prefix);
1987 case GOT_OBJ_TYPE_COMMIT:
1988 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1989 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1991 case GOT_OBJ_TYPE_TAG:
1992 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1993 GOT_OBJ_LABEL_TAG, id_str_prefix);
1996 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
2004 const struct got_error *
2005 got_repo_match_object_id(struct got_object_id **id, char **label,
2006 const char *id_str, int obj_type, struct got_reflist_head *refs,
2007 struct got_repository *repo)
2009 const struct got_error *err;
2010 struct got_tag_object *tag;
2011 struct got_reference *ref = NULL;
2018 err = got_repo_object_match_tag(&tag, id_str, obj_type,
2021 *id = got_object_id_dup(
2022 got_object_tag_get_object_id(tag));
2024 err = got_error_from_errno("got_object_id_dup");
2025 else if (label && asprintf(label, "refs/tags/%s",
2026 got_object_tag_get_name(tag)) == -1) {
2027 err = got_error_from_errno("asprintf");
2031 got_object_tag_close(tag);
2033 } else if (err->code != GOT_ERR_OBJ_TYPE &&
2034 err->code != GOT_ERR_NO_OBJ)
2038 err = got_ref_open(&ref, repo, id_str, 0);
2040 err = got_ref_resolve(id, repo, ref);
2044 *label = strdup(got_ref_get_name(ref));
2045 if (*label == NULL) {
2046 err = got_error_from_errno("strdup");
2051 if (err->code != GOT_ERR_NOT_REF &&
2052 err->code != GOT_ERR_BAD_REF_NAME)
2054 err = got_repo_match_object_id_prefix(id, id_str,
2057 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
2058 err = got_error_not_ref(id_str);
2062 err = got_object_id_str(label, *id);
2063 if (*label == NULL) {
2064 err = got_error_from_errno("strdup");
2075 const struct got_error *
2076 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
2077 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
2079 const struct got_error *err = NULL;
2080 struct got_reflist_entry *re;
2081 struct got_object_id *tag_id;
2082 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
2086 TAILQ_FOREACH(re, refs, entry) {
2087 const char *refname;
2088 refname = got_ref_get_name(re->ref);
2089 if (got_ref_is_symbolic(re->ref))
2091 if (strncmp(refname, "refs/tags/", 10) != 0)
2093 if (!name_is_absolute)
2094 refname += strlen("refs/tags/");
2095 if (strcmp(refname, name) != 0)
2097 err = got_ref_resolve(&tag_id, repo, re->ref);
2100 err = got_object_open_as_tag(tag, repo, tag_id);
2104 if (obj_type == GOT_OBJ_TYPE_ANY ||
2105 got_object_tag_get_object_type(*tag) == obj_type)
2107 got_object_tag_close(*tag);
2111 if (err == NULL && *tag == NULL)
2112 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2113 GOT_OBJ_LABEL_TAG, name);
2117 static const struct got_error *
2118 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2119 const char *name, mode_t mode, struct got_object_id *blob_id)
2121 const struct got_error *err = NULL;
2125 *new_te = calloc(1, sizeof(**new_te));
2126 if (*new_te == NULL)
2127 return got_error_from_errno("calloc");
2129 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2130 sizeof((*new_te)->name)) {
2131 err = got_error(GOT_ERR_NO_SPACE);
2135 if (S_ISLNK(mode)) {
2136 (*new_te)->mode = S_IFLNK;
2138 (*new_te)->mode = S_IFREG;
2139 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2141 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2143 if (err && *new_te) {
2150 static const struct got_error *
2151 import_file(struct got_tree_entry **new_te, struct dirent *de,
2152 const char *path, struct got_repository *repo)
2154 const struct got_error *err;
2155 struct got_object_id *blob_id = NULL;
2159 if (asprintf(&filepath, "%s%s%s", path,
2160 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2161 return got_error_from_errno("asprintf");
2163 if (lstat(filepath, &sb) != 0) {
2164 err = got_error_from_errno2("lstat", path);
2168 err = got_object_blob_create(&blob_id, filepath, repo);
2172 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2181 static const struct got_error *
2182 insert_tree_entry(struct got_tree_entry *new_te,
2183 struct got_pathlist_head *paths)
2185 const struct got_error *err = NULL;
2186 struct got_pathlist_entry *new_pe;
2188 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2192 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2196 static const struct got_error *write_tree(struct got_object_id **,
2197 const char *, struct got_pathlist_head *, struct got_repository *,
2198 got_repo_import_cb progress_cb, void *progress_arg);
2200 static const struct got_error *
2201 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2202 const char *path, struct got_pathlist_head *ignores,
2203 struct got_repository *repo,
2204 got_repo_import_cb progress_cb, void *progress_arg)
2206 const struct got_error *err;
2207 struct got_object_id *id = NULL;
2210 if (asprintf(&subdirpath, "%s%s%s", path,
2211 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2212 return got_error_from_errno("asprintf");
2214 (*new_te) = calloc(1, sizeof(**new_te));
2215 if (*new_te == NULL)
2216 return got_error_from_errno("calloc");
2217 (*new_te)->mode = S_IFDIR;
2218 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2219 sizeof((*new_te)->name)) {
2220 err = got_error(GOT_ERR_NO_SPACE);
2223 err = write_tree(&id, subdirpath, ignores, repo,
2224 progress_cb, progress_arg);
2227 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2239 static const struct got_error *
2240 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2241 struct got_pathlist_head *ignores, struct got_repository *repo,
2242 got_repo_import_cb progress_cb, void *progress_arg)
2244 const struct got_error *err = NULL;
2248 struct got_tree_entry *new_te = NULL;
2249 struct got_pathlist_head paths;
2250 struct got_pathlist_entry *pe;
2252 *new_tree_id = NULL;
2256 dir = opendir(path_dir);
2258 err = got_error_from_errno2("opendir", path_dir);
2263 while ((de = readdir(dir)) != NULL) {
2267 if (strcmp(de->d_name, ".") == 0 ||
2268 strcmp(de->d_name, "..") == 0)
2271 err = got_path_dirent_type(&type, path_dir, de);
2275 TAILQ_FOREACH(pe, ignores, entry) {
2276 if (type == DT_DIR && pe->path_len > 0 &&
2277 pe->path[pe->path_len - 1] == '/') {
2278 char stripped[PATH_MAX];
2280 if (strlcpy(stripped, pe->path,
2281 sizeof(stripped)) >= sizeof(stripped)) {
2282 err = got_error(GOT_ERR_NO_SPACE);
2285 got_path_strip_trailing_slashes(stripped);
2286 if (fnmatch(stripped, de->d_name, 0) == 0) {
2290 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2298 if (type == DT_DIR) {
2299 err = import_subdir(&new_te, de, path_dir,
2300 ignores, repo, progress_cb, progress_arg);
2302 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2307 } else if (type == DT_REG || type == DT_LNK) {
2308 err = import_file(&new_te, de, path_dir, repo);
2314 err = insert_tree_entry(new_te, &paths);
2320 if (TAILQ_EMPTY(&paths)) {
2321 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2322 "cannot create tree without any entries");
2326 TAILQ_FOREACH(pe, &paths, entry) {
2327 struct got_tree_entry *te = pe->data;
2329 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2331 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2332 err = got_error_from_errno("asprintf");
2335 err = (*progress_cb)(progress_arg, path);
2341 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2345 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2349 const struct got_error *
2350 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2351 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2352 struct got_repository *repo, got_repo_import_cb progress_cb,
2355 const struct got_error *err;
2356 struct got_object_id *new_tree_id;
2358 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2359 progress_cb, progress_arg);
2363 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2364 author, time(NULL), author, time(NULL), logmsg, repo);
2369 const struct got_error *
2370 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2371 struct got_repository *repo)
2373 const struct got_error *err = NULL;
2374 char *path_objects = NULL, *path = NULL;
2376 struct got_object_id id;
2382 path_objects = got_repo_get_path_objects(repo);
2383 if (path_objects == NULL)
2384 return got_error_from_errno("got_repo_get_path_objects");
2386 for (i = 0; i <= 0xff; i++) {
2387 struct dirent *dent;
2389 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2390 err = got_error_from_errno("asprintf");
2394 dir = opendir(path);
2396 if (errno == ENOENT) {
2400 err = got_error_from_errno2("opendir", path);
2404 while ((dent = readdir(dir)) != NULL) {
2409 if (strcmp(dent->d_name, ".") == 0 ||
2410 strcmp(dent->d_name, "..") == 0)
2413 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2414 err = got_error_from_errno("asprintf");
2418 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2424 err = got_object_open_loose_fd(&fd, &id, repo);
2428 if (fstat(fd, &sb) == -1) {
2429 err = got_error_from_errno("fstat");
2434 (*ondisk_size) += sb.st_size;
2436 if (close(fd) == -1) {
2437 err = got_error_from_errno("close");
2442 if (closedir(dir) != 0) {
2443 err = got_error_from_errno("closedir");
2452 if (dir && closedir(dir) != 0 && err == NULL)
2453 err = got_error_from_errno("closedir");
2464 const struct got_error *
2465 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2466 off_t *total_packsize, struct got_repository *repo)
2468 const struct got_error *err = NULL;
2469 DIR *packdir = NULL;
2470 struct dirent *dent;
2471 struct got_packidx *packidx = NULL;
2473 char *path_packfile;
2479 *total_packsize = 0;
2481 packdir_fd = openat(got_repo_get_fd(repo),
2482 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2483 if (packdir_fd == -1) {
2484 return got_error_from_errno_fmt("openat: %s/%s",
2485 got_repo_get_path_git_dir(repo),
2486 GOT_OBJECTS_PACK_DIR);
2489 packdir = fdopendir(packdir_fd);
2490 if (packdir == NULL) {
2491 err = got_error_from_errno("fdopendir");
2495 while ((dent = readdir(packdir)) != NULL) {
2496 if (!got_repo_is_packidx_filename(dent->d_name,
2497 strlen(dent->d_name)))
2500 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2501 dent->d_name) == -1) {
2502 err = got_error_from_errno("asprintf");
2506 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2512 if (fstat(packidx->fd, &sb) == -1)
2514 *total_packsize += sb.st_size;
2516 err = got_packidx_get_packfile_path(&path_packfile,
2517 packidx->path_packidx);
2521 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2523 free(path_packfile);
2526 free(path_packfile);
2527 *total_packsize += sb.st_size;
2529 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2533 got_packidx_close(packidx);
2538 got_packidx_close(packidx);
2539 if (packdir && closedir(packdir) != 0 && err == NULL)
2540 err = got_error_from_errno("closedir");
2544 *total_packsize = 0;
2549 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2550 got_packidx_bloom_filter_cmp);