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_delta_cache.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_hash.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
69 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
70 got_packidx_bloom_filter_cmp);
73 is_boolean_val(const char *val)
75 return (strcasecmp(val, "true") == 0 ||
76 strcasecmp(val, "false") == 0 ||
77 strcasecmp(val, "on") == 0 ||
78 strcasecmp(val, "off") == 0 ||
79 strcasecmp(val, "yes") == 0 ||
80 strcasecmp(val, "no") == 0 ||
81 strcasecmp(val, "1") == 0 ||
82 strcasecmp(val, "0") == 0);
86 get_boolean_val(const char *val)
88 return (strcasecmp(val, "true") == 0 ||
89 strcasecmp(val, "on") == 0 ||
90 strcasecmp(val, "yes") == 0 ||
91 strcasecmp(val, "1") == 0);
95 got_repo_get_path(struct got_repository *repo)
101 got_repo_get_path_git_dir(struct got_repository *repo)
103 return repo->path_git_dir;
107 got_repo_get_fd(struct got_repository *repo)
109 return repo->gitdir_fd;
112 enum got_hash_algorithm
113 got_repo_get_object_format(struct got_repository *repo)
119 got_repo_get_gitconfig_author_name(struct got_repository *repo)
121 return repo->gitconfig_author_name;
125 got_repo_get_gitconfig_author_email(struct got_repository *repo)
127 return repo->gitconfig_author_email;
131 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
133 return repo->global_gitconfig_author_name;
137 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
139 return repo->global_gitconfig_author_email;
143 got_repo_get_gitconfig_owner(struct got_repository *repo)
145 return repo->gitconfig_owner;
149 got_repo_has_extension(struct got_repository *repo, const char *ext)
153 for (i = 0; i < repo->nextensions; ++i) {
154 if (!strcasecmp(ext, repo->extnames[i]))
155 return get_boolean_val(repo->extvals[i]);
162 got_repo_is_bare(struct got_repository *repo)
164 return (strcmp(repo->path, repo->path_git_dir) == 0);
168 get_path_git_child(struct got_repository *repo, const char *basename)
172 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
180 got_repo_get_path_objects(struct got_repository *repo)
182 return get_path_git_child(repo, GOT_OBJECTS_DIR);
186 got_repo_get_path_objects_pack(struct got_repository *repo)
188 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
192 got_repo_get_path_refs(struct got_repository *repo)
194 return get_path_git_child(repo, GOT_REFS_DIR);
198 got_repo_get_path_packed_refs(struct got_repository *repo)
200 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
204 get_path_head(struct got_repository *repo)
206 return get_path_git_child(repo, GOT_HEAD_FILE);
210 got_repo_get_path_gitconfig(struct got_repository *repo)
212 return get_path_git_child(repo, GOT_GITCONFIG);
216 got_repo_get_path_gotconfig(struct got_repository *repo)
218 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
221 const struct got_gotconfig *
222 got_repo_get_gotconfig(struct got_repository *repo)
224 return repo->gotconfig;
228 got_repo_get_gitconfig_remotes(int *nremotes,
229 const struct got_remote_repo **remotes, struct got_repository *repo)
231 *nremotes = repo->ngitconfig_remotes;
232 *remotes = repo->gitconfig_remotes;
236 is_git_repo(struct got_repository *repo)
238 const char *path_git = got_repo_get_path_git_dir(repo);
239 char *path_objects = got_repo_get_path_objects(repo);
240 char *path_refs = got_repo_get_path_refs(repo);
241 char *path_head = get_path_head(repo);
244 struct got_reference *head_ref;
246 if (lstat(path_git, &sb) == -1)
248 if (!S_ISDIR(sb.st_mode))
251 if (lstat(path_objects, &sb) == -1)
253 if (!S_ISDIR(sb.st_mode))
256 if (lstat(path_refs, &sb) == -1)
258 if (!S_ISDIR(sb.st_mode))
261 if (lstat(path_head, &sb) == -1)
263 if (!S_ISREG(sb.st_mode))
266 /* Check if the HEAD reference can be opened. */
267 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
269 got_ref_close(head_ref);
280 static const struct got_error *
281 close_tempfiles(int *fds, size_t nfds)
283 const struct got_error *err = NULL;
286 for (i = 0; i < nfds; i++) {
289 if (close(fds[i]) == -1) {
290 err = got_error_from_errno("close");
298 static const struct got_error *
299 open_tempfiles(int **fds, size_t array_size, size_t nfds)
301 const struct got_error *err = NULL;
304 *fds = calloc(array_size, sizeof(**fds));
306 return got_error_from_errno("calloc");
308 for (i = 0; i < array_size; i++)
311 for (i = 0; i < nfds; i++) {
312 (*fds)[i] = got_opentempfd();
313 if ((*fds)[i] == -1) {
314 err = got_error_from_errno("got_opentempfd");
315 close_tempfiles(*fds, nfds);
324 static const struct got_error *
325 get_pack_cache_size(int *pack_cache_size)
329 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
330 return got_error_from_errno("getrlimit");
332 *pack_cache_size = GOT_PACK_CACHE_SIZE;
333 if (*pack_cache_size > rl.rlim_cur / 8)
334 *pack_cache_size = rl.rlim_cur / 8;
339 const struct got_error *
340 got_repo_pack_fds_open(int **pack_fds)
342 const struct got_error *err;
345 err = get_pack_cache_size(&nfds);
350 * We need one basefd and one accumfd per cached pack.
351 * Our constants should be set up in a way such that
352 * this error never triggers.
354 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
355 return got_error(GOT_ERR_NO_SPACE);
357 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
360 const struct got_error *
361 got_repo_pack_fds_close(int *pack_fds)
363 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
366 const struct got_error *
367 got_repo_temp_fds_open(int **temp_fds)
369 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
370 GOT_REPO_NUM_TEMPFILES);
374 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
378 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
379 repo->tempfiles[i] = temp_fds[i];
382 const struct got_error *
383 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
390 for (i = 0; i < nitems(repo->tempfiles); i++) {
391 if (repo->tempfile_use_mask & (1 << i))
393 if (repo->tempfiles[i] != -1) {
394 if (ftruncate(repo->tempfiles[i], 0L) == -1)
395 return got_error_from_errno("ftruncate");
396 *fd = repo->tempfiles[i];
398 repo->tempfile_use_mask |= (1 << i);
403 return got_error(GOT_ERR_REPO_TEMPFILE);
407 got_repo_temp_fds_put(int idx, struct got_repository *repo)
409 repo->tempfile_use_mask &= ~(1 << idx);
412 const struct got_error *
413 got_repo_temp_fds_close(int *temp_fds)
415 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
418 const struct got_error *
419 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
420 struct got_object *obj)
422 #ifndef GOT_NO_OBJ_CACHE
423 const struct got_error *err = NULL;
424 err = got_object_cache_add(&repo->objcache, id, obj);
426 if (err->code == GOT_ERR_OBJ_EXISTS ||
427 err->code == GOT_ERR_OBJ_TOO_LARGE)
437 got_repo_get_cached_object(struct got_repository *repo,
438 struct got_object_id *id)
440 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
443 const struct got_error *
444 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
445 struct got_tree_object *tree)
447 #ifndef GOT_NO_OBJ_CACHE
448 const struct got_error *err = NULL;
449 err = got_object_cache_add(&repo->treecache, id, tree);
451 if (err->code == GOT_ERR_OBJ_EXISTS ||
452 err->code == GOT_ERR_OBJ_TOO_LARGE)
461 struct got_tree_object *
462 got_repo_get_cached_tree(struct got_repository *repo,
463 struct got_object_id *id)
465 return (struct got_tree_object *)got_object_cache_get(
466 &repo->treecache, id);
469 const struct got_error *
470 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
471 struct got_commit_object *commit)
473 #ifndef GOT_NO_OBJ_CACHE
474 const struct got_error *err = NULL;
475 err = got_object_cache_add(&repo->commitcache, id, commit);
477 if (err->code == GOT_ERR_OBJ_EXISTS ||
478 err->code == GOT_ERR_OBJ_TOO_LARGE)
487 struct got_commit_object *
488 got_repo_get_cached_commit(struct got_repository *repo,
489 struct got_object_id *id)
491 return (struct got_commit_object *)got_object_cache_get(
492 &repo->commitcache, id);
495 const struct got_error *
496 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
497 struct got_tag_object *tag)
499 #ifndef GOT_NO_OBJ_CACHE
500 const struct got_error *err = NULL;
501 err = got_object_cache_add(&repo->tagcache, id, tag);
503 if (err->code == GOT_ERR_OBJ_EXISTS ||
504 err->code == GOT_ERR_OBJ_TOO_LARGE)
513 struct got_tag_object *
514 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
516 return (struct got_tag_object *)got_object_cache_get(
517 &repo->tagcache, id);
520 const struct got_error *
521 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
522 struct got_raw_object *raw)
524 #ifndef GOT_NO_OBJ_CACHE
525 const struct got_error *err = NULL;
526 err = got_object_cache_add(&repo->rawcache, id, raw);
528 if (err->code == GOT_ERR_OBJ_EXISTS ||
529 err->code == GOT_ERR_OBJ_TOO_LARGE)
539 struct got_raw_object *
540 got_repo_get_cached_raw_object(struct got_repository *repo,
541 struct got_object_id *id)
543 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
547 static const struct got_error *
548 open_repo(struct got_repository *repo, const char *path)
550 const struct got_error *err = NULL;
552 repo->gitdir_fd = -1;
554 /* bare git repository? */
555 repo->path_git_dir = strdup(path);
556 if (repo->path_git_dir == NULL)
557 return got_error_from_errno("strdup");
558 if (is_git_repo(repo)) {
559 repo->path = strdup(repo->path_git_dir);
560 if (repo->path == NULL) {
561 err = got_error_from_errno("strdup");
564 repo->gitdir_fd = open(repo->path_git_dir,
565 O_DIRECTORY | O_CLOEXEC);
566 if (repo->gitdir_fd == -1) {
567 err = got_error_from_errno2("open",
574 /* git repository with working tree? */
575 free(repo->path_git_dir);
576 repo->path_git_dir = NULL;
577 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
578 err = got_error_from_errno("asprintf");
581 if (is_git_repo(repo)) {
582 repo->path = strdup(path);
583 if (repo->path == NULL) {
584 err = got_error_from_errno("strdup");
587 repo->gitdir_fd = open(repo->path_git_dir,
588 O_DIRECTORY | O_CLOEXEC);
589 if (repo->gitdir_fd == -1) {
590 err = got_error_from_errno2("open",
597 err = got_error(GOT_ERR_NOT_GIT_REPO);
602 free(repo->path_git_dir);
603 repo->path_git_dir = NULL;
604 if (repo->gitdir_fd != -1)
605 close(repo->gitdir_fd);
606 repo->gitdir_fd = -1;
612 static const struct got_error *
613 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
615 const struct got_error *err = NULL;
616 char *repo_gitconfig_path = NULL;
618 if (global_gitconfig_path) {
619 /* Read settings from ~/.gitconfig. */
620 int dummy_repo_version;
621 err = got_repo_read_gitconfig(&dummy_repo_version,
622 &repo->global_gitconfig_author_name,
623 &repo->global_gitconfig_author_email,
624 NULL, NULL, NULL, NULL, NULL, NULL,
625 global_gitconfig_path);
630 /* Read repository's .git/config file. */
631 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
632 if (repo_gitconfig_path == NULL)
633 return got_error_from_errno("got_repo_get_path_gitconfig");
635 err = got_repo_read_gitconfig(
636 &repo->gitconfig_repository_format_version,
637 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
638 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
639 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
640 &repo->nextensions, repo_gitconfig_path);
644 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
647 for (i = 0; i < repo->ngitconfig_remotes; i++) {
648 got_repo_free_remote_repo_data(
649 &repo->gitconfig_remotes[i]);
651 free(repo->gitconfig_remotes);
652 repo->gitconfig_remotes = NULL;
653 repo->ngitconfig_remotes = 0;
655 free(repo->gitconfig_author_name);
656 repo->gitconfig_author_name = NULL;
657 free(repo->gitconfig_author_email);
658 repo->gitconfig_author_email = NULL;
660 free(repo->global_gitconfig_author_name);
661 repo->global_gitconfig_author_name = NULL;
662 free(repo->global_gitconfig_author_email);
663 repo->global_gitconfig_author_email = NULL;
667 free(repo_gitconfig_path);
671 static const struct got_error *
672 read_gotconfig(struct got_repository *repo)
674 const struct got_error *err = NULL;
675 char *gotconfig_path;
677 gotconfig_path = got_repo_get_path_gotconfig(repo);
678 if (gotconfig_path == NULL)
679 return got_error_from_errno("got_repo_get_path_gotconfig");
681 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
682 free(gotconfig_path);
686 /* Supported repository format extensions. */
687 static const char *const repo_extensions[] = {
688 "noop", /* Got supports repository format version 1. */
689 "preciousObjects", /* Supported by gotadmin cleanup. */
690 "worktreeConfig", /* Got does not care about Git work trees. */
693 const struct got_error *
694 got_repo_open(struct got_repository **repop, const char *path,
695 const char *global_gitconfig_path, int *pack_fds)
697 struct got_repository *repo = NULL;
698 const struct got_error *err = NULL;
699 char *repo_path = NULL;
704 repo = calloc(1, sizeof(*repo));
706 return got_error_from_errno("calloc");
708 RB_INIT(&repo->packidx_bloom_filters);
709 TAILQ_INIT(&repo->packidx_paths);
711 for (i = 0; i < nitems(repo->privsep_children); i++) {
712 memset(&repo->privsep_children[i], 0,
713 sizeof(repo->privsep_children[0]));
714 repo->privsep_children[i].imsg_fd = -1;
717 err = got_object_cache_init(&repo->objcache,
718 GOT_OBJECT_CACHE_TYPE_OBJ);
721 err = got_object_cache_init(&repo->treecache,
722 GOT_OBJECT_CACHE_TYPE_TREE);
725 err = got_object_cache_init(&repo->commitcache,
726 GOT_OBJECT_CACHE_TYPE_COMMIT);
729 err = got_object_cache_init(&repo->tagcache,
730 GOT_OBJECT_CACHE_TYPE_TAG);
733 err = got_object_cache_init(&repo->rawcache,
734 GOT_OBJECT_CACHE_TYPE_RAW);
738 err = get_pack_cache_size(&repo->pack_cache_size);
741 for (i = 0; i < nitems(repo->packs); i++) {
742 if (pack_fds != NULL && i < repo->pack_cache_size) {
743 repo->packs[i].basefd = pack_fds[j++];
744 repo->packs[i].accumfd = pack_fds[j++];
746 repo->packs[i].basefd = -1;
747 repo->packs[i].accumfd = -1;
750 for (i = 0; i < nitems(repo->tempfiles); i++)
751 repo->tempfiles[i] = -1;
752 repo->pinned_pack = -1;
753 repo->pinned_packidx = -1;
754 repo->pinned_pid = 0;
756 repo_path = realpath(path, NULL);
757 if (repo_path == NULL) {
758 err = got_error_from_errno2("realpath", path);
765 err = open_repo(repo, repo_path);
768 if (err->code != GOT_ERR_NOT_GIT_REPO)
770 if (repo_path[0] == '/' && repo_path[1] == '\0') {
771 err = got_error(GOT_ERR_NOT_GIT_REPO);
774 err = got_path_dirname(&parent_path, repo_path);
778 repo_path = parent_path;
781 err = read_gotconfig(repo);
785 err = read_gitconfig(repo, global_gitconfig_path);
788 if (repo->gitconfig_repository_format_version != 0) {
789 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
792 for (i = 0; i < repo->nextensions; i++) {
793 char *ext = repo->extnames[i];
794 char *val = repo->extvals[i];
795 int j, supported = 0;
797 if (!is_boolean_val(val)) {
798 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
802 if (!get_boolean_val(val))
805 for (j = 0; j < nitems(repo_extensions); j++) {
806 if (strcmp(ext, repo_extensions[j]) == 0) {
812 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
817 err = got_repo_list_packidx(&repo->packidx_paths, repo);
820 got_repo_close(repo);
827 const struct got_error *
828 got_repo_close(struct got_repository *repo)
830 const struct got_error *err = NULL, *child_err;
831 struct got_packidx_bloom_filter *bf;
834 for (i = 0; i < repo->pack_cache_size; i++) {
835 if (repo->packidx_cache[i] == NULL)
837 got_packidx_close(repo->packidx_cache[i]);
840 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
841 &repo->packidx_bloom_filters))) {
842 RB_REMOVE(got_packidx_bloom_filter_tree,
843 &repo->packidx_bloom_filters, bf);
844 bloom_free(bf->bloom);
849 for (i = 0; i < repo->pack_cache_size; i++)
850 if (repo->packs[i].path_packfile)
851 if (repo->packs[i].path_packfile)
852 got_pack_close(&repo->packs[i]);
855 free(repo->path_git_dir);
857 got_object_cache_close(&repo->objcache);
858 got_object_cache_close(&repo->treecache);
859 got_object_cache_close(&repo->commitcache);
860 got_object_cache_close(&repo->tagcache);
861 got_object_cache_close(&repo->rawcache);
863 for (i = 0; i < nitems(repo->privsep_children); i++) {
864 if (repo->privsep_children[i].imsg_fd == -1)
866 imsg_clear(repo->privsep_children[i].ibuf);
867 free(repo->privsep_children[i].ibuf);
868 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
869 child_err = got_privsep_wait_for_child(
870 repo->privsep_children[i].pid);
871 if (child_err && err == NULL)
873 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
875 err = got_error_from_errno("close");
878 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
880 err = got_error_from_errno("close");
883 got_gotconfig_free(repo->gotconfig);
884 free(repo->gitconfig_author_name);
885 free(repo->gitconfig_author_email);
886 for (i = 0; i < repo->ngitconfig_remotes; i++)
887 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
888 free(repo->gitconfig_remotes);
889 for (i = 0; i < repo->nextensions; i++) {
890 free(repo->extnames[i]);
891 free(repo->extvals[i]);
893 free(repo->extnames);
896 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
903 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
909 free(repo->fetch_url);
910 repo->fetch_url = NULL;
911 free(repo->send_url);
912 repo->send_url = NULL;
913 for (i = 0; i < repo->nfetch_branches; i++)
914 free(repo->fetch_branches[i]);
915 free(repo->fetch_branches);
916 repo->fetch_branches = NULL;
917 repo->nfetch_branches = 0;
918 for (i = 0; i < repo->nsend_branches; i++)
919 free(repo->send_branches[i]);
920 free(repo->send_branches);
921 repo->send_branches = NULL;
922 repo->nsend_branches = 0;
925 const struct got_error *
926 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
927 const char *input_path)
929 const struct got_error *err = NULL;
930 const char *repo_abspath = NULL;
932 char *canonpath, *path = NULL;
934 *in_repo_path = NULL;
936 canonpath = strdup(input_path);
937 if (canonpath == NULL) {
938 err = got_error_from_errno("strdup");
941 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
945 repo_abspath = got_repo_get_path(repo);
947 if (canonpath[0] == '\0') {
948 path = strdup(canonpath);
950 err = got_error_from_errno("strdup");
954 path = realpath(canonpath, NULL);
956 if (errno != ENOENT) {
957 err = got_error_from_errno2("realpath",
962 * Path is not on disk.
963 * Assume it is already relative to repository root.
965 path = strdup(canonpath);
967 err = got_error_from_errno("strdup");
972 repolen = strlen(repo_abspath);
976 if (strcmp(path, repo_abspath) == 0) {
980 err = got_error_from_errno("strdup");
983 } else if (len > repolen &&
984 got_path_is_child(path, repo_abspath, repolen)) {
985 /* Matched an on-disk path inside repository. */
986 if (got_repo_is_bare(repo)) {
988 * Matched an on-disk path inside repository
989 * database. Treat input as repository-relative.
996 /* Strip common prefix with repository path. */
997 err = got_path_skip_common_ancestor(&child,
1006 * Matched unrelated on-disk path.
1007 * Treat input as repository-relative.
1015 /* Make in-repository path absolute */
1016 if (path[0] != '/') {
1018 if (asprintf(&abspath, "/%s", path) == -1) {
1019 err = got_error_from_errno("asprintf");
1031 *in_repo_path = path;
1035 static const struct got_error *
1036 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1037 const char *path_packidx)
1039 const struct got_error *err = NULL;
1042 for (i = 0; i < repo->pack_cache_size; i++) {
1043 if (repo->packidx_cache[i] == NULL)
1045 if (strcmp(repo->packidx_cache[i]->path_packidx,
1046 path_packidx) == 0) {
1047 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1050 if (i == repo->pack_cache_size) {
1053 } while (i > 0 && repo->pinned_packidx >= 0 &&
1054 i == repo->pinned_packidx);
1055 err = got_packidx_close(repo->packidx_cache[i]);
1060 repo->packidx_cache[i] = packidx;
1066 got_repo_is_packidx_filename(const char *name, size_t len)
1068 if (len != GOT_PACKIDX_NAMELEN)
1071 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1074 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1075 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1081 static struct got_packidx_bloom_filter *
1082 get_packidx_bloom_filter(struct got_repository *repo,
1083 const char *path, size_t path_len)
1085 struct got_packidx_bloom_filter key;
1087 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1088 return NULL; /* XXX */
1089 key.path_len = path_len;
1091 return RB_FIND(got_packidx_bloom_filter_tree,
1092 &repo->packidx_bloom_filters, &key);
1096 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1097 const char *path_packidx, struct got_object_id *id)
1099 struct got_packidx_bloom_filter *bf;
1101 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1103 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1105 /* No bloom filter means this pack index must be searched. */
1109 static const struct got_error *
1110 add_packidx_bloom_filter(struct got_repository *repo,
1111 struct got_packidx *packidx, const char *path_packidx)
1113 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1114 struct got_packidx_bloom_filter *bf;
1118 * Don't use bloom filters for very large pack index files.
1119 * Large pack files will contain a relatively large fraction
1120 * of our objects so we will likely need to visit them anyway.
1121 * The more objects a pack file contains the higher the probability
1122 * of a false-positive match from the bloom filter. And reading
1123 * all object IDs from a large pack index file can be expensive.
1125 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1128 /* Do we already have a filter for this pack index? */
1129 if (get_packidx_bloom_filter(repo, path_packidx,
1130 strlen(path_packidx)) != NULL)
1133 bf = calloc(1, sizeof(*bf));
1135 return got_error_from_errno("calloc");
1136 bf->bloom = calloc(1, sizeof(*bf->bloom));
1137 if (bf->bloom == NULL) {
1139 return got_error_from_errno("calloc");
1142 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1143 if (len >= sizeof(bf->path)) {
1146 return got_error(GOT_ERR_NO_SPACE);
1150 /* Minimum size supported by our bloom filter is 1000 entries. */
1151 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1152 for (i = 0; i < nobjects; i++) {
1153 struct got_packidx_object_id *id;
1154 id = &packidx->hdr.sorted_ids[i];
1155 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1158 RB_INSERT(got_packidx_bloom_filter_tree,
1159 &repo->packidx_bloom_filters, bf);
1164 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1166 struct got_pathlist_entry *pe;
1168 while (!TAILQ_EMPTY(packidx_paths)) {
1169 pe = TAILQ_FIRST(packidx_paths);
1170 TAILQ_REMOVE(packidx_paths, pe, entry);
1171 free((char *)pe->path);
1176 static const struct got_error *
1177 refresh_packidx_paths(struct got_repository *repo)
1179 const struct got_error *err = NULL;
1180 char *objects_pack_dir = NULL;
1183 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1184 if (objects_pack_dir == NULL)
1185 return got_error_from_errno("got_repo_get_path_objects_pack");
1187 if (stat(objects_pack_dir, &sb) == -1) {
1188 if (errno != ENOENT) {
1189 err = got_error_from_errno2("stat", objects_pack_dir);
1192 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1193 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1194 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1195 purge_packidx_paths(&repo->packidx_paths);
1196 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1201 free(objects_pack_dir);
1205 const struct got_error *
1206 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1207 struct got_repository *repo, struct got_object_id *id)
1209 const struct got_error *err;
1210 struct got_pathlist_entry *pe;
1213 /* Search pack index cache. */
1214 for (i = 0; i < repo->pack_cache_size; i++) {
1215 if (repo->packidx_cache[i] == NULL)
1217 if (!got_repo_check_packidx_bloom_filter(repo,
1218 repo->packidx_cache[i]->path_packidx, id))
1219 continue; /* object will not be found in this index */
1220 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1222 *packidx = repo->packidx_cache[i];
1224 * Move this cache entry to the front. Repeatedly
1225 * searching a wrong pack index can be expensive.
1228 memmove(&repo->packidx_cache[1],
1229 &repo->packidx_cache[0],
1230 i * sizeof(repo->packidx_cache[0]));
1231 repo->packidx_cache[0] = *packidx;
1232 if (repo->pinned_packidx >= 0 &&
1233 repo->pinned_packidx < i)
1234 repo->pinned_packidx++;
1235 else if (repo->pinned_packidx == i)
1236 repo->pinned_packidx = 0;
1241 /* No luck. Search the filesystem. */
1243 err = refresh_packidx_paths(repo);
1247 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1248 const char *path_packidx = pe->path;
1251 if (!got_repo_check_packidx_bloom_filter(repo,
1253 continue; /* object will not be found in this index */
1255 for (i = 0; i < repo->pack_cache_size; i++) {
1256 if (repo->packidx_cache[i] == NULL)
1258 if (strcmp(repo->packidx_cache[i]->path_packidx,
1259 path_packidx) == 0) {
1265 continue; /* already searched */
1267 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1272 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1276 err = cache_packidx(repo, *packidx, path_packidx);
1280 *idx = got_packidx_get_object_idx(*packidx, id);
1282 err = NULL; /* found the object */
1287 err = got_error_no_obj(id);
1292 const struct got_error *
1293 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1294 struct got_repository *repo)
1296 const struct got_error *err = NULL;
1297 DIR *packdir = NULL;
1298 struct dirent *dent;
1299 char *path_packidx = NULL;
1303 packdir_fd = openat(got_repo_get_fd(repo),
1304 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1305 if (packdir_fd == -1) {
1306 return got_error_from_errno_fmt("openat: %s/%s",
1307 got_repo_get_path_git_dir(repo),
1308 GOT_OBJECTS_PACK_DIR);
1311 packdir = fdopendir(packdir_fd);
1312 if (packdir == NULL) {
1313 err = got_error_from_errno("fdopendir");
1317 if (fstat(packdir_fd, &sb) == -1) {
1318 err = got_error_from_errno("fstat");
1321 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1322 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1324 while ((dent = readdir(packdir)) != NULL) {
1325 if (!got_repo_is_packidx_filename(dent->d_name,
1326 strlen(dent->d_name)))
1329 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1330 dent->d_name) == -1) {
1331 err = got_error_from_errno("asprintf");
1332 path_packidx = NULL;
1336 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1343 if (packdir && closedir(packdir) != 0 && err == NULL)
1344 err = got_error_from_errno("closedir");
1348 const struct got_error *
1349 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1350 struct got_repository *repo)
1352 const struct got_error *err;
1357 /* Search pack index cache. */
1358 for (i = 0; i < repo->pack_cache_size; i++) {
1359 if (repo->packidx_cache[i] == NULL)
1361 if (strcmp(repo->packidx_cache[i]->path_packidx,
1362 path_packidx) == 0) {
1363 *packidx = repo->packidx_cache[i];
1367 /* No luck. Search the filesystem. */
1369 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1374 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1378 err = cache_packidx(repo, *packidx, path_packidx);
1381 got_packidx_close(*packidx);
1387 static const struct got_error *
1388 read_packfile_hdr(int fd, struct got_packidx *packidx)
1390 const struct got_error *err = NULL;
1391 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1392 struct got_packfile_hdr hdr;
1395 n = read(fd, &hdr, sizeof(hdr));
1397 return got_error_from_errno("read");
1398 if (n != sizeof(hdr))
1399 return got_error(GOT_ERR_BAD_PACKFILE);
1401 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1402 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1403 be32toh(hdr.nobjects) != totobj)
1404 err = got_error(GOT_ERR_BAD_PACKFILE);
1409 static const struct got_error *
1410 open_packfile(int *fd, struct got_repository *repo,
1411 const char *relpath, struct got_packidx *packidx)
1413 const struct got_error *err = NULL;
1415 *fd = openat(got_repo_get_fd(repo), relpath,
1416 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1418 return got_error_from_errno_fmt("openat: %s/%s",
1419 got_repo_get_path_git_dir(repo), relpath);
1422 err = read_packfile_hdr(*fd, packidx);
1432 const struct got_error *
1433 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1434 const char *path_packfile, struct got_packidx *packidx)
1436 const struct got_error *err = NULL;
1437 struct got_pack *pack = NULL;
1444 for (i = 0; i < repo->pack_cache_size; i++) {
1445 pack = &repo->packs[i];
1446 if (pack->path_packfile == NULL)
1448 if (strcmp(pack->path_packfile, path_packfile) == 0)
1449 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1452 if (i == repo->pack_cache_size) {
1453 struct got_pack tmp;
1456 } while (i > 0 && repo->pinned_pack >= 0 &&
1457 i == repo->pinned_pack);
1458 err = got_pack_close(&repo->packs[i]);
1461 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1462 return got_error_from_errno("ftruncate");
1463 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1464 return got_error_from_errno("ftruncate");
1465 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1466 memcpy(&repo->packs[i], &repo->packs[0],
1467 sizeof(repo->packs[i]));
1468 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1469 if (repo->pinned_pack == 0)
1470 repo->pinned_pack = i;
1471 else if (repo->pinned_pack == i)
1472 repo->pinned_pack = 0;
1476 pack = &repo->packs[i];
1478 pack->path_packfile = strdup(path_packfile);
1479 if (pack->path_packfile == NULL) {
1480 err = got_error_from_errno("strdup");
1484 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1488 if (fstat(pack->fd, &sb) != 0) {
1489 err = got_error_from_errno("fstat");
1492 pack->filesize = sb.st_size;
1494 pack->privsep_child = NULL;
1496 err = got_delta_cache_alloc(&pack->delta_cache);
1500 #ifndef GOT_PACK_NO_MMAP
1501 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1502 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1504 if (pack->map == MAP_FAILED) {
1505 if (errno != ENOMEM) {
1506 err = got_error_from_errno("mmap");
1509 pack->map = NULL; /* fall back to read(2) */
1516 got_pack_close(pack);
1523 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1525 struct got_pack *pack = NULL;
1528 for (i = 0; i < repo->pack_cache_size; i++) {
1529 pack = &repo->packs[i];
1530 if (pack->path_packfile == NULL)
1532 if (strcmp(pack->path_packfile, path_packfile) == 0)
1539 const struct got_error *
1540 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1541 struct got_pack *pack)
1544 int pinned_pack = -1, pinned_packidx = -1;
1546 for (i = 0; i < repo->pack_cache_size; i++) {
1547 if (repo->packidx_cache[i] &&
1548 strcmp(repo->packidx_cache[i]->path_packidx,
1549 packidx->path_packidx) == 0)
1551 if (repo->packs[i].path_packfile &&
1552 strcmp(repo->packs[i].path_packfile,
1553 pack->path_packfile) == 0)
1557 if (pinned_packidx == -1 || pinned_pack == -1)
1558 return got_error(GOT_ERR_PIN_PACK);
1560 repo->pinned_pack = pinned_pack;
1561 repo->pinned_packidx = pinned_packidx;
1562 if (repo->packs[pinned_pack].privsep_child)
1563 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1568 got_repo_get_pinned_pack(struct got_repository *repo)
1570 if (repo->pinned_pack >= 0 &&
1571 repo->pinned_pack < repo->pack_cache_size)
1572 return &repo->packs[repo->pinned_pack];
1578 got_repo_unpin_pack(struct got_repository *repo)
1580 repo->pinned_packidx = -1;
1581 repo->pinned_pack = -1;
1582 repo->pinned_pid = 0;
1585 const struct got_error *
1586 got_repo_init(const char *repo_path, const char *head_name)
1588 const struct got_error *err = NULL;
1589 const char *dirnames[] = {
1591 GOT_OBJECTS_PACK_DIR,
1594 const char *description_str = "Unnamed repository; "
1595 "edit this file 'description' to name the repository.";
1596 const char *headref = "ref: refs/heads/";
1597 const char *gitconfig_str = "[core]\n"
1598 "\trepositoryformatversion = 0\n"
1599 "\tfilemode = true\n"
1601 char *headref_str, *path;
1604 if (!got_path_dir_is_empty(repo_path))
1605 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1607 for (i = 0; i < nitems(dirnames); i++) {
1608 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1609 return got_error_from_errno("asprintf");
1611 err = got_path_mkdir(path);
1617 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1618 return got_error_from_errno("asprintf");
1619 err = got_path_create_file(path, description_str);
1624 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1625 return got_error_from_errno("asprintf");
1626 if (asprintf(&headref_str, "%s%s", headref,
1627 head_name ? head_name : "main") == -1) {
1629 return got_error_from_errno("asprintf");
1631 err = got_path_create_file(path, headref_str);
1637 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1638 return got_error_from_errno("asprintf");
1639 err = got_path_create_file(path, gitconfig_str);
1647 static const struct got_error *
1648 match_packed_object(struct got_object_id **unique_id,
1649 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1651 const struct got_error *err = NULL;
1652 struct got_object_id_queue matched_ids;
1653 struct got_pathlist_entry *pe;
1655 STAILQ_INIT(&matched_ids);
1657 err = refresh_packidx_paths(repo);
1661 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1662 const char *path_packidx = pe->path;
1663 struct got_packidx *packidx;
1664 struct got_object_qid *qid;
1666 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1671 err = got_packidx_match_id_str_prefix(&matched_ids,
1672 packidx, id_str_prefix);
1674 got_packidx_close(packidx);
1677 err = got_packidx_close(packidx);
1681 STAILQ_FOREACH(qid, &matched_ids, entry) {
1682 if (obj_type != GOT_OBJ_TYPE_ANY) {
1684 err = got_object_get_type(&matched_type, repo,
1688 if (matched_type != obj_type)
1691 if (*unique_id == NULL) {
1692 *unique_id = got_object_id_dup(&qid->id);
1693 if (*unique_id == NULL) {
1694 err = got_error_from_errno("malloc");
1698 if (got_object_id_cmp(*unique_id,
1700 continue; /* packed multiple times */
1701 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1707 got_object_id_queue_free(&matched_ids);
1715 static const struct got_error *
1716 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1717 const char *object_dir, const char *id_str_prefix, int obj_type,
1718 struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 char *path, *id_str = NULL;
1723 struct dirent *dent;
1724 struct got_object_id id;
1726 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1727 err = got_error_from_errno("asprintf");
1731 dir = opendir(path);
1733 if (errno == ENOENT) {
1737 err = got_error_from_errno2("opendir", path);
1740 while ((dent = readdir(dir)) != NULL) {
1746 if (strcmp(dent->d_name, ".") == 0 ||
1747 strcmp(dent->d_name, "..") == 0)
1750 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1751 err = got_error_from_errno("asprintf");
1755 if (!got_parse_object_id(&id, id_str, repo->algo))
1759 * Directory entries do not necessarily appear in
1760 * sorted order, so we must iterate over all of them.
1762 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1766 if (*unique_id == NULL) {
1767 if (obj_type != GOT_OBJ_TYPE_ANY) {
1769 err = got_object_get_type(&matched_type, repo,
1773 if (matched_type != obj_type)
1776 *unique_id = got_object_id_dup(&id);
1777 if (*unique_id == NULL) {
1778 err = got_error_from_errno("got_object_id_dup");
1782 if (got_object_id_cmp(*unique_id, &id) == 0)
1783 continue; /* both packed and loose */
1784 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1789 if (dir && closedir(dir) != 0 && err == NULL)
1790 err = got_error_from_errno("closedir");
1800 const struct got_error *
1801 got_repo_match_object_id_prefix(struct got_object_id **id,
1802 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1804 const struct got_error *err = NULL;
1805 char *path_objects = NULL, *object_dir = NULL;
1811 path_objects = got_repo_get_path_objects(repo);
1813 len = strlen(id_str_prefix);
1814 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1815 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1819 for (i = 0; i < len; i++) {
1820 if (isxdigit((unsigned char)id_str_prefix[i]))
1822 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1827 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1830 object_dir = strndup(id_str_prefix, 2);
1831 if (object_dir == NULL) {
1832 err = got_error_from_errno("strdup");
1835 err = match_loose_object(id, path_objects, object_dir,
1836 id_str_prefix, obj_type, repo);
1837 } else if (len == 1) {
1839 for (i = 0; i < 0xf; i++) {
1840 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1842 err = got_error_from_errno("asprintf");
1845 err = match_packed_object(id, repo, object_dir,
1849 err = match_loose_object(id, path_objects, object_dir,
1850 id_str_prefix, obj_type, repo);
1855 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1864 } else if (*id == NULL) {
1866 case GOT_OBJ_TYPE_BLOB:
1867 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1868 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1870 case GOT_OBJ_TYPE_TREE:
1871 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1872 GOT_OBJ_LABEL_TREE, id_str_prefix);
1874 case GOT_OBJ_TYPE_COMMIT:
1875 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1876 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1878 case GOT_OBJ_TYPE_TAG:
1879 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1880 GOT_OBJ_LABEL_TAG, id_str_prefix);
1883 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1891 const struct got_error *
1892 got_repo_match_object_id(struct got_object_id **id, char **label,
1893 const char *id_str, int obj_type, struct got_reflist_head *refs,
1894 struct got_repository *repo)
1896 const struct got_error *err;
1897 struct got_tag_object *tag;
1898 struct got_reference *ref = NULL;
1905 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1908 *id = got_object_id_dup(
1909 got_object_tag_get_object_id(tag));
1911 err = got_error_from_errno("got_object_id_dup");
1912 else if (label && asprintf(label, "refs/tags/%s",
1913 got_object_tag_get_name(tag)) == -1) {
1914 err = got_error_from_errno("asprintf");
1918 got_object_tag_close(tag);
1920 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1921 err->code != GOT_ERR_NO_OBJ)
1925 err = got_ref_open(&ref, repo, id_str, 0);
1927 err = got_ref_resolve(id, repo, ref);
1931 *label = strdup(got_ref_get_name(ref));
1932 if (*label == NULL) {
1933 err = got_error_from_errno("strdup");
1938 if (err->code != GOT_ERR_NOT_REF &&
1939 err->code != GOT_ERR_BAD_REF_NAME)
1941 err = got_repo_match_object_id_prefix(id, id_str,
1944 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1945 err = got_error_not_ref(id_str);
1949 err = got_object_id_str(label, *id);
1950 if (*label == NULL) {
1951 err = got_error_from_errno("strdup");
1962 const struct got_error *
1963 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1964 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_reflist_entry *re;
1968 struct got_object_id *tag_id;
1969 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1973 TAILQ_FOREACH(re, refs, entry) {
1974 const char *refname;
1975 refname = got_ref_get_name(re->ref);
1976 if (got_ref_is_symbolic(re->ref))
1978 if (strncmp(refname, "refs/tags/", 10) != 0)
1980 if (!name_is_absolute)
1981 refname += strlen("refs/tags/");
1982 if (strcmp(refname, name) != 0)
1984 err = got_ref_resolve(&tag_id, repo, re->ref);
1987 err = got_object_open_as_tag(tag, repo, tag_id);
1991 if (obj_type == GOT_OBJ_TYPE_ANY ||
1992 got_object_tag_get_object_type(*tag) == obj_type)
1994 got_object_tag_close(*tag);
1998 if (err == NULL && *tag == NULL)
1999 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2000 GOT_OBJ_LABEL_TAG, name);
2004 static const struct got_error *
2005 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2006 const char *name, mode_t mode, struct got_object_id *blob_id)
2008 const struct got_error *err = NULL;
2012 *new_te = calloc(1, sizeof(**new_te));
2013 if (*new_te == NULL)
2014 return got_error_from_errno("calloc");
2016 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2017 sizeof((*new_te)->name)) {
2018 err = got_error(GOT_ERR_NO_SPACE);
2022 if (S_ISLNK(mode)) {
2023 (*new_te)->mode = S_IFLNK;
2025 (*new_te)->mode = S_IFREG;
2026 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2028 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2030 if (err && *new_te) {
2037 static const struct got_error *
2038 import_file(struct got_tree_entry **new_te, struct dirent *de,
2039 const char *path, struct got_repository *repo)
2041 const struct got_error *err;
2042 struct got_object_id *blob_id = NULL;
2046 if (asprintf(&filepath, "%s%s%s", path,
2047 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2048 return got_error_from_errno("asprintf");
2050 if (lstat(filepath, &sb) != 0) {
2051 err = got_error_from_errno2("lstat", path);
2055 err = got_object_blob_create(&blob_id, filepath, repo);
2059 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2068 static const struct got_error *
2069 insert_tree_entry(struct got_tree_entry *new_te,
2070 struct got_pathlist_head *paths)
2072 const struct got_error *err = NULL;
2073 struct got_pathlist_entry *new_pe;
2075 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2079 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2083 static const struct got_error *write_tree(struct got_object_id **,
2084 const char *, struct got_pathlist_head *, struct got_repository *,
2085 got_repo_import_cb progress_cb, void *progress_arg);
2087 static const struct got_error *
2088 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2089 const char *path, struct got_pathlist_head *ignores,
2090 struct got_repository *repo,
2091 got_repo_import_cb progress_cb, void *progress_arg)
2093 const struct got_error *err;
2094 struct got_object_id *id = NULL;
2097 if (asprintf(&subdirpath, "%s%s%s", path,
2098 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2099 return got_error_from_errno("asprintf");
2101 (*new_te) = calloc(1, sizeof(**new_te));
2102 if (*new_te == NULL)
2103 return got_error_from_errno("calloc");
2104 (*new_te)->mode = S_IFDIR;
2105 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2106 sizeof((*new_te)->name)) {
2107 err = got_error(GOT_ERR_NO_SPACE);
2110 err = write_tree(&id, subdirpath, ignores, repo,
2111 progress_cb, progress_arg);
2114 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2126 static const struct got_error *
2127 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2128 struct got_pathlist_head *ignores, struct got_repository *repo,
2129 got_repo_import_cb progress_cb, void *progress_arg)
2131 const struct got_error *err = NULL;
2135 struct got_tree_entry *new_te = NULL;
2136 struct got_pathlist_head paths;
2137 struct got_pathlist_entry *pe;
2139 *new_tree_id = NULL;
2143 dir = opendir(path_dir);
2145 err = got_error_from_errno2("opendir", path_dir);
2150 while ((de = readdir(dir)) != NULL) {
2154 if (strcmp(de->d_name, ".") == 0 ||
2155 strcmp(de->d_name, "..") == 0)
2158 err = got_path_dirent_type(&type, path_dir, de);
2162 TAILQ_FOREACH(pe, ignores, entry) {
2163 if (type == DT_DIR && pe->path_len > 0 &&
2164 pe->path[pe->path_len - 1] == '/') {
2165 char stripped[PATH_MAX];
2167 if (strlcpy(stripped, pe->path,
2168 sizeof(stripped)) >= sizeof(stripped)) {
2169 err = got_error(GOT_ERR_NO_SPACE);
2172 got_path_strip_trailing_slashes(stripped);
2173 if (fnmatch(stripped, de->d_name, 0) == 0) {
2177 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2185 if (type == DT_DIR) {
2186 err = import_subdir(&new_te, de, path_dir,
2187 ignores, repo, progress_cb, progress_arg);
2189 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2194 } else if (type == DT_REG || type == DT_LNK) {
2195 err = import_file(&new_te, de, path_dir, repo);
2201 err = insert_tree_entry(new_te, &paths);
2207 if (TAILQ_EMPTY(&paths)) {
2208 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2209 "cannot create tree without any entries");
2213 TAILQ_FOREACH(pe, &paths, entry) {
2214 struct got_tree_entry *te = pe->data;
2216 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2218 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2219 err = got_error_from_errno("asprintf");
2222 err = (*progress_cb)(progress_arg, path);
2228 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2232 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2236 const struct got_error *
2237 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2238 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2239 struct got_repository *repo, got_repo_import_cb progress_cb,
2242 const struct got_error *err;
2243 struct got_object_id *new_tree_id;
2245 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2246 progress_cb, progress_arg);
2250 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2251 author, time(NULL), author, time(NULL), logmsg, repo);
2256 const struct got_error *
2257 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2258 struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 char *path_objects = NULL, *path = NULL;
2263 struct got_object_id id;
2269 path_objects = got_repo_get_path_objects(repo);
2270 if (path_objects == NULL)
2271 return got_error_from_errno("got_repo_get_path_objects");
2273 for (i = 0; i <= 0xff; i++) {
2274 struct dirent *dent;
2276 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2277 err = got_error_from_errno("asprintf");
2281 dir = opendir(path);
2283 if (errno == ENOENT) {
2287 err = got_error_from_errno2("opendir", path);
2291 while ((dent = readdir(dir)) != NULL) {
2296 if (strcmp(dent->d_name, ".") == 0 ||
2297 strcmp(dent->d_name, "..") == 0)
2300 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2301 err = got_error_from_errno("asprintf");
2305 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2311 err = got_object_open_loose_fd(&fd, &id, repo);
2315 if (fstat(fd, &sb) == -1) {
2316 err = got_error_from_errno("fstat");
2321 (*ondisk_size) += sb.st_size;
2323 if (close(fd) == -1) {
2324 err = got_error_from_errno("close");
2329 if (closedir(dir) != 0) {
2330 err = got_error_from_errno("closedir");
2339 if (dir && closedir(dir) != 0 && err == NULL)
2340 err = got_error_from_errno("closedir");
2351 const struct got_error *
2352 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2353 off_t *total_packsize, struct got_repository *repo)
2355 const struct got_error *err = NULL;
2356 DIR *packdir = NULL;
2357 struct dirent *dent;
2358 struct got_packidx *packidx = NULL;
2360 char *path_packfile;
2366 *total_packsize = 0;
2368 packdir_fd = openat(got_repo_get_fd(repo),
2369 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2370 if (packdir_fd == -1) {
2371 return got_error_from_errno_fmt("openat: %s/%s",
2372 got_repo_get_path_git_dir(repo),
2373 GOT_OBJECTS_PACK_DIR);
2376 packdir = fdopendir(packdir_fd);
2377 if (packdir == NULL) {
2378 err = got_error_from_errno("fdopendir");
2382 while ((dent = readdir(packdir)) != NULL) {
2383 if (!got_repo_is_packidx_filename(dent->d_name,
2384 strlen(dent->d_name)))
2387 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2388 dent->d_name) == -1) {
2389 err = got_error_from_errno("asprintf");
2393 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2399 if (fstat(packidx->fd, &sb) == -1)
2401 *total_packsize += sb.st_size;
2403 err = got_packidx_get_packfile_path(&path_packfile,
2404 packidx->path_packidx);
2408 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2410 free(path_packfile);
2413 free(path_packfile);
2414 *total_packsize += sb.st_size;
2416 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2420 got_packidx_close(packidx);
2425 got_packidx_close(packidx);
2426 if (packdir && closedir(packdir) != 0 && err == NULL)
2427 err = got_error_from_errno("closedir");
2431 *total_packsize = 0;
2436 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2437 got_packidx_bloom_filter_cmp);