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>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_cancel.h"
48 #include "got_object.h"
49 #include "got_opentemp.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_delta_cache.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_hash.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
62 #include "got_lib_gotconfig.h"
65 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
68 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
70 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
71 got_packidx_bloom_filter_cmp);
74 is_boolean_val(const char *val)
76 return (strcasecmp(val, "true") == 0 ||
77 strcasecmp(val, "false") == 0 ||
78 strcasecmp(val, "on") == 0 ||
79 strcasecmp(val, "off") == 0 ||
80 strcasecmp(val, "yes") == 0 ||
81 strcasecmp(val, "no") == 0 ||
82 strcasecmp(val, "1") == 0 ||
83 strcasecmp(val, "0") == 0);
87 get_boolean_val(const char *val)
89 return (strcasecmp(val, "true") == 0 ||
90 strcasecmp(val, "on") == 0 ||
91 strcasecmp(val, "yes") == 0 ||
92 strcasecmp(val, "1") == 0);
96 got_repo_get_path(struct got_repository *repo)
102 got_repo_get_path_git_dir(struct got_repository *repo)
104 return repo->path_git_dir;
108 got_repo_get_fd(struct got_repository *repo)
110 return repo->gitdir_fd;
114 got_repo_get_gitconfig_author_name(struct got_repository *repo)
116 return repo->gitconfig_author_name;
120 got_repo_get_gitconfig_author_email(struct got_repository *repo)
122 return repo->gitconfig_author_email;
126 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
128 return repo->global_gitconfig_author_name;
132 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
134 return repo->global_gitconfig_author_email;
138 got_repo_get_gitconfig_owner(struct got_repository *repo)
140 return repo->gitconfig_owner;
144 got_repo_has_extension(struct got_repository *repo, const char *ext)
148 for (i = 0; i < repo->nextensions; ++i) {
149 if (!strcasecmp(ext, repo->extnames[i]))
150 return get_boolean_val(repo->extvals[i]);
157 got_repo_is_bare(struct got_repository *repo)
159 return (strcmp(repo->path, repo->path_git_dir) == 0);
163 get_path_git_child(struct got_repository *repo, const char *basename)
167 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
175 got_repo_get_path_objects(struct got_repository *repo)
177 return get_path_git_child(repo, GOT_OBJECTS_DIR);
181 got_repo_get_path_objects_pack(struct got_repository *repo)
183 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
187 got_repo_get_path_refs(struct got_repository *repo)
189 return get_path_git_child(repo, GOT_REFS_DIR);
193 got_repo_get_path_packed_refs(struct got_repository *repo)
195 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
199 get_path_head(struct got_repository *repo)
201 return get_path_git_child(repo, GOT_HEAD_FILE);
205 got_repo_get_path_gitconfig(struct got_repository *repo)
207 return get_path_git_child(repo, GOT_GITCONFIG);
211 got_repo_get_path_gotconfig(struct got_repository *repo)
213 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
216 const struct got_gotconfig *
217 got_repo_get_gotconfig(struct got_repository *repo)
219 return repo->gotconfig;
223 got_repo_get_gitconfig_remotes(int *nremotes,
224 const struct got_remote_repo **remotes, struct got_repository *repo)
226 *nremotes = repo->ngitconfig_remotes;
227 *remotes = repo->gitconfig_remotes;
231 is_git_repo(struct got_repository *repo)
233 const char *path_git = got_repo_get_path_git_dir(repo);
234 char *path_objects = got_repo_get_path_objects(repo);
235 char *path_refs = got_repo_get_path_refs(repo);
236 char *path_head = get_path_head(repo);
239 struct got_reference *head_ref;
241 if (lstat(path_git, &sb) == -1)
243 if (!S_ISDIR(sb.st_mode))
246 if (lstat(path_objects, &sb) == -1)
248 if (!S_ISDIR(sb.st_mode))
251 if (lstat(path_refs, &sb) == -1)
253 if (!S_ISDIR(sb.st_mode))
256 if (lstat(path_head, &sb) == -1)
258 if (!S_ISREG(sb.st_mode))
261 /* Check if the HEAD reference can be opened. */
262 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
264 got_ref_close(head_ref);
275 static const struct got_error *
276 close_tempfiles(int *fds, size_t nfds)
278 const struct got_error *err = NULL;
281 for (i = 0; i < nfds; i++) {
284 if (close(fds[i]) == -1) {
285 err = got_error_from_errno("close");
293 static const struct got_error *
294 open_tempfiles(int **fds, size_t array_size, size_t nfds)
296 const struct got_error *err = NULL;
299 *fds = calloc(array_size, sizeof(**fds));
301 return got_error_from_errno("calloc");
303 for (i = 0; i < array_size; i++)
306 for (i = 0; i < nfds; i++) {
307 (*fds)[i] = got_opentempfd();
308 if ((*fds)[i] == -1) {
309 err = got_error_from_errno("got_opentempfd");
310 close_tempfiles(*fds, nfds);
319 static const struct got_error *
320 get_pack_cache_size(int *pack_cache_size)
324 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
325 return got_error_from_errno("getrlimit");
327 *pack_cache_size = GOT_PACK_CACHE_SIZE;
328 if (*pack_cache_size > rl.rlim_cur / 8)
329 *pack_cache_size = rl.rlim_cur / 8;
334 const struct got_error *
335 got_repo_pack_fds_open(int **pack_fds)
337 const struct got_error *err;
340 err = get_pack_cache_size(&nfds);
345 * We need one basefd and one accumfd per cached pack.
346 * Our constants should be set up in a way such that
347 * this error never triggers.
349 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
350 return got_error(GOT_ERR_NO_SPACE);
352 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
355 const struct got_error *
356 got_repo_pack_fds_close(int *pack_fds)
358 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
361 const struct got_error *
362 got_repo_temp_fds_open(int **temp_fds)
364 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
365 GOT_REPO_NUM_TEMPFILES);
369 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
373 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
374 repo->tempfiles[i] = temp_fds[i];
377 const struct got_error *
378 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
385 for (i = 0; i < nitems(repo->tempfiles); i++) {
386 if (repo->tempfile_use_mask & (1 << i))
388 if (repo->tempfiles[i] != -1) {
389 if (ftruncate(repo->tempfiles[i], 0L) == -1)
390 return got_error_from_errno("ftruncate");
391 *fd = repo->tempfiles[i];
393 repo->tempfile_use_mask |= (1 << i);
398 return got_error(GOT_ERR_REPO_TEMPFILE);
402 got_repo_temp_fds_put(int idx, struct got_repository *repo)
404 repo->tempfile_use_mask &= ~(1 << idx);
407 const struct got_error *
408 got_repo_temp_fds_close(int *temp_fds)
410 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
413 const struct got_error *
414 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
415 struct got_object *obj)
417 #ifndef GOT_NO_OBJ_CACHE
418 const struct got_error *err = NULL;
419 err = got_object_cache_add(&repo->objcache, id, obj);
421 if (err->code == GOT_ERR_OBJ_EXISTS ||
422 err->code == GOT_ERR_OBJ_TOO_LARGE)
432 got_repo_get_cached_object(struct got_repository *repo,
433 struct got_object_id *id)
435 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
438 const struct got_error *
439 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
440 struct got_tree_object *tree)
442 #ifndef GOT_NO_OBJ_CACHE
443 const struct got_error *err = NULL;
444 err = got_object_cache_add(&repo->treecache, id, tree);
446 if (err->code == GOT_ERR_OBJ_EXISTS ||
447 err->code == GOT_ERR_OBJ_TOO_LARGE)
456 struct got_tree_object *
457 got_repo_get_cached_tree(struct got_repository *repo,
458 struct got_object_id *id)
460 return (struct got_tree_object *)got_object_cache_get(
461 &repo->treecache, id);
464 const struct got_error *
465 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
466 struct got_commit_object *commit)
468 #ifndef GOT_NO_OBJ_CACHE
469 const struct got_error *err = NULL;
470 err = got_object_cache_add(&repo->commitcache, id, commit);
472 if (err->code == GOT_ERR_OBJ_EXISTS ||
473 err->code == GOT_ERR_OBJ_TOO_LARGE)
482 struct got_commit_object *
483 got_repo_get_cached_commit(struct got_repository *repo,
484 struct got_object_id *id)
486 return (struct got_commit_object *)got_object_cache_get(
487 &repo->commitcache, id);
490 const struct got_error *
491 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
492 struct got_tag_object *tag)
494 #ifndef GOT_NO_OBJ_CACHE
495 const struct got_error *err = NULL;
496 err = got_object_cache_add(&repo->tagcache, id, tag);
498 if (err->code == GOT_ERR_OBJ_EXISTS ||
499 err->code == GOT_ERR_OBJ_TOO_LARGE)
508 struct got_tag_object *
509 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
511 return (struct got_tag_object *)got_object_cache_get(
512 &repo->tagcache, id);
515 const struct got_error *
516 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
517 struct got_raw_object *raw)
519 #ifndef GOT_NO_OBJ_CACHE
520 const struct got_error *err = NULL;
521 err = got_object_cache_add(&repo->rawcache, id, raw);
523 if (err->code == GOT_ERR_OBJ_EXISTS ||
524 err->code == GOT_ERR_OBJ_TOO_LARGE)
534 struct got_raw_object *
535 got_repo_get_cached_raw_object(struct got_repository *repo,
536 struct got_object_id *id)
538 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
542 static const struct got_error *
543 open_repo(struct got_repository *repo, const char *path)
545 const struct got_error *err = NULL;
547 repo->gitdir_fd = -1;
549 /* bare git repository? */
550 repo->path_git_dir = strdup(path);
551 if (repo->path_git_dir == NULL)
552 return got_error_from_errno("strdup");
553 if (is_git_repo(repo)) {
554 repo->path = strdup(repo->path_git_dir);
555 if (repo->path == NULL) {
556 err = got_error_from_errno("strdup");
559 repo->gitdir_fd = open(repo->path_git_dir,
560 O_DIRECTORY | O_CLOEXEC);
561 if (repo->gitdir_fd == -1) {
562 err = got_error_from_errno2("open",
569 /* git repository with working tree? */
570 free(repo->path_git_dir);
571 repo->path_git_dir = NULL;
572 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
573 err = got_error_from_errno("asprintf");
576 if (is_git_repo(repo)) {
577 repo->path = strdup(path);
578 if (repo->path == NULL) {
579 err = got_error_from_errno("strdup");
582 repo->gitdir_fd = open(repo->path_git_dir,
583 O_DIRECTORY | O_CLOEXEC);
584 if (repo->gitdir_fd == -1) {
585 err = got_error_from_errno2("open",
592 err = got_error(GOT_ERR_NOT_GIT_REPO);
597 free(repo->path_git_dir);
598 repo->path_git_dir = NULL;
599 if (repo->gitdir_fd != -1)
600 close(repo->gitdir_fd);
601 repo->gitdir_fd = -1;
607 static const struct got_error *
608 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
610 const struct got_error *err = NULL;
611 char *repo_gitconfig_path = NULL;
613 if (global_gitconfig_path) {
614 /* Read settings from ~/.gitconfig. */
615 int dummy_repo_version;
616 err = got_repo_read_gitconfig(&dummy_repo_version,
617 &repo->global_gitconfig_author_name,
618 &repo->global_gitconfig_author_email,
619 NULL, NULL, NULL, NULL, NULL, NULL,
620 global_gitconfig_path);
625 /* Read repository's .git/config file. */
626 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
627 if (repo_gitconfig_path == NULL)
628 return got_error_from_errno("got_repo_get_path_gitconfig");
630 err = got_repo_read_gitconfig(
631 &repo->gitconfig_repository_format_version,
632 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
633 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
634 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
635 &repo->nextensions, repo_gitconfig_path);
639 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
642 for (i = 0; i < repo->ngitconfig_remotes; i++) {
643 got_repo_free_remote_repo_data(
644 &repo->gitconfig_remotes[i]);
646 free(repo->gitconfig_remotes);
647 repo->gitconfig_remotes = NULL;
648 repo->ngitconfig_remotes = 0;
650 free(repo->gitconfig_author_name);
651 repo->gitconfig_author_name = NULL;
652 free(repo->gitconfig_author_email);
653 repo->gitconfig_author_email = NULL;
655 free(repo->global_gitconfig_author_name);
656 repo->global_gitconfig_author_name = NULL;
657 free(repo->global_gitconfig_author_email);
658 repo->global_gitconfig_author_email = NULL;
662 free(repo_gitconfig_path);
666 static const struct got_error *
667 read_gotconfig(struct got_repository *repo)
669 const struct got_error *err = NULL;
670 char *gotconfig_path;
672 gotconfig_path = got_repo_get_path_gotconfig(repo);
673 if (gotconfig_path == NULL)
674 return got_error_from_errno("got_repo_get_path_gotconfig");
676 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
677 free(gotconfig_path);
681 /* Supported repository format extensions. */
682 static const char *const repo_extensions[] = {
683 "noop", /* Got supports repository format version 1. */
684 "preciousObjects", /* Supported by gotadmin cleanup. */
685 "worktreeConfig", /* Got does not care about Git work trees. */
688 const struct got_error *
689 got_repo_open(struct got_repository **repop, const char *path,
690 const char *global_gitconfig_path, int *pack_fds)
692 struct got_repository *repo = NULL;
693 const struct got_error *err = NULL;
694 char *repo_path = NULL;
699 repo = calloc(1, sizeof(*repo));
701 return got_error_from_errno("calloc");
703 RB_INIT(&repo->packidx_bloom_filters);
704 TAILQ_INIT(&repo->packidx_paths);
706 for (i = 0; i < nitems(repo->privsep_children); i++) {
707 memset(&repo->privsep_children[i], 0,
708 sizeof(repo->privsep_children[0]));
709 repo->privsep_children[i].imsg_fd = -1;
712 err = got_object_cache_init(&repo->objcache,
713 GOT_OBJECT_CACHE_TYPE_OBJ);
716 err = got_object_cache_init(&repo->treecache,
717 GOT_OBJECT_CACHE_TYPE_TREE);
720 err = got_object_cache_init(&repo->commitcache,
721 GOT_OBJECT_CACHE_TYPE_COMMIT);
724 err = got_object_cache_init(&repo->tagcache,
725 GOT_OBJECT_CACHE_TYPE_TAG);
728 err = got_object_cache_init(&repo->rawcache,
729 GOT_OBJECT_CACHE_TYPE_RAW);
733 err = get_pack_cache_size(&repo->pack_cache_size);
736 for (i = 0; i < nitems(repo->packs); i++) {
737 if (pack_fds != NULL && i < repo->pack_cache_size) {
738 repo->packs[i].basefd = pack_fds[j++];
739 repo->packs[i].accumfd = pack_fds[j++];
741 repo->packs[i].basefd = -1;
742 repo->packs[i].accumfd = -1;
745 for (i = 0; i < nitems(repo->tempfiles); i++)
746 repo->tempfiles[i] = -1;
747 repo->pinned_pack = -1;
748 repo->pinned_packidx = -1;
749 repo->pinned_pid = 0;
751 repo_path = realpath(path, NULL);
752 if (repo_path == NULL) {
753 err = got_error_from_errno2("realpath", path);
760 err = open_repo(repo, repo_path);
763 if (err->code != GOT_ERR_NOT_GIT_REPO)
765 if (repo_path[0] == '/' && repo_path[1] == '\0') {
766 err = got_error(GOT_ERR_NOT_GIT_REPO);
769 err = got_path_dirname(&parent_path, repo_path);
773 repo_path = parent_path;
776 err = read_gotconfig(repo);
780 err = read_gitconfig(repo, global_gitconfig_path);
783 if (repo->gitconfig_repository_format_version != 0) {
784 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
787 for (i = 0; i < repo->nextensions; i++) {
788 char *ext = repo->extnames[i];
789 char *val = repo->extvals[i];
790 int j, supported = 0;
792 if (!is_boolean_val(val)) {
793 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
797 if (!get_boolean_val(val))
800 for (j = 0; j < nitems(repo_extensions); j++) {
801 if (strcmp(ext, repo_extensions[j]) == 0) {
807 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
812 err = got_repo_list_packidx(&repo->packidx_paths, repo);
815 got_repo_close(repo);
822 const struct got_error *
823 got_repo_close(struct got_repository *repo)
825 const struct got_error *err = NULL, *child_err;
826 struct got_packidx_bloom_filter *bf;
829 for (i = 0; i < repo->pack_cache_size; i++) {
830 if (repo->packidx_cache[i] == NULL)
832 got_packidx_close(repo->packidx_cache[i]);
835 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
836 &repo->packidx_bloom_filters))) {
837 RB_REMOVE(got_packidx_bloom_filter_tree,
838 &repo->packidx_bloom_filters, bf);
839 bloom_free(bf->bloom);
844 for (i = 0; i < repo->pack_cache_size; i++)
845 if (repo->packs[i].path_packfile)
846 if (repo->packs[i].path_packfile)
847 got_pack_close(&repo->packs[i]);
850 free(repo->path_git_dir);
852 got_object_cache_close(&repo->objcache);
853 got_object_cache_close(&repo->treecache);
854 got_object_cache_close(&repo->commitcache);
855 got_object_cache_close(&repo->tagcache);
856 got_object_cache_close(&repo->rawcache);
858 for (i = 0; i < nitems(repo->privsep_children); i++) {
859 if (repo->privsep_children[i].imsg_fd == -1)
861 imsg_clear(repo->privsep_children[i].ibuf);
862 free(repo->privsep_children[i].ibuf);
863 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
864 child_err = got_privsep_wait_for_child(
865 repo->privsep_children[i].pid);
866 if (child_err && err == NULL)
868 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
870 err = got_error_from_errno("close");
873 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
875 err = got_error_from_errno("close");
878 got_gotconfig_free(repo->gotconfig);
879 free(repo->gitconfig_author_name);
880 free(repo->gitconfig_author_email);
881 for (i = 0; i < repo->ngitconfig_remotes; i++)
882 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
883 free(repo->gitconfig_remotes);
884 for (i = 0; i < repo->nextensions; i++) {
885 free(repo->extnames[i]);
886 free(repo->extvals[i]);
888 free(repo->extnames);
891 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
898 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
904 free(repo->fetch_url);
905 repo->fetch_url = NULL;
906 free(repo->send_url);
907 repo->send_url = NULL;
908 for (i = 0; i < repo->nfetch_branches; i++)
909 free(repo->fetch_branches[i]);
910 free(repo->fetch_branches);
911 repo->fetch_branches = NULL;
912 repo->nfetch_branches = 0;
913 for (i = 0; i < repo->nsend_branches; i++)
914 free(repo->send_branches[i]);
915 free(repo->send_branches);
916 repo->send_branches = NULL;
917 repo->nsend_branches = 0;
920 const struct got_error *
921 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
922 const char *input_path)
924 const struct got_error *err = NULL;
925 const char *repo_abspath = NULL;
927 char *canonpath, *path = NULL;
929 *in_repo_path = NULL;
931 canonpath = strdup(input_path);
932 if (canonpath == NULL) {
933 err = got_error_from_errno("strdup");
936 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
940 repo_abspath = got_repo_get_path(repo);
942 if (canonpath[0] == '\0') {
943 path = strdup(canonpath);
945 err = got_error_from_errno("strdup");
949 path = realpath(canonpath, NULL);
951 if (errno != ENOENT) {
952 err = got_error_from_errno2("realpath",
957 * Path is not on disk.
958 * Assume it is already relative to repository root.
960 path = strdup(canonpath);
962 err = got_error_from_errno("strdup");
967 repolen = strlen(repo_abspath);
971 if (strcmp(path, repo_abspath) == 0) {
975 err = got_error_from_errno("strdup");
978 } else if (len > repolen &&
979 got_path_is_child(path, repo_abspath, repolen)) {
980 /* Matched an on-disk path inside repository. */
981 if (got_repo_is_bare(repo)) {
983 * Matched an on-disk path inside repository
984 * database. Treat input as repository-relative.
991 /* Strip common prefix with repository path. */
992 err = got_path_skip_common_ancestor(&child,
1001 * Matched unrelated on-disk path.
1002 * Treat input as repository-relative.
1010 /* Make in-repository path absolute */
1011 if (path[0] != '/') {
1013 if (asprintf(&abspath, "/%s", path) == -1) {
1014 err = got_error_from_errno("asprintf");
1026 *in_repo_path = path;
1030 static const struct got_error *
1031 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1032 const char *path_packidx)
1034 const struct got_error *err = NULL;
1037 for (i = 0; i < repo->pack_cache_size; i++) {
1038 if (repo->packidx_cache[i] == NULL)
1040 if (strcmp(repo->packidx_cache[i]->path_packidx,
1041 path_packidx) == 0) {
1042 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1045 if (i == repo->pack_cache_size) {
1048 } while (i > 0 && repo->pinned_packidx >= 0 &&
1049 i == repo->pinned_packidx);
1050 err = got_packidx_close(repo->packidx_cache[i]);
1055 repo->packidx_cache[i] = packidx;
1061 got_repo_is_packidx_filename(const char *name, size_t len)
1063 if (len != GOT_PACKIDX_NAMELEN)
1066 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1069 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1070 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1076 static struct got_packidx_bloom_filter *
1077 get_packidx_bloom_filter(struct got_repository *repo,
1078 const char *path, size_t path_len)
1080 struct got_packidx_bloom_filter key;
1082 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1083 return NULL; /* XXX */
1084 key.path_len = path_len;
1086 return RB_FIND(got_packidx_bloom_filter_tree,
1087 &repo->packidx_bloom_filters, &key);
1091 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1092 const char *path_packidx, struct got_object_id *id)
1094 struct got_packidx_bloom_filter *bf;
1096 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1098 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1100 /* No bloom filter means this pack index must be searched. */
1104 static const struct got_error *
1105 add_packidx_bloom_filter(struct got_repository *repo,
1106 struct got_packidx *packidx, const char *path_packidx)
1108 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1109 struct got_packidx_bloom_filter *bf;
1113 * Don't use bloom filters for very large pack index files.
1114 * Large pack files will contain a relatively large fraction
1115 * of our objects so we will likely need to visit them anyway.
1116 * The more objects a pack file contains the higher the probability
1117 * of a false-positive match from the bloom filter. And reading
1118 * all object IDs from a large pack index file can be expensive.
1120 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1123 /* Do we already have a filter for this pack index? */
1124 if (get_packidx_bloom_filter(repo, path_packidx,
1125 strlen(path_packidx)) != NULL)
1128 bf = calloc(1, sizeof(*bf));
1130 return got_error_from_errno("calloc");
1131 bf->bloom = calloc(1, sizeof(*bf->bloom));
1132 if (bf->bloom == NULL) {
1134 return got_error_from_errno("calloc");
1137 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1138 if (len >= sizeof(bf->path)) {
1141 return got_error(GOT_ERR_NO_SPACE);
1145 /* Minimum size supported by our bloom filter is 1000 entries. */
1146 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1147 for (i = 0; i < nobjects; i++) {
1148 struct got_packidx_object_id *id;
1149 id = &packidx->hdr.sorted_ids[i];
1150 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1153 RB_INSERT(got_packidx_bloom_filter_tree,
1154 &repo->packidx_bloom_filters, bf);
1159 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1161 struct got_pathlist_entry *pe;
1163 while (!TAILQ_EMPTY(packidx_paths)) {
1164 pe = TAILQ_FIRST(packidx_paths);
1165 TAILQ_REMOVE(packidx_paths, pe, entry);
1166 free((char *)pe->path);
1171 static const struct got_error *
1172 refresh_packidx_paths(struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 char *objects_pack_dir = NULL;
1178 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1179 if (objects_pack_dir == NULL)
1180 return got_error_from_errno("got_repo_get_path_objects_pack");
1182 if (stat(objects_pack_dir, &sb) == -1) {
1183 if (errno != ENOENT) {
1184 err = got_error_from_errno2("stat", objects_pack_dir);
1187 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1188 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1189 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1190 purge_packidx_paths(&repo->packidx_paths);
1191 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1196 free(objects_pack_dir);
1200 const struct got_error *
1201 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1202 struct got_repository *repo, struct got_object_id *id)
1204 const struct got_error *err;
1205 struct got_pathlist_entry *pe;
1208 /* Search pack index cache. */
1209 for (i = 0; i < repo->pack_cache_size; i++) {
1210 if (repo->packidx_cache[i] == NULL)
1212 if (!got_repo_check_packidx_bloom_filter(repo,
1213 repo->packidx_cache[i]->path_packidx, id))
1214 continue; /* object will not be found in this index */
1215 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1217 *packidx = repo->packidx_cache[i];
1219 * Move this cache entry to the front. Repeatedly
1220 * searching a wrong pack index can be expensive.
1223 memmove(&repo->packidx_cache[1],
1224 &repo->packidx_cache[0],
1225 i * sizeof(repo->packidx_cache[0]));
1226 repo->packidx_cache[0] = *packidx;
1227 if (repo->pinned_packidx >= 0 &&
1228 repo->pinned_packidx < i)
1229 repo->pinned_packidx++;
1230 else if (repo->pinned_packidx == i)
1231 repo->pinned_packidx = 0;
1236 /* No luck. Search the filesystem. */
1238 err = refresh_packidx_paths(repo);
1242 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1243 const char *path_packidx = pe->path;
1246 if (!got_repo_check_packidx_bloom_filter(repo,
1248 continue; /* object will not be found in this index */
1250 for (i = 0; i < repo->pack_cache_size; i++) {
1251 if (repo->packidx_cache[i] == NULL)
1253 if (strcmp(repo->packidx_cache[i]->path_packidx,
1254 path_packidx) == 0) {
1260 continue; /* already searched */
1262 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1267 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1271 err = cache_packidx(repo, *packidx, path_packidx);
1275 *idx = got_packidx_get_object_idx(*packidx, id);
1277 err = NULL; /* found the object */
1282 err = got_error_no_obj(id);
1287 const struct got_error *
1288 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1289 struct got_repository *repo)
1291 const struct got_error *err = NULL;
1292 DIR *packdir = NULL;
1293 struct dirent *dent;
1294 char *path_packidx = NULL;
1298 packdir_fd = openat(got_repo_get_fd(repo),
1299 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1300 if (packdir_fd == -1) {
1301 return got_error_from_errno_fmt("openat: %s/%s",
1302 got_repo_get_path_git_dir(repo),
1303 GOT_OBJECTS_PACK_DIR);
1306 packdir = fdopendir(packdir_fd);
1307 if (packdir == NULL) {
1308 err = got_error_from_errno("fdopendir");
1312 if (fstat(packdir_fd, &sb) == -1) {
1313 err = got_error_from_errno("fstat");
1316 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1317 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1319 while ((dent = readdir(packdir)) != NULL) {
1320 if (!got_repo_is_packidx_filename(dent->d_name,
1321 strlen(dent->d_name)))
1324 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1325 dent->d_name) == -1) {
1326 err = got_error_from_errno("asprintf");
1327 path_packidx = NULL;
1331 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1338 if (packdir && closedir(packdir) != 0 && err == NULL)
1339 err = got_error_from_errno("closedir");
1343 const struct got_error *
1344 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1345 struct got_repository *repo)
1347 const struct got_error *err;
1352 /* Search pack index cache. */
1353 for (i = 0; i < repo->pack_cache_size; i++) {
1354 if (repo->packidx_cache[i] == NULL)
1356 if (strcmp(repo->packidx_cache[i]->path_packidx,
1357 path_packidx) == 0) {
1358 *packidx = repo->packidx_cache[i];
1362 /* No luck. Search the filesystem. */
1364 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1369 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1373 err = cache_packidx(repo, *packidx, path_packidx);
1376 got_packidx_close(*packidx);
1382 static const struct got_error *
1383 read_packfile_hdr(int fd, struct got_packidx *packidx)
1385 const struct got_error *err = NULL;
1386 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1387 struct got_packfile_hdr hdr;
1390 n = read(fd, &hdr, sizeof(hdr));
1392 return got_error_from_errno("read");
1393 if (n != sizeof(hdr))
1394 return got_error(GOT_ERR_BAD_PACKFILE);
1396 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1397 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1398 be32toh(hdr.nobjects) != totobj)
1399 err = got_error(GOT_ERR_BAD_PACKFILE);
1404 static const struct got_error *
1405 open_packfile(int *fd, struct got_repository *repo,
1406 const char *relpath, struct got_packidx *packidx)
1408 const struct got_error *err = NULL;
1410 *fd = openat(got_repo_get_fd(repo), relpath,
1411 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1413 return got_error_from_errno_fmt("openat: %s/%s",
1414 got_repo_get_path_git_dir(repo), relpath);
1417 err = read_packfile_hdr(*fd, packidx);
1427 const struct got_error *
1428 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1429 const char *path_packfile, struct got_packidx *packidx)
1431 const struct got_error *err = NULL;
1432 struct got_pack *pack = NULL;
1439 for (i = 0; i < repo->pack_cache_size; i++) {
1440 pack = &repo->packs[i];
1441 if (pack->path_packfile == NULL)
1443 if (strcmp(pack->path_packfile, path_packfile) == 0)
1444 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1447 if (i == repo->pack_cache_size) {
1448 struct got_pack tmp;
1451 } while (i > 0 && repo->pinned_pack >= 0 &&
1452 i == repo->pinned_pack);
1453 err = got_pack_close(&repo->packs[i]);
1456 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1457 return got_error_from_errno("ftruncate");
1458 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1459 return got_error_from_errno("ftruncate");
1460 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1461 memcpy(&repo->packs[i], &repo->packs[0],
1462 sizeof(repo->packs[i]));
1463 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1464 if (repo->pinned_pack == 0)
1465 repo->pinned_pack = i;
1466 else if (repo->pinned_pack == i)
1467 repo->pinned_pack = 0;
1471 pack = &repo->packs[i];
1473 pack->path_packfile = strdup(path_packfile);
1474 if (pack->path_packfile == NULL) {
1475 err = got_error_from_errno("strdup");
1479 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1483 if (fstat(pack->fd, &sb) != 0) {
1484 err = got_error_from_errno("fstat");
1487 pack->filesize = sb.st_size;
1489 pack->privsep_child = NULL;
1491 err = got_delta_cache_alloc(&pack->delta_cache);
1495 #ifndef GOT_PACK_NO_MMAP
1496 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1497 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1499 if (pack->map == MAP_FAILED) {
1500 if (errno != ENOMEM) {
1501 err = got_error_from_errno("mmap");
1504 pack->map = NULL; /* fall back to read(2) */
1511 got_pack_close(pack);
1518 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1520 struct got_pack *pack = NULL;
1523 for (i = 0; i < repo->pack_cache_size; i++) {
1524 pack = &repo->packs[i];
1525 if (pack->path_packfile == NULL)
1527 if (strcmp(pack->path_packfile, path_packfile) == 0)
1534 const struct got_error *
1535 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1536 struct got_pack *pack)
1539 int pinned_pack = -1, pinned_packidx = -1;
1541 for (i = 0; i < repo->pack_cache_size; i++) {
1542 if (repo->packidx_cache[i] &&
1543 strcmp(repo->packidx_cache[i]->path_packidx,
1544 packidx->path_packidx) == 0)
1546 if (repo->packs[i].path_packfile &&
1547 strcmp(repo->packs[i].path_packfile,
1548 pack->path_packfile) == 0)
1552 if (pinned_packidx == -1 || pinned_pack == -1)
1553 return got_error(GOT_ERR_PIN_PACK);
1555 repo->pinned_pack = pinned_pack;
1556 repo->pinned_packidx = pinned_packidx;
1557 if (repo->packs[pinned_pack].privsep_child)
1558 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1563 got_repo_get_pinned_pack(struct got_repository *repo)
1565 if (repo->pinned_pack >= 0 &&
1566 repo->pinned_pack < repo->pack_cache_size)
1567 return &repo->packs[repo->pinned_pack];
1573 got_repo_unpin_pack(struct got_repository *repo)
1575 repo->pinned_packidx = -1;
1576 repo->pinned_pack = -1;
1577 repo->pinned_pid = 0;
1580 const struct got_error *
1581 got_repo_init(const char *repo_path, const char *head_name)
1583 const struct got_error *err = NULL;
1584 const char *dirnames[] = {
1586 GOT_OBJECTS_PACK_DIR,
1589 const char *description_str = "Unnamed repository; "
1590 "edit this file 'description' to name the repository.";
1591 const char *headref = "ref: refs/heads/";
1592 const char *gitconfig_str = "[core]\n"
1593 "\trepositoryformatversion = 0\n"
1594 "\tfilemode = true\n"
1596 char *headref_str, *path;
1599 if (!got_path_dir_is_empty(repo_path))
1600 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1602 for (i = 0; i < nitems(dirnames); i++) {
1603 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1604 return got_error_from_errno("asprintf");
1606 err = got_path_mkdir(path);
1612 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1613 return got_error_from_errno("asprintf");
1614 err = got_path_create_file(path, description_str);
1619 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1620 return got_error_from_errno("asprintf");
1621 if (asprintf(&headref_str, "%s%s", headref,
1622 head_name ? head_name : "main") == -1) {
1624 return got_error_from_errno("asprintf");
1626 err = got_path_create_file(path, headref_str);
1632 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1633 return got_error_from_errno("asprintf");
1634 err = got_path_create_file(path, gitconfig_str);
1642 static const struct got_error *
1643 match_packed_object(struct got_object_id **unique_id,
1644 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1646 const struct got_error *err = NULL;
1647 struct got_object_id_queue matched_ids;
1648 struct got_pathlist_entry *pe;
1650 STAILQ_INIT(&matched_ids);
1652 err = refresh_packidx_paths(repo);
1656 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1657 const char *path_packidx = pe->path;
1658 struct got_packidx *packidx;
1659 struct got_object_qid *qid;
1661 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1666 err = got_packidx_match_id_str_prefix(&matched_ids,
1667 packidx, id_str_prefix);
1669 got_packidx_close(packidx);
1672 err = got_packidx_close(packidx);
1676 STAILQ_FOREACH(qid, &matched_ids, entry) {
1677 if (obj_type != GOT_OBJ_TYPE_ANY) {
1679 err = got_object_get_type(&matched_type, repo,
1683 if (matched_type != obj_type)
1686 if (*unique_id == NULL) {
1687 *unique_id = got_object_id_dup(&qid->id);
1688 if (*unique_id == NULL) {
1689 err = got_error_from_errno("malloc");
1693 if (got_object_id_cmp(*unique_id,
1695 continue; /* packed multiple times */
1696 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1702 got_object_id_queue_free(&matched_ids);
1710 static const struct got_error *
1711 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1712 const char *object_dir, const char *id_str_prefix, int obj_type,
1713 struct got_repository *repo)
1715 const struct got_error *err = NULL;
1716 char *path, *id_str = NULL;
1718 struct dirent *dent;
1719 struct got_object_id id;
1721 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1722 err = got_error_from_errno("asprintf");
1726 dir = opendir(path);
1728 if (errno == ENOENT) {
1732 err = got_error_from_errno2("opendir", path);
1735 while ((dent = readdir(dir)) != NULL) {
1737 enum got_hash_algorithm algo = GOT_HASH_SHA1;
1742 if (strcmp(dent->d_name, ".") == 0 ||
1743 strcmp(dent->d_name, "..") == 0)
1746 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1747 err = got_error_from_errno("asprintf");
1751 if (!got_parse_object_id(&id, id_str, algo))
1755 * Directory entries do not necessarily appear in
1756 * sorted order, so we must iterate over all of them.
1758 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1762 if (*unique_id == NULL) {
1763 if (obj_type != GOT_OBJ_TYPE_ANY) {
1765 err = got_object_get_type(&matched_type, repo,
1769 if (matched_type != obj_type)
1772 *unique_id = got_object_id_dup(&id);
1773 if (*unique_id == NULL) {
1774 err = got_error_from_errno("got_object_id_dup");
1778 if (got_object_id_cmp(*unique_id, &id) == 0)
1779 continue; /* both packed and loose */
1780 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1785 if (dir && closedir(dir) != 0 && err == NULL)
1786 err = got_error_from_errno("closedir");
1796 const struct got_error *
1797 got_repo_match_object_id_prefix(struct got_object_id **id,
1798 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1800 const struct got_error *err = NULL;
1801 char *path_objects = NULL, *object_dir = NULL;
1807 path_objects = got_repo_get_path_objects(repo);
1809 len = strlen(id_str_prefix);
1810 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1811 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1815 for (i = 0; i < len; i++) {
1816 if (isxdigit((unsigned char)id_str_prefix[i]))
1818 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1823 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1826 object_dir = strndup(id_str_prefix, 2);
1827 if (object_dir == NULL) {
1828 err = got_error_from_errno("strdup");
1831 err = match_loose_object(id, path_objects, object_dir,
1832 id_str_prefix, obj_type, repo);
1833 } else if (len == 1) {
1835 for (i = 0; i < 0xf; i++) {
1836 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1838 err = got_error_from_errno("asprintf");
1841 err = match_packed_object(id, repo, object_dir,
1845 err = match_loose_object(id, path_objects, object_dir,
1846 id_str_prefix, obj_type, repo);
1851 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1860 } else if (*id == NULL) {
1862 case GOT_OBJ_TYPE_BLOB:
1863 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1864 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1866 case GOT_OBJ_TYPE_TREE:
1867 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1868 GOT_OBJ_LABEL_TREE, id_str_prefix);
1870 case GOT_OBJ_TYPE_COMMIT:
1871 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1872 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1874 case GOT_OBJ_TYPE_TAG:
1875 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1876 GOT_OBJ_LABEL_TAG, id_str_prefix);
1879 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1887 const struct got_error *
1888 got_repo_match_object_id(struct got_object_id **id, char **label,
1889 const char *id_str, int obj_type, struct got_reflist_head *refs,
1890 struct got_repository *repo)
1892 const struct got_error *err;
1893 struct got_tag_object *tag;
1894 struct got_reference *ref = NULL;
1901 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1904 *id = got_object_id_dup(
1905 got_object_tag_get_object_id(tag));
1907 err = got_error_from_errno("got_object_id_dup");
1908 else if (label && asprintf(label, "refs/tags/%s",
1909 got_object_tag_get_name(tag)) == -1) {
1910 err = got_error_from_errno("asprintf");
1914 got_object_tag_close(tag);
1916 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1917 err->code != GOT_ERR_NO_OBJ)
1921 err = got_ref_open(&ref, repo, id_str, 0);
1923 err = got_ref_resolve(id, repo, ref);
1927 *label = strdup(got_ref_get_name(ref));
1928 if (*label == NULL) {
1929 err = got_error_from_errno("strdup");
1934 if (err->code != GOT_ERR_NOT_REF &&
1935 err->code != GOT_ERR_BAD_REF_NAME)
1937 err = got_repo_match_object_id_prefix(id, id_str,
1940 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1941 err = got_error_not_ref(id_str);
1945 err = got_object_id_str(label, *id);
1946 if (*label == NULL) {
1947 err = got_error_from_errno("strdup");
1958 const struct got_error *
1959 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1960 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1962 const struct got_error *err = NULL;
1963 struct got_reflist_entry *re;
1964 struct got_object_id *tag_id;
1965 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1969 TAILQ_FOREACH(re, refs, entry) {
1970 const char *refname;
1971 refname = got_ref_get_name(re->ref);
1972 if (got_ref_is_symbolic(re->ref))
1974 if (strncmp(refname, "refs/tags/", 10) != 0)
1976 if (!name_is_absolute)
1977 refname += strlen("refs/tags/");
1978 if (strcmp(refname, name) != 0)
1980 err = got_ref_resolve(&tag_id, repo, re->ref);
1983 err = got_object_open_as_tag(tag, repo, tag_id);
1987 if (obj_type == GOT_OBJ_TYPE_ANY ||
1988 got_object_tag_get_object_type(*tag) == obj_type)
1990 got_object_tag_close(*tag);
1994 if (err == NULL && *tag == NULL)
1995 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1996 GOT_OBJ_LABEL_TAG, name);
2000 static const struct got_error *
2001 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2002 const char *name, mode_t mode, struct got_object_id *blob_id)
2004 const struct got_error *err = NULL;
2008 *new_te = calloc(1, sizeof(**new_te));
2009 if (*new_te == NULL)
2010 return got_error_from_errno("calloc");
2012 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2013 sizeof((*new_te)->name)) {
2014 err = got_error(GOT_ERR_NO_SPACE);
2018 if (S_ISLNK(mode)) {
2019 (*new_te)->mode = S_IFLNK;
2021 (*new_te)->mode = S_IFREG;
2022 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2024 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2026 if (err && *new_te) {
2033 static const struct got_error *
2034 import_file(struct got_tree_entry **new_te, struct dirent *de,
2035 const char *path, struct got_repository *repo)
2037 const struct got_error *err;
2038 struct got_object_id *blob_id = NULL;
2042 if (asprintf(&filepath, "%s%s%s", path,
2043 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2044 return got_error_from_errno("asprintf");
2046 if (lstat(filepath, &sb) != 0) {
2047 err = got_error_from_errno2("lstat", path);
2051 err = got_object_blob_create(&blob_id, filepath, repo);
2055 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2064 static const struct got_error *
2065 insert_tree_entry(struct got_tree_entry *new_te,
2066 struct got_pathlist_head *paths)
2068 const struct got_error *err = NULL;
2069 struct got_pathlist_entry *new_pe;
2071 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2075 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2079 static const struct got_error *write_tree(struct got_object_id **,
2080 const char *, struct got_pathlist_head *, struct got_repository *,
2081 got_repo_import_cb progress_cb, void *progress_arg);
2083 static const struct got_error *
2084 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2085 const char *path, struct got_pathlist_head *ignores,
2086 struct got_repository *repo,
2087 got_repo_import_cb progress_cb, void *progress_arg)
2089 const struct got_error *err;
2090 struct got_object_id *id = NULL;
2093 if (asprintf(&subdirpath, "%s%s%s", path,
2094 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2095 return got_error_from_errno("asprintf");
2097 (*new_te) = calloc(1, sizeof(**new_te));
2098 if (*new_te == NULL)
2099 return got_error_from_errno("calloc");
2100 (*new_te)->mode = S_IFDIR;
2101 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2102 sizeof((*new_te)->name)) {
2103 err = got_error(GOT_ERR_NO_SPACE);
2106 err = write_tree(&id, subdirpath, ignores, repo,
2107 progress_cb, progress_arg);
2110 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2122 static const struct got_error *
2123 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2124 struct got_pathlist_head *ignores, struct got_repository *repo,
2125 got_repo_import_cb progress_cb, void *progress_arg)
2127 const struct got_error *err = NULL;
2131 struct got_tree_entry *new_te = NULL;
2132 struct got_pathlist_head paths;
2133 struct got_pathlist_entry *pe;
2135 *new_tree_id = NULL;
2139 dir = opendir(path_dir);
2141 err = got_error_from_errno2("opendir", path_dir);
2146 while ((de = readdir(dir)) != NULL) {
2150 if (strcmp(de->d_name, ".") == 0 ||
2151 strcmp(de->d_name, "..") == 0)
2154 err = got_path_dirent_type(&type, path_dir, de);
2158 TAILQ_FOREACH(pe, ignores, entry) {
2159 if (type == DT_DIR && pe->path_len > 0 &&
2160 pe->path[pe->path_len - 1] == '/') {
2161 char stripped[PATH_MAX];
2163 if (strlcpy(stripped, pe->path,
2164 sizeof(stripped)) >= sizeof(stripped)) {
2165 err = got_error(GOT_ERR_NO_SPACE);
2168 got_path_strip_trailing_slashes(stripped);
2169 if (fnmatch(stripped, de->d_name, 0) == 0) {
2173 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2181 if (type == DT_DIR) {
2182 err = import_subdir(&new_te, de, path_dir,
2183 ignores, repo, progress_cb, progress_arg);
2185 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2190 } else if (type == DT_REG || type == DT_LNK) {
2191 err = import_file(&new_te, de, path_dir, repo);
2197 err = insert_tree_entry(new_te, &paths);
2203 if (TAILQ_EMPTY(&paths)) {
2204 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2205 "cannot create tree without any entries");
2209 TAILQ_FOREACH(pe, &paths, entry) {
2210 struct got_tree_entry *te = pe->data;
2212 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2214 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2215 err = got_error_from_errno("asprintf");
2218 err = (*progress_cb)(progress_arg, path);
2224 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2228 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2232 const struct got_error *
2233 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2234 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2235 struct got_repository *repo, got_repo_import_cb progress_cb,
2238 const struct got_error *err;
2239 struct got_object_id *new_tree_id;
2241 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2242 progress_cb, progress_arg);
2246 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2247 author, time(NULL), author, time(NULL), logmsg, repo);
2252 const struct got_error *
2253 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2254 struct got_repository *repo)
2256 const struct got_error *err = NULL;
2257 char *path_objects = NULL, *path = NULL;
2259 struct got_object_id id;
2265 path_objects = got_repo_get_path_objects(repo);
2266 if (path_objects == NULL)
2267 return got_error_from_errno("got_repo_get_path_objects");
2269 for (i = 0; i <= 0xff; i++) {
2270 struct dirent *dent;
2272 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2273 err = got_error_from_errno("asprintf");
2277 dir = opendir(path);
2279 if (errno == ENOENT) {
2283 err = got_error_from_errno2("opendir", path);
2287 while ((dent = readdir(dir)) != NULL) {
2291 enum got_hash_algorithm algo = GOT_HASH_SHA1;
2293 if (strcmp(dent->d_name, ".") == 0 ||
2294 strcmp(dent->d_name, "..") == 0)
2297 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2298 err = got_error_from_errno("asprintf");
2302 if (!got_parse_object_id(&id, id_str, algo)) {
2308 err = got_object_open_loose_fd(&fd, &id, repo);
2312 if (fstat(fd, &sb) == -1) {
2313 err = got_error_from_errno("fstat");
2318 (*ondisk_size) += sb.st_size;
2320 if (close(fd) == -1) {
2321 err = got_error_from_errno("close");
2326 if (closedir(dir) != 0) {
2327 err = got_error_from_errno("closedir");
2336 if (dir && closedir(dir) != 0 && err == NULL)
2337 err = got_error_from_errno("closedir");
2348 const struct got_error *
2349 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2350 off_t *total_packsize, struct got_repository *repo)
2352 const struct got_error *err = NULL;
2353 DIR *packdir = NULL;
2354 struct dirent *dent;
2355 struct got_packidx *packidx = NULL;
2357 char *path_packfile;
2363 *total_packsize = 0;
2365 packdir_fd = openat(got_repo_get_fd(repo),
2366 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2367 if (packdir_fd == -1) {
2368 return got_error_from_errno_fmt("openat: %s/%s",
2369 got_repo_get_path_git_dir(repo),
2370 GOT_OBJECTS_PACK_DIR);
2373 packdir = fdopendir(packdir_fd);
2374 if (packdir == NULL) {
2375 err = got_error_from_errno("fdopendir");
2379 while ((dent = readdir(packdir)) != NULL) {
2380 if (!got_repo_is_packidx_filename(dent->d_name,
2381 strlen(dent->d_name)))
2384 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2385 dent->d_name) == -1) {
2386 err = got_error_from_errno("asprintf");
2390 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2396 if (fstat(packidx->fd, &sb) == -1)
2398 *total_packsize += sb.st_size;
2400 err = got_packidx_get_packfile_path(&path_packfile,
2401 packidx->path_packidx);
2405 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2407 free(path_packfile);
2410 free(path_packfile);
2411 *total_packsize += sb.st_size;
2413 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2417 got_packidx_close(packidx);
2422 got_packidx_close(packidx);
2423 if (packdir && closedir(packdir) != 0 && err == NULL)
2424 err = got_error_from_errno("closedir");
2428 *total_packsize = 0;
2433 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2434 got_packidx_bloom_filter_cmp);