2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <sys/resource.h>
48 #include "got_error.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_cancel.h"
53 #include "got_object.h"
54 #include "got_opentemp.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_delta_cache.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_object.h"
60 #include "got_lib_object_parse.h"
61 #include "got_lib_object_create.h"
62 #include "got_lib_pack.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_hash.h"
65 #include "got_lib_object_cache.h"
66 #include "got_lib_repository.h"
67 #include "got_lib_gotconfig.h"
70 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
73 #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
75 RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
76 got_packidx_bloom_filter_cmp);
79 is_boolean_val(const char *val)
81 return (strcasecmp(val, "true") == 0 ||
82 strcasecmp(val, "false") == 0 ||
83 strcasecmp(val, "on") == 0 ||
84 strcasecmp(val, "off") == 0 ||
85 strcasecmp(val, "yes") == 0 ||
86 strcasecmp(val, "no") == 0 ||
87 strcasecmp(val, "1") == 0 ||
88 strcasecmp(val, "0") == 0);
92 get_boolean_val(const char *val)
94 return (strcasecmp(val, "true") == 0 ||
95 strcasecmp(val, "on") == 0 ||
96 strcasecmp(val, "yes") == 0 ||
97 strcasecmp(val, "1") == 0);
101 got_repo_get_path(struct got_repository *repo)
107 got_repo_get_path_git_dir(struct got_repository *repo)
109 return repo->path_git_dir;
113 got_repo_get_fd(struct got_repository *repo)
115 return repo->gitdir_fd;
118 enum got_hash_algorithm
119 got_repo_get_object_format(struct got_repository *repo)
125 got_repo_get_gitconfig_author_name(struct got_repository *repo)
127 return repo->gitconfig_author_name;
131 got_repo_get_gitconfig_author_email(struct got_repository *repo)
133 return repo->gitconfig_author_email;
137 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
139 return repo->global_gitconfig_author_name;
143 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
145 return repo->global_gitconfig_author_email;
149 got_repo_get_gitconfig_owner(struct got_repository *repo)
151 return repo->gitconfig_owner;
155 got_repo_has_extension(struct got_repository *repo, const char *ext)
159 for (i = 0; i < repo->nextensions; ++i) {
160 if (!strcasecmp(ext, repo->extnames[i]))
161 return get_boolean_val(repo->extvals[i]);
168 got_repo_is_bare(struct got_repository *repo)
170 return (strcmp(repo->path, repo->path_git_dir) == 0);
174 get_path_git_child(struct got_repository *repo, const char *basename)
178 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
186 got_repo_get_path_objects(struct got_repository *repo)
188 return get_path_git_child(repo, GOT_OBJECTS_DIR);
192 got_repo_get_path_objects_pack(struct got_repository *repo)
194 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
198 got_repo_get_path_refs(struct got_repository *repo)
200 return get_path_git_child(repo, GOT_REFS_DIR);
204 got_repo_get_path_packed_refs(struct got_repository *repo)
206 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
210 get_path_head(struct got_repository *repo)
212 return get_path_git_child(repo, GOT_HEAD_FILE);
216 got_repo_get_path_gitconfig(struct got_repository *repo)
218 return get_path_git_child(repo, GOT_GITCONFIG);
222 got_repo_get_path_gotconfig(struct got_repository *repo)
224 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
227 const struct got_gotconfig *
228 got_repo_get_gotconfig(struct got_repository *repo)
230 return repo->gotconfig;
234 got_repo_get_gitconfig_remotes(int *nremotes,
235 const struct got_remote_repo **remotes, struct got_repository *repo)
237 *nremotes = repo->ngitconfig_remotes;
238 *remotes = repo->gitconfig_remotes;
242 is_git_repo(struct got_repository *repo)
244 const char *path_git = got_repo_get_path_git_dir(repo);
245 char *path_objects = got_repo_get_path_objects(repo);
246 char *path_refs = got_repo_get_path_refs(repo);
247 char *path_head = get_path_head(repo);
250 struct got_reference *head_ref;
252 if (lstat(path_git, &sb) == -1)
254 if (!S_ISDIR(sb.st_mode))
257 if (lstat(path_objects, &sb) == -1)
259 if (!S_ISDIR(sb.st_mode))
262 if (lstat(path_refs, &sb) == -1)
264 if (!S_ISDIR(sb.st_mode))
267 if (lstat(path_head, &sb) == -1)
269 if (!S_ISREG(sb.st_mode))
272 /* Check if the HEAD reference can be opened. */
273 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
275 got_ref_close(head_ref);
286 static const struct got_error *
287 close_tempfiles(int *fds, size_t nfds)
289 const struct got_error *err = NULL;
292 for (i = 0; i < nfds; i++) {
295 if (close(fds[i]) == -1) {
296 err = got_error_from_errno("close");
304 static const struct got_error *
305 open_tempfiles(int **fds, size_t array_size, size_t nfds)
307 const struct got_error *err = NULL;
310 *fds = calloc(array_size, sizeof(**fds));
312 return got_error_from_errno("calloc");
314 for (i = 0; i < array_size; i++)
317 for (i = 0; i < nfds; i++) {
318 (*fds)[i] = got_opentempfd();
319 if ((*fds)[i] == -1) {
320 err = got_error_from_errno("got_opentempfd");
321 close_tempfiles(*fds, nfds);
330 static const struct got_error *
331 get_pack_cache_size(int *pack_cache_size)
335 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
336 return got_error_from_errno("getrlimit");
338 *pack_cache_size = GOT_PACK_CACHE_SIZE;
339 if (*pack_cache_size > rl.rlim_cur / 8)
340 *pack_cache_size = rl.rlim_cur / 8;
345 const struct got_error *
346 got_repo_pack_fds_open(int **pack_fds)
348 const struct got_error *err;
351 err = get_pack_cache_size(&nfds);
356 * We need one basefd and one accumfd per cached pack.
357 * Our constants should be set up in a way such that
358 * this error never triggers.
360 if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
361 return got_error(GOT_ERR_NO_SPACE);
363 return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
366 const struct got_error *
367 got_repo_pack_fds_close(int *pack_fds)
369 return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
372 const struct got_error *
373 got_repo_temp_fds_open(int **temp_fds)
375 return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
376 GOT_REPO_NUM_TEMPFILES);
380 got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
384 for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
385 repo->tempfiles[i] = temp_fds[i];
388 const struct got_error *
389 got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
396 for (i = 0; i < nitems(repo->tempfiles); i++) {
397 if (repo->tempfile_use_mask & (1 << i))
399 if (repo->tempfiles[i] != -1) {
400 if (ftruncate(repo->tempfiles[i], 0L) == -1)
401 return got_error_from_errno("ftruncate");
402 *fd = repo->tempfiles[i];
404 repo->tempfile_use_mask |= (1 << i);
409 return got_error(GOT_ERR_REPO_TEMPFILE);
413 got_repo_temp_fds_put(int idx, struct got_repository *repo)
415 repo->tempfile_use_mask &= ~(1 << idx);
418 const struct got_error *
419 got_repo_temp_fds_close(int *temp_fds)
421 return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
424 const struct got_error *
425 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
426 struct got_object *obj)
428 #ifndef GOT_NO_OBJ_CACHE
429 const struct got_error *err = NULL;
430 err = got_object_cache_add(&repo->objcache, id, obj);
432 if (err->code == GOT_ERR_OBJ_EXISTS ||
433 err->code == GOT_ERR_OBJ_TOO_LARGE)
443 got_repo_get_cached_object(struct got_repository *repo,
444 struct got_object_id *id)
446 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
449 const struct got_error *
450 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
451 struct got_tree_object *tree)
453 #ifndef GOT_NO_OBJ_CACHE
454 const struct got_error *err = NULL;
455 err = got_object_cache_add(&repo->treecache, id, tree);
457 if (err->code == GOT_ERR_OBJ_EXISTS ||
458 err->code == GOT_ERR_OBJ_TOO_LARGE)
467 struct got_tree_object *
468 got_repo_get_cached_tree(struct got_repository *repo,
469 struct got_object_id *id)
471 return (struct got_tree_object *)got_object_cache_get(
472 &repo->treecache, id);
475 const struct got_error *
476 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
477 struct got_commit_object *commit)
479 #ifndef GOT_NO_OBJ_CACHE
480 const struct got_error *err = NULL;
481 err = got_object_cache_add(&repo->commitcache, id, commit);
483 if (err->code == GOT_ERR_OBJ_EXISTS ||
484 err->code == GOT_ERR_OBJ_TOO_LARGE)
493 struct got_commit_object *
494 got_repo_get_cached_commit(struct got_repository *repo,
495 struct got_object_id *id)
497 return (struct got_commit_object *)got_object_cache_get(
498 &repo->commitcache, id);
501 const struct got_error *
502 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
503 struct got_tag_object *tag)
505 #ifndef GOT_NO_OBJ_CACHE
506 const struct got_error *err = NULL;
507 err = got_object_cache_add(&repo->tagcache, id, tag);
509 if (err->code == GOT_ERR_OBJ_EXISTS ||
510 err->code == GOT_ERR_OBJ_TOO_LARGE)
519 struct got_tag_object *
520 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
522 return (struct got_tag_object *)got_object_cache_get(
523 &repo->tagcache, id);
526 const struct got_error *
527 got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
528 struct got_raw_object *raw)
530 #ifndef GOT_NO_OBJ_CACHE
531 const struct got_error *err = NULL;
532 err = got_object_cache_add(&repo->rawcache, id, raw);
534 if (err->code == GOT_ERR_OBJ_EXISTS ||
535 err->code == GOT_ERR_OBJ_TOO_LARGE)
545 struct got_raw_object *
546 got_repo_get_cached_raw_object(struct got_repository *repo,
547 struct got_object_id *id)
549 return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
553 static const struct got_error *
554 open_repo(struct got_repository *repo, const char *path)
556 const struct got_error *err = NULL;
558 repo->gitdir_fd = -1;
560 /* bare git repository? */
561 repo->path_git_dir = strdup(path);
562 if (repo->path_git_dir == NULL)
563 return got_error_from_errno("strdup");
564 if (is_git_repo(repo)) {
565 repo->path = strdup(repo->path_git_dir);
566 if (repo->path == NULL) {
567 err = got_error_from_errno("strdup");
570 repo->gitdir_fd = open(repo->path_git_dir,
571 O_DIRECTORY | O_CLOEXEC);
572 if (repo->gitdir_fd == -1) {
573 err = got_error_from_errno2("open",
580 /* git repository with working tree? */
581 free(repo->path_git_dir);
582 repo->path_git_dir = NULL;
583 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
584 err = got_error_from_errno("asprintf");
587 if (is_git_repo(repo)) {
588 repo->path = strdup(path);
589 if (repo->path == NULL) {
590 err = got_error_from_errno("strdup");
593 repo->gitdir_fd = open(repo->path_git_dir,
594 O_DIRECTORY | O_CLOEXEC);
595 if (repo->gitdir_fd == -1) {
596 err = got_error_from_errno2("open",
603 err = got_error(GOT_ERR_NOT_GIT_REPO);
608 free(repo->path_git_dir);
609 repo->path_git_dir = NULL;
610 if (repo->gitdir_fd != -1)
611 close(repo->gitdir_fd);
612 repo->gitdir_fd = -1;
618 static const struct got_error *
619 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
621 const struct got_error *err = NULL;
622 char *repo_gitconfig_path = NULL;
624 if (global_gitconfig_path) {
625 /* Read settings from ~/.gitconfig. */
626 int dummy_repo_version;
627 err = got_repo_read_gitconfig(&dummy_repo_version,
628 &repo->global_gitconfig_author_name,
629 &repo->global_gitconfig_author_email,
630 NULL, NULL, NULL, NULL, NULL, NULL,
631 global_gitconfig_path);
636 /* Read repository's .git/config file. */
637 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
638 if (repo_gitconfig_path == NULL)
639 return got_error_from_errno("got_repo_get_path_gitconfig");
641 err = got_repo_read_gitconfig(
642 &repo->gitconfig_repository_format_version,
643 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
644 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
645 &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
646 &repo->nextensions, repo_gitconfig_path);
650 if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
653 for (i = 0; i < repo->ngitconfig_remotes; i++) {
654 got_repo_free_remote_repo_data(
655 &repo->gitconfig_remotes[i]);
657 free(repo->gitconfig_remotes);
658 repo->gitconfig_remotes = NULL;
659 repo->ngitconfig_remotes = 0;
661 free(repo->gitconfig_author_name);
662 repo->gitconfig_author_name = NULL;
663 free(repo->gitconfig_author_email);
664 repo->gitconfig_author_email = NULL;
666 free(repo->global_gitconfig_author_name);
667 repo->global_gitconfig_author_name = NULL;
668 free(repo->global_gitconfig_author_email);
669 repo->global_gitconfig_author_email = NULL;
673 free(repo_gitconfig_path);
677 static const struct got_error *
678 read_gotconfig(struct got_repository *repo)
680 const struct got_error *err = NULL;
681 char *gotconfig_path;
683 gotconfig_path = got_repo_get_path_gotconfig(repo);
684 if (gotconfig_path == NULL)
685 return got_error_from_errno("got_repo_get_path_gotconfig");
687 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
688 free(gotconfig_path);
692 /* Supported repository format extensions. */
693 static const char *const repo_extensions[] = {
694 "noop", /* Got supports repository format version 1. */
695 "preciousObjects", /* Supported by gotadmin cleanup. */
696 "worktreeConfig", /* Got does not care about Git work trees. */
699 const struct got_error *
700 got_repo_open(struct got_repository **repop, const char *path,
701 const char *global_gitconfig_path, int *pack_fds)
703 struct got_repository *repo = NULL;
704 const struct got_error *err = NULL;
705 char *repo_path = NULL;
710 repo = calloc(1, sizeof(*repo));
712 return got_error_from_errno("calloc");
714 RB_INIT(&repo->packidx_bloom_filters);
715 TAILQ_INIT(&repo->packidx_paths);
717 for (i = 0; i < nitems(repo->privsep_children); i++) {
718 memset(&repo->privsep_children[i], 0,
719 sizeof(repo->privsep_children[0]));
720 repo->privsep_children[i].imsg_fd = -1;
723 err = got_object_cache_init(&repo->objcache,
724 GOT_OBJECT_CACHE_TYPE_OBJ);
727 err = got_object_cache_init(&repo->treecache,
728 GOT_OBJECT_CACHE_TYPE_TREE);
731 err = got_object_cache_init(&repo->commitcache,
732 GOT_OBJECT_CACHE_TYPE_COMMIT);
735 err = got_object_cache_init(&repo->tagcache,
736 GOT_OBJECT_CACHE_TYPE_TAG);
739 err = got_object_cache_init(&repo->rawcache,
740 GOT_OBJECT_CACHE_TYPE_RAW);
744 err = get_pack_cache_size(&repo->pack_cache_size);
747 for (i = 0; i < nitems(repo->packs); i++) {
748 if (pack_fds != NULL && i < repo->pack_cache_size) {
749 repo->packs[i].basefd = pack_fds[j++];
750 repo->packs[i].accumfd = pack_fds[j++];
752 repo->packs[i].basefd = -1;
753 repo->packs[i].accumfd = -1;
756 for (i = 0; i < nitems(repo->tempfiles); i++)
757 repo->tempfiles[i] = -1;
758 repo->pinned_pack = -1;
759 repo->pinned_packidx = -1;
760 repo->pinned_pid = 0;
762 repo_path = realpath(path, NULL);
763 if (repo_path == NULL) {
764 err = got_error_from_errno2("realpath", path);
771 err = open_repo(repo, repo_path);
774 if (err->code != GOT_ERR_NOT_GIT_REPO)
776 if (repo_path[0] == '/' && repo_path[1] == '\0') {
777 err = got_error(GOT_ERR_NOT_GIT_REPO);
780 err = got_path_dirname(&parent_path, repo_path);
784 repo_path = parent_path;
787 err = read_gotconfig(repo);
791 err = read_gitconfig(repo, global_gitconfig_path);
794 if (repo->gitconfig_repository_format_version != 0) {
795 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
798 for (i = 0; i < repo->nextensions; i++) {
799 char *ext = repo->extnames[i];
800 char *val = repo->extvals[i];
801 int j, supported = 0;
803 if (!is_boolean_val(val)) {
804 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
808 if (!get_boolean_val(val))
811 for (j = 0; j < nitems(repo_extensions); j++) {
812 if (strcmp(ext, repo_extensions[j]) == 0) {
818 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
823 err = got_repo_list_packidx(&repo->packidx_paths, repo);
826 got_repo_close(repo);
833 const struct got_error *
834 got_repo_close(struct got_repository *repo)
836 const struct got_error *err = NULL, *child_err;
837 struct got_packidx_bloom_filter *bf;
840 for (i = 0; i < repo->pack_cache_size; i++) {
841 if (repo->packidx_cache[i] == NULL)
843 got_packidx_close(repo->packidx_cache[i]);
846 while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
847 &repo->packidx_bloom_filters))) {
848 RB_REMOVE(got_packidx_bloom_filter_tree,
849 &repo->packidx_bloom_filters, bf);
850 bloom_free(bf->bloom);
855 for (i = 0; i < repo->pack_cache_size; i++)
856 if (repo->packs[i].path_packfile)
857 if (repo->packs[i].path_packfile)
858 got_pack_close(&repo->packs[i]);
861 free(repo->path_git_dir);
863 got_object_cache_close(&repo->objcache);
864 got_object_cache_close(&repo->treecache);
865 got_object_cache_close(&repo->commitcache);
866 got_object_cache_close(&repo->tagcache);
867 got_object_cache_close(&repo->rawcache);
869 for (i = 0; i < nitems(repo->privsep_children); i++) {
870 if (repo->privsep_children[i].imsg_fd == -1)
872 imsg_clear(repo->privsep_children[i].ibuf);
873 free(repo->privsep_children[i].ibuf);
874 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
875 if (err && err->code == GOT_ERR_EOF)
877 child_err = got_privsep_wait_for_child(
878 repo->privsep_children[i].pid);
879 if (child_err && err == NULL)
881 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
883 err = got_error_from_errno("close");
886 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
888 err = got_error_from_errno("close");
891 got_gotconfig_free(repo->gotconfig);
892 free(repo->gitconfig_author_name);
893 free(repo->gitconfig_author_email);
894 for (i = 0; i < repo->ngitconfig_remotes; i++)
895 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
896 free(repo->gitconfig_remotes);
897 for (i = 0; i < repo->nextensions; i++) {
898 free(repo->extnames[i]);
899 free(repo->extvals[i]);
901 free(repo->extnames);
904 got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
911 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
917 free(repo->fetch_url);
918 repo->fetch_url = NULL;
919 free(repo->send_url);
920 repo->send_url = NULL;
921 for (i = 0; i < repo->nfetch_branches; i++)
922 free(repo->fetch_branches[i]);
923 free(repo->fetch_branches);
924 repo->fetch_branches = NULL;
925 repo->nfetch_branches = 0;
926 for (i = 0; i < repo->nsend_branches; i++)
927 free(repo->send_branches[i]);
928 free(repo->send_branches);
929 repo->send_branches = NULL;
930 repo->nsend_branches = 0;
933 const struct got_error *
934 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
935 const char *input_path)
937 const struct got_error *err = NULL;
938 const char *repo_abspath = NULL;
940 char *canonpath, *path = NULL;
942 *in_repo_path = NULL;
944 canonpath = strdup(input_path);
945 if (canonpath == NULL) {
946 err = got_error_from_errno("strdup");
949 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
953 repo_abspath = got_repo_get_path(repo);
955 if (canonpath[0] == '\0') {
956 path = strdup(canonpath);
958 err = got_error_from_errno("strdup");
962 path = realpath(canonpath, NULL);
964 if (errno != ENOENT) {
965 err = got_error_from_errno2("realpath",
970 * Path is not on disk.
971 * Assume it is already relative to repository root.
973 path = strdup(canonpath);
975 err = got_error_from_errno("strdup");
980 repolen = strlen(repo_abspath);
984 if (strcmp(path, repo_abspath) == 0) {
988 err = got_error_from_errno("strdup");
991 } else if (len > repolen &&
992 got_path_is_child(path, repo_abspath, repolen)) {
993 /* Matched an on-disk path inside repository. */
994 if (got_repo_is_bare(repo)) {
996 * Matched an on-disk path inside repository
997 * database. Treat input as repository-relative.
1004 /* Strip common prefix with repository path. */
1005 err = got_path_skip_common_ancestor(&child,
1006 repo_abspath, path);
1014 * Matched unrelated on-disk path.
1015 * Treat input as repository-relative.
1023 /* Make in-repository path absolute */
1024 if (path[0] != '/') {
1026 if (asprintf(&abspath, "/%s", path) == -1) {
1027 err = got_error_from_errno("asprintf");
1039 *in_repo_path = path;
1043 static const struct got_error *
1044 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1045 const char *path_packidx)
1047 const struct got_error *err = NULL;
1050 for (i = 0; i < repo->pack_cache_size; i++) {
1051 if (repo->packidx_cache[i] == NULL)
1053 if (strcmp(repo->packidx_cache[i]->path_packidx,
1054 path_packidx) == 0) {
1055 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1058 if (i == repo->pack_cache_size) {
1061 } while (i > 0 && repo->pinned_packidx >= 0 &&
1062 i == repo->pinned_packidx);
1063 err = got_packidx_close(repo->packidx_cache[i]);
1068 repo->packidx_cache[i] = packidx;
1074 got_repo_is_packidx_filename(const char *name, size_t len)
1076 if (len != GOT_PACKIDX_NAMELEN)
1079 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1082 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1083 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1089 static struct got_packidx_bloom_filter *
1090 get_packidx_bloom_filter(struct got_repository *repo,
1091 const char *path, size_t path_len)
1093 struct got_packidx_bloom_filter key;
1095 if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1096 return NULL; /* XXX */
1097 key.path_len = path_len;
1099 return RB_FIND(got_packidx_bloom_filter_tree,
1100 &repo->packidx_bloom_filters, &key);
1104 got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1105 const char *path_packidx, struct got_object_id *id)
1107 struct got_packidx_bloom_filter *bf;
1109 bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1111 return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1113 /* No bloom filter means this pack index must be searched. */
1117 static const struct got_error *
1118 add_packidx_bloom_filter(struct got_repository *repo,
1119 struct got_packidx *packidx, const char *path_packidx)
1121 int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1122 struct got_packidx_bloom_filter *bf;
1126 * Don't use bloom filters for very large pack index files.
1127 * Large pack files will contain a relatively large fraction
1128 * of our objects so we will likely need to visit them anyway.
1129 * The more objects a pack file contains the higher the probability
1130 * of a false-positive match from the bloom filter. And reading
1131 * all object IDs from a large pack index file can be expensive.
1133 if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1136 /* Do we already have a filter for this pack index? */
1137 if (get_packidx_bloom_filter(repo, path_packidx,
1138 strlen(path_packidx)) != NULL)
1141 bf = calloc(1, sizeof(*bf));
1143 return got_error_from_errno("calloc");
1144 bf->bloom = calloc(1, sizeof(*bf->bloom));
1145 if (bf->bloom == NULL) {
1147 return got_error_from_errno("calloc");
1150 len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1151 if (len >= sizeof(bf->path)) {
1154 return got_error(GOT_ERR_NO_SPACE);
1158 /* Minimum size supported by our bloom filter is 1000 entries. */
1159 bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1160 for (i = 0; i < nobjects; i++) {
1161 struct got_packidx_object_id *id;
1162 id = &packidx->hdr.sorted_ids[i];
1163 bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1166 RB_INSERT(got_packidx_bloom_filter_tree,
1167 &repo->packidx_bloom_filters, bf);
1172 purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1174 struct got_pathlist_entry *pe;
1176 while (!TAILQ_EMPTY(packidx_paths)) {
1177 pe = TAILQ_FIRST(packidx_paths);
1178 TAILQ_REMOVE(packidx_paths, pe, entry);
1179 free((char *)pe->path);
1184 static const struct got_error *
1185 refresh_packidx_paths(struct got_repository *repo)
1187 const struct got_error *err = NULL;
1188 char *objects_pack_dir = NULL;
1191 objects_pack_dir = got_repo_get_path_objects_pack(repo);
1192 if (objects_pack_dir == NULL)
1193 return got_error_from_errno("got_repo_get_path_objects_pack");
1195 if (stat(objects_pack_dir, &sb) == -1) {
1196 if (errno != ENOENT) {
1197 err = got_error_from_errno2("stat", objects_pack_dir);
1200 } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1201 sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1202 sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1203 purge_packidx_paths(&repo->packidx_paths);
1204 err = got_repo_list_packidx(&repo->packidx_paths, repo);
1209 free(objects_pack_dir);
1213 const struct got_error *
1214 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1215 struct got_repository *repo, struct got_object_id *id)
1217 const struct got_error *err;
1218 struct got_pathlist_entry *pe;
1221 /* Search pack index cache. */
1222 for (i = 0; i < repo->pack_cache_size; i++) {
1223 if (repo->packidx_cache[i] == NULL)
1225 if (!got_repo_check_packidx_bloom_filter(repo,
1226 repo->packidx_cache[i]->path_packidx, id))
1227 continue; /* object will not be found in this index */
1228 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1230 *packidx = repo->packidx_cache[i];
1232 * Move this cache entry to the front. Repeatedly
1233 * searching a wrong pack index can be expensive.
1236 memmove(&repo->packidx_cache[1],
1237 &repo->packidx_cache[0],
1238 i * sizeof(repo->packidx_cache[0]));
1239 repo->packidx_cache[0] = *packidx;
1240 if (repo->pinned_packidx >= 0 &&
1241 repo->pinned_packidx < i)
1242 repo->pinned_packidx++;
1243 else if (repo->pinned_packidx == i)
1244 repo->pinned_packidx = 0;
1249 /* No luck. Search the filesystem. */
1251 err = refresh_packidx_paths(repo);
1255 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1256 const char *path_packidx = pe->path;
1259 if (!got_repo_check_packidx_bloom_filter(repo,
1261 continue; /* object will not be found in this index */
1263 for (i = 0; i < repo->pack_cache_size; i++) {
1264 if (repo->packidx_cache[i] == NULL)
1266 if (strcmp(repo->packidx_cache[i]->path_packidx,
1267 path_packidx) == 0) {
1273 continue; /* already searched */
1275 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1280 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1284 err = cache_packidx(repo, *packidx, path_packidx);
1288 *idx = got_packidx_get_object_idx(*packidx, id);
1290 err = NULL; /* found the object */
1295 err = got_error_no_obj(id);
1300 const struct got_error *
1301 got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1302 struct got_repository *repo)
1304 const struct got_error *err = NULL;
1305 DIR *packdir = NULL;
1306 struct dirent *dent;
1307 char *path_packidx = NULL;
1311 packdir_fd = openat(got_repo_get_fd(repo),
1312 GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1313 if (packdir_fd == -1) {
1314 return got_error_from_errno_fmt("openat: %s/%s",
1315 got_repo_get_path_git_dir(repo),
1316 GOT_OBJECTS_PACK_DIR);
1319 packdir = fdopendir(packdir_fd);
1320 if (packdir == NULL) {
1321 err = got_error_from_errno("fdopendir");
1325 if (fstat(packdir_fd, &sb) == -1) {
1326 err = got_error_from_errno("fstat");
1329 repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1330 repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1332 while ((dent = readdir(packdir)) != NULL) {
1333 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1336 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1337 dent->d_name) == -1) {
1338 err = got_error_from_errno("asprintf");
1339 path_packidx = NULL;
1343 err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1350 if (packdir && closedir(packdir) != 0 && err == NULL)
1351 err = got_error_from_errno("closedir");
1355 const struct got_error *
1356 got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1357 struct got_repository *repo)
1359 const struct got_error *err;
1364 /* Search pack index cache. */
1365 for (i = 0; i < repo->pack_cache_size; i++) {
1366 if (repo->packidx_cache[i] == NULL)
1368 if (strcmp(repo->packidx_cache[i]->path_packidx,
1369 path_packidx) == 0) {
1370 *packidx = repo->packidx_cache[i];
1374 /* No luck. Search the filesystem. */
1376 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1381 err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1385 err = cache_packidx(repo, *packidx, path_packidx);
1388 got_packidx_close(*packidx);
1394 static const struct got_error *
1395 read_packfile_hdr(int fd, struct got_packidx *packidx)
1397 const struct got_error *err = NULL;
1398 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1399 struct got_packfile_hdr hdr;
1402 n = read(fd, &hdr, sizeof(hdr));
1404 return got_error_from_errno("read");
1405 if (n != sizeof(hdr))
1406 return got_error(GOT_ERR_BAD_PACKFILE);
1408 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1409 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1410 be32toh(hdr.nobjects) != totobj)
1411 err = got_error(GOT_ERR_BAD_PACKFILE);
1416 static const struct got_error *
1417 open_packfile(int *fd, struct got_repository *repo,
1418 const char *relpath, struct got_packidx *packidx)
1420 const struct got_error *err = NULL;
1422 *fd = openat(got_repo_get_fd(repo), relpath,
1423 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1425 return got_error_from_errno_fmt("openat: %s/%s",
1426 got_repo_get_path_git_dir(repo), relpath);
1429 err = read_packfile_hdr(*fd, packidx);
1439 const struct got_error *
1440 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1441 const char *path_packfile, struct got_packidx *packidx)
1443 const struct got_error *err = NULL;
1444 struct got_pack *pack = NULL;
1451 for (i = 0; i < repo->pack_cache_size; i++) {
1452 pack = &repo->packs[i];
1453 if (pack->path_packfile == NULL)
1455 if (strcmp(pack->path_packfile, path_packfile) == 0)
1456 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1459 if (i == repo->pack_cache_size) {
1460 struct got_pack tmp;
1463 } while (i > 0 && repo->pinned_pack >= 0 &&
1464 i == repo->pinned_pack);
1465 err = got_pack_close(&repo->packs[i]);
1468 if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1469 return got_error_from_errno("ftruncate");
1470 if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1471 return got_error_from_errno("ftruncate");
1472 memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1473 memcpy(&repo->packs[i], &repo->packs[0],
1474 sizeof(repo->packs[i]));
1475 memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1476 if (repo->pinned_pack == 0)
1477 repo->pinned_pack = i;
1478 else if (repo->pinned_pack == i)
1479 repo->pinned_pack = 0;
1483 pack = &repo->packs[i];
1485 pack->path_packfile = strdup(path_packfile);
1486 if (pack->path_packfile == NULL) {
1487 err = got_error_from_errno("strdup");
1491 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1495 if (fstat(pack->fd, &sb) != 0) {
1496 err = got_error_from_errno("fstat");
1499 pack->filesize = sb.st_size;
1501 pack->privsep_child = NULL;
1503 err = got_delta_cache_alloc(&pack->delta_cache);
1507 #ifndef GOT_PACK_NO_MMAP
1508 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1509 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1511 if (pack->map == MAP_FAILED) {
1512 if (errno != ENOMEM) {
1513 err = got_error_from_errno("mmap");
1516 pack->map = NULL; /* fall back to read(2) */
1523 got_pack_close(pack);
1530 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1532 struct got_pack *pack = NULL;
1535 for (i = 0; i < repo->pack_cache_size; i++) {
1536 pack = &repo->packs[i];
1537 if (pack->path_packfile == NULL)
1539 if (strcmp(pack->path_packfile, path_packfile) == 0)
1546 const struct got_error *
1547 got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1548 struct got_pack *pack)
1551 int pinned_pack = -1, pinned_packidx = -1;
1553 for (i = 0; i < repo->pack_cache_size; i++) {
1554 if (repo->packidx_cache[i] &&
1555 strcmp(repo->packidx_cache[i]->path_packidx,
1556 packidx->path_packidx) == 0)
1558 if (repo->packs[i].path_packfile &&
1559 strcmp(repo->packs[i].path_packfile,
1560 pack->path_packfile) == 0)
1564 if (pinned_packidx == -1 || pinned_pack == -1)
1565 return got_error(GOT_ERR_PIN_PACK);
1567 repo->pinned_pack = pinned_pack;
1568 repo->pinned_packidx = pinned_packidx;
1569 if (repo->packs[pinned_pack].privsep_child)
1570 repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1575 got_repo_get_pinned_pack(struct got_repository *repo)
1577 if (repo->pinned_pack >= 0 &&
1578 repo->pinned_pack < repo->pack_cache_size)
1579 return &repo->packs[repo->pinned_pack];
1585 got_repo_unpin_pack(struct got_repository *repo)
1587 repo->pinned_packidx = -1;
1588 repo->pinned_pack = -1;
1589 repo->pinned_pid = 0;
1592 const struct got_error *
1593 got_repo_init(const char *repo_path, const char *head_name)
1595 const struct got_error *err = NULL;
1596 const char *dirnames[] = {
1598 GOT_OBJECTS_PACK_DIR,
1601 const char *description_str = "Unnamed repository; "
1602 "edit this file 'description' to name the repository.";
1603 const char *headref = "ref: refs/heads/";
1604 const char *gitconfig_str = "[core]\n"
1605 "\trepositoryformatversion = 0\n"
1606 "\tfilemode = true\n"
1608 char *headref_str, *path;
1611 if (!got_path_dir_is_empty(repo_path))
1612 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1614 for (i = 0; i < nitems(dirnames); i++) {
1615 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1616 return got_error_from_errno("asprintf");
1618 err = got_path_mkdir(path);
1624 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1625 return got_error_from_errno("asprintf");
1626 err = got_path_create_file(path, description_str);
1631 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1632 return got_error_from_errno("asprintf");
1633 if (asprintf(&headref_str, "%s%s", headref,
1634 head_name ? head_name : "main") == -1) {
1636 return got_error_from_errno("asprintf");
1638 err = got_path_create_file(path, headref_str);
1644 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1645 return got_error_from_errno("asprintf");
1646 err = got_path_create_file(path, gitconfig_str);
1654 static const struct got_error *
1655 match_packed_object(struct got_object_id **unique_id,
1656 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1658 const struct got_error *err = NULL;
1659 struct got_object_id_queue matched_ids;
1660 struct got_pathlist_entry *pe;
1662 STAILQ_INIT(&matched_ids);
1664 err = refresh_packidx_paths(repo);
1668 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1669 const char *path_packidx = pe->path;
1670 struct got_packidx *packidx;
1671 struct got_object_qid *qid;
1673 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1678 err = got_packidx_match_id_str_prefix(&matched_ids,
1679 packidx, id_str_prefix);
1681 got_packidx_close(packidx);
1684 err = got_packidx_close(packidx);
1688 STAILQ_FOREACH(qid, &matched_ids, entry) {
1689 if (obj_type != GOT_OBJ_TYPE_ANY) {
1691 err = got_object_get_type(&matched_type, repo,
1695 if (matched_type != obj_type)
1698 if (*unique_id == NULL) {
1699 *unique_id = got_object_id_dup(&qid->id);
1700 if (*unique_id == NULL) {
1701 err = got_error_from_errno("malloc");
1705 if (got_object_id_cmp(*unique_id,
1707 continue; /* packed multiple times */
1708 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1714 got_object_id_queue_free(&matched_ids);
1722 static const struct got_error *
1723 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1724 const char *object_dir, const char *id_str_prefix, int obj_type,
1725 struct got_repository *repo)
1727 const struct got_error *err = NULL;
1728 char *path, *id_str = NULL;
1730 struct dirent *dent;
1731 struct got_object_id id;
1733 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1734 err = got_error_from_errno("asprintf");
1738 dir = opendir(path);
1740 if (errno == ENOENT) {
1744 err = got_error_from_errno2("opendir", path);
1747 while ((dent = readdir(dir)) != NULL) {
1753 if (strcmp(dent->d_name, ".") == 0 ||
1754 strcmp(dent->d_name, "..") == 0)
1757 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1758 err = got_error_from_errno("asprintf");
1762 if (!got_parse_object_id(&id, id_str, repo->algo))
1766 * Directory entries do not necessarily appear in
1767 * sorted order, so we must iterate over all of them.
1769 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1773 if (*unique_id == NULL) {
1774 if (obj_type != GOT_OBJ_TYPE_ANY) {
1776 err = got_object_get_type(&matched_type, repo,
1780 if (matched_type != obj_type)
1783 *unique_id = got_object_id_dup(&id);
1784 if (*unique_id == NULL) {
1785 err = got_error_from_errno("got_object_id_dup");
1789 if (got_object_id_cmp(*unique_id, &id) == 0)
1790 continue; /* both packed and loose */
1791 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1796 if (dir && closedir(dir) != 0 && err == NULL)
1797 err = got_error_from_errno("closedir");
1807 const struct got_error *
1808 got_repo_match_object_id_prefix(struct got_object_id **id,
1809 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1811 const struct got_error *err = NULL;
1812 char *path_objects = NULL, *object_dir = NULL;
1818 path_objects = got_repo_get_path_objects(repo);
1820 len = strlen(id_str_prefix);
1821 if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1822 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1826 for (i = 0; i < len; i++) {
1827 if (isxdigit((unsigned char)id_str_prefix[i]))
1829 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1834 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1837 object_dir = strndup(id_str_prefix, 2);
1838 if (object_dir == NULL) {
1839 err = got_error_from_errno("strdup");
1842 err = match_loose_object(id, path_objects, object_dir,
1843 id_str_prefix, obj_type, repo);
1844 } else if (len == 1) {
1846 for (i = 0; i < 0xf; i++) {
1847 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1849 err = got_error_from_errno("asprintf");
1852 err = match_packed_object(id, repo, object_dir,
1856 err = match_loose_object(id, path_objects, object_dir,
1857 id_str_prefix, obj_type, repo);
1862 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1871 } else if (*id == NULL) {
1873 case GOT_OBJ_TYPE_BLOB:
1874 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1875 GOT_OBJ_LABEL_BLOB, id_str_prefix);
1877 case GOT_OBJ_TYPE_TREE:
1878 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1879 GOT_OBJ_LABEL_TREE, id_str_prefix);
1881 case GOT_OBJ_TYPE_COMMIT:
1882 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1883 GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1885 case GOT_OBJ_TYPE_TAG:
1886 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1887 GOT_OBJ_LABEL_TAG, id_str_prefix);
1890 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1898 const struct got_error *
1899 got_repo_match_object_id(struct got_object_id **id, char **label,
1900 const char *id_str, int obj_type, struct got_reflist_head *refs,
1901 struct got_repository *repo)
1903 const struct got_error *err;
1904 struct got_tag_object *tag;
1905 struct got_reference *ref = NULL;
1912 err = got_repo_object_match_tag(&tag, id_str, obj_type,
1915 *id = got_object_id_dup(
1916 got_object_tag_get_object_id(tag));
1918 err = got_error_from_errno("got_object_id_dup");
1919 else if (label && asprintf(label, "refs/tags/%s",
1920 got_object_tag_get_name(tag)) == -1) {
1921 err = got_error_from_errno("asprintf");
1925 got_object_tag_close(tag);
1927 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1928 err->code != GOT_ERR_NO_OBJ)
1932 err = got_ref_open(&ref, repo, id_str, 0);
1934 err = got_ref_resolve(id, repo, ref);
1938 *label = strdup(got_ref_get_name(ref));
1939 if (*label == NULL) {
1940 err = got_error_from_errno("strdup");
1945 if (err->code != GOT_ERR_NOT_REF &&
1946 err->code != GOT_ERR_BAD_REF_NAME)
1948 err = got_repo_match_object_id_prefix(id, id_str,
1951 if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1952 err = got_error_not_ref(id_str);
1956 err = got_object_id_str(label, *id);
1957 if (*label == NULL) {
1958 err = got_error_from_errno("strdup");
1969 const struct got_error *
1970 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1971 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1973 const struct got_error *err = NULL;
1974 struct got_reflist_entry *re;
1975 struct got_object_id *tag_id;
1976 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1980 TAILQ_FOREACH(re, refs, entry) {
1981 const char *refname;
1982 refname = got_ref_get_name(re->ref);
1983 if (got_ref_is_symbolic(re->ref))
1985 if (strncmp(refname, "refs/tags/", 10) != 0)
1987 if (!name_is_absolute)
1988 refname += strlen("refs/tags/");
1989 if (strcmp(refname, name) != 0)
1991 err = got_ref_resolve(&tag_id, repo, re->ref);
1994 err = got_object_open_as_tag(tag, repo, tag_id);
1998 if (obj_type == GOT_OBJ_TYPE_ANY ||
1999 got_object_tag_get_object_type(*tag) == obj_type)
2001 got_object_tag_close(*tag);
2005 if (err == NULL && *tag == NULL)
2006 err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2007 GOT_OBJ_LABEL_TAG, name);
2011 static const struct got_error *
2012 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2013 const char *name, mode_t mode, struct got_object_id *blob_id)
2015 const struct got_error *err = NULL;
2019 *new_te = calloc(1, sizeof(**new_te));
2020 if (*new_te == NULL)
2021 return got_error_from_errno("calloc");
2023 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2024 sizeof((*new_te)->name)) {
2025 err = got_error(GOT_ERR_NO_SPACE);
2029 if (S_ISLNK(mode)) {
2030 (*new_te)->mode = S_IFLNK;
2032 (*new_te)->mode = S_IFREG;
2033 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2035 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2037 if (err && *new_te) {
2044 static const struct got_error *
2045 import_file(struct got_tree_entry **new_te, struct dirent *de,
2046 const char *path, struct got_repository *repo)
2048 const struct got_error *err;
2049 struct got_object_id *blob_id = NULL;
2053 if (asprintf(&filepath, "%s%s%s", path,
2054 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2055 return got_error_from_errno("asprintf");
2057 if (lstat(filepath, &sb) != 0) {
2058 err = got_error_from_errno2("lstat", path);
2062 err = got_object_blob_create(&blob_id, filepath, repo);
2066 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2075 static const struct got_error *
2076 insert_tree_entry(struct got_tree_entry *new_te,
2077 struct got_pathlist_head *paths)
2079 const struct got_error *err = NULL;
2080 struct got_pathlist_entry *new_pe;
2082 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2086 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2090 static const struct got_error *write_tree(struct got_object_id **,
2091 const char *, struct got_pathlist_head *, struct got_repository *,
2092 got_repo_import_cb progress_cb, void *progress_arg);
2094 static const struct got_error *
2095 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2096 const char *path, struct got_pathlist_head *ignores,
2097 struct got_repository *repo,
2098 got_repo_import_cb progress_cb, void *progress_arg)
2100 const struct got_error *err;
2101 struct got_object_id *id = NULL;
2104 if (asprintf(&subdirpath, "%s%s%s", path,
2105 path[0] == '\0' ? "" : "/", de->d_name) == -1)
2106 return got_error_from_errno("asprintf");
2108 (*new_te) = calloc(1, sizeof(**new_te));
2109 if (*new_te == NULL)
2110 return got_error_from_errno("calloc");
2111 (*new_te)->mode = S_IFDIR;
2112 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2113 sizeof((*new_te)->name)) {
2114 err = got_error(GOT_ERR_NO_SPACE);
2117 err = write_tree(&id, subdirpath, ignores, repo,
2118 progress_cb, progress_arg);
2121 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2133 static const struct got_error *
2134 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2135 struct got_pathlist_head *ignores, struct got_repository *repo,
2136 got_repo_import_cb progress_cb, void *progress_arg)
2138 const struct got_error *err = NULL;
2142 struct got_tree_entry *new_te = NULL;
2143 struct got_pathlist_head paths;
2144 struct got_pathlist_entry *pe;
2146 *new_tree_id = NULL;
2150 dir = opendir(path_dir);
2152 err = got_error_from_errno2("opendir", path_dir);
2157 while ((de = readdir(dir)) != NULL) {
2161 if (strcmp(de->d_name, ".") == 0 ||
2162 strcmp(de->d_name, "..") == 0)
2165 err = got_path_dirent_type(&type, path_dir, de);
2169 TAILQ_FOREACH(pe, ignores, entry) {
2170 if (type == DT_DIR && pe->path_len > 0 &&
2171 pe->path[pe->path_len - 1] == '/') {
2172 char stripped[PATH_MAX];
2174 if (strlcpy(stripped, pe->path,
2175 sizeof(stripped)) >= sizeof(stripped)) {
2176 err = got_error(GOT_ERR_NO_SPACE);
2179 got_path_strip_trailing_slashes(stripped);
2180 if (fnmatch(stripped, de->d_name, 0) == 0) {
2184 } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2192 if (type == DT_DIR) {
2193 err = import_subdir(&new_te, de, path_dir,
2194 ignores, repo, progress_cb, progress_arg);
2196 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2201 } else if (type == DT_REG || type == DT_LNK) {
2202 err = import_file(&new_te, de, path_dir, repo);
2208 err = insert_tree_entry(new_te, &paths);
2214 if (TAILQ_EMPTY(&paths)) {
2215 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2216 "cannot create tree without any entries");
2220 TAILQ_FOREACH(pe, &paths, entry) {
2221 struct got_tree_entry *te = pe->data;
2223 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2225 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2226 err = got_error_from_errno("asprintf");
2229 err = (*progress_cb)(progress_arg, path);
2235 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2239 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2243 const struct got_error *
2244 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2245 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2246 struct got_repository *repo, got_repo_import_cb progress_cb,
2249 const struct got_error *err;
2250 struct got_object_id *new_tree_id;
2252 err = write_tree(&new_tree_id, path_dir, ignores, repo,
2253 progress_cb, progress_arg);
2257 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2258 author, time(NULL), author, time(NULL), logmsg, repo);
2263 const struct got_error *
2264 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2265 struct got_repository *repo)
2267 const struct got_error *err = NULL;
2268 char *path_objects = NULL, *path = NULL;
2270 struct got_object_id id;
2276 path_objects = got_repo_get_path_objects(repo);
2277 if (path_objects == NULL)
2278 return got_error_from_errno("got_repo_get_path_objects");
2280 for (i = 0; i <= 0xff; i++) {
2281 struct dirent *dent;
2283 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2284 err = got_error_from_errno("asprintf");
2288 dir = opendir(path);
2290 if (errno == ENOENT) {
2294 err = got_error_from_errno2("opendir", path);
2298 while ((dent = readdir(dir)) != NULL) {
2303 if (strcmp(dent->d_name, ".") == 0 ||
2304 strcmp(dent->d_name, "..") == 0)
2307 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2308 err = got_error_from_errno("asprintf");
2312 if (!got_parse_object_id(&id, id_str, repo->algo)) {
2318 err = got_object_open_loose_fd(&fd, &id, repo);
2322 if (fstat(fd, &sb) == -1) {
2323 err = got_error_from_errno("fstat");
2328 (*ondisk_size) += sb.st_size;
2330 if (close(fd) == -1) {
2331 err = got_error_from_errno("close");
2336 if (closedir(dir) != 0) {
2337 err = got_error_from_errno("closedir");
2346 if (dir && closedir(dir) != 0 && err == NULL)
2347 err = got_error_from_errno("closedir");
2358 const struct got_error *
2359 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2360 off_t *total_packsize, struct got_repository *repo)
2362 const struct got_error *err = NULL;
2363 DIR *packdir = NULL;
2364 struct dirent *dent;
2365 struct got_packidx *packidx = NULL;
2367 char *path_packfile;
2373 *total_packsize = 0;
2375 packdir_fd = openat(got_repo_get_fd(repo),
2376 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2377 if (packdir_fd == -1) {
2378 return got_error_from_errno_fmt("openat: %s/%s",
2379 got_repo_get_path_git_dir(repo),
2380 GOT_OBJECTS_PACK_DIR);
2383 packdir = fdopendir(packdir_fd);
2384 if (packdir == NULL) {
2385 err = got_error_from_errno("fdopendir");
2389 while ((dent = readdir(packdir)) != NULL) {
2390 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2393 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2394 dent->d_name) == -1) {
2395 err = got_error_from_errno("asprintf");
2399 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2405 if (fstat(packidx->fd, &sb) == -1)
2407 *total_packsize += sb.st_size;
2409 err = got_packidx_get_packfile_path(&path_packfile,
2410 packidx->path_packidx);
2414 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2416 free(path_packfile);
2419 free(path_packfile);
2420 *total_packsize += sb.st_size;
2422 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2426 got_packidx_close(packidx);
2431 got_packidx_close(packidx);
2432 if (packdir && closedir(packdir) != 0 && err == NULL)
2433 err = got_error_from_errno("closedir");
2437 *total_packsize = 0;
2442 RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2443 got_packidx_bloom_filter_cmp);