From 6168d85a886286a43b55a0cd6739a1e1e3987d90 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 22 Jul 2019 20:08:39 +0100 Subject: Unline git helpers and throw NetFS exceptions --- src/blob.cpp | 13 +---------- src/blob.h | 1 - src/dir.cpp | 11 +++------- src/git.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++---------- src/git.h | 71 +++++++++++++++++++++--------------------------------------- src/repo.cpp | 53 +++++++++++++++++---------------------------- 6 files changed, 99 insertions(+), 112 deletions(-) (limited to 'src') diff --git a/src/blob.cpp b/src/blob.cpp index 4bd8b15..a3408b3 100644 --- a/src/blob.cpp +++ b/src/blob.cpp @@ -5,24 +5,13 @@ GitFS::Blob::Blob(const Repo * r, const std::string & path) : repo(r), - entry(getTreeEntry(path)), + entry(Git::TreeEntryByPath(repo->tree, path)), blob(getBlob()), blobSize(git_blob_rawsize(blob.get())), blobContent(static_cast(git_blob_rawcontent(blob.get()))) { } -GitFS::Git::TreeEntryPtr -GitFS::Blob::getTreeEntry(const std::string & path) const -{ - try { - return Git::TreeEntryByPath(repo->tree, path); - } - catch (const Git::Error & e) { - Git::ErrorToSystemError(e); - } -} - GitFS::Git::BlobPtr GitFS::Blob::getBlob() const { diff --git a/src/blob.h b/src/blob.h index 64f41ac..e1dd86f 100644 --- a/src/blob.h +++ b/src/blob.h @@ -18,7 +18,6 @@ namespace GitFS { void write(long long int offset, long long int size, Buffer data, const ::Ice::Current& current) override; private: - Git::TreeEntryPtr getTreeEntry(const std::string & path) const; Git::BlobPtr getBlob() const; const Repo * repo; Git::TreeEntryPtr entry; diff --git a/src/dir.cpp b/src/dir.cpp index cee26f0..c590450 100644 --- a/src/dir.cpp +++ b/src/dir.cpp @@ -19,14 +19,9 @@ GitFS::Directory::getSubtree() const subTreeCache = repo->tree; } else { - try { - auto e = Git::TreeEntryByPath(repo->tree, path); - if (!S_ISDIR(git_tree_entry_filemode(e.get()))) throw NetFS::SystemError(ENOTDIR); - subTreeCache = Git::TreeLookup(repo->repo, *git_tree_entry_id(e.get())); - } - catch (const Git::Error & e) { - Git::ErrorToSystemError(e); - } + auto e = Git::TreeEntryByPath(repo->tree, path); + if (!S_ISDIR(git_tree_entry_filemode(e.get()))) throw NetFS::SystemError(ENOTDIR); + subTreeCache = Git::TreeLookup(repo->repo, *git_tree_entry_id(e.get())); } subTreeCacheRootId = *git_tree_id(repo->tree.get()); } diff --git a/src/git.cpp b/src/git.cpp index 850b8b3..045d146 100644 --- a/src/git.cpp +++ b/src/git.cpp @@ -6,25 +6,63 @@ namespace GitFS { namespace Git { + template<> + [[noreturn]] void - throwError(int err) + throwError(int err) { -#if LIBGIT2_VER_MINOR >= 28 - const git_error * e = git_error_last(); -#else - const git_error * e = giterr_last(); -#endif - throw Error { err, e->klass, e->message }; + switch (err) { + case GIT_ENOTFOUND: + throw NetFS::SystemError(ENOENT); + default: + throw NetFS::SystemError(EIO); + } } - [[noreturn]] void ErrorToSystemError(const Error & e) + template<> + [[noreturn]] + void + throwError(int) { - if (e.err == GIT_ENOTFOUND) { - throw NetFS::SystemError(ENOENT); - } - throw NetFS::SystemError(EIO); + throw NetFS::ConfigError(); + } + + git_oid OidParse(const std::string_view & str) + { + git_oid oid; + gitSafe(git_oid_fromstrn, &oid, str.data(), str.length()); + return oid; + } + + RepositoryPtr RepositoryOpenBare(const std::string & path) + { + return gitSafeGet( + git_repository_open_bare, git_repository_free, path.c_str()); + } + + BlobPtr BlobLookup(const RepositoryPtr & repo, const git_oid & blob) + { + return gitSafeGet( + git_blob_lookup, git_blob_free, repo.get(), &blob); + } + + CommitPtr CommitLookup(const RepositoryPtr & repo, const git_oid & commitId) + { + return gitSafeGet( + git_commit_lookup, git_commit_free, repo.get(), &commitId); } + TreePtr TreeLookup(const RepositoryPtr & repo, const git_oid & treeId) + { + return gitSafeGet( + git_tree_lookup, git_tree_free, repo.get(), &treeId); + } + + TreeEntryPtr TreeEntryByPath(const TreePtr & tree, const std::string & path) + { + return gitSafeGet( + git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1); + } } } diff --git a/src/git.h b/src/git.h index 9107f8b..56a7cf6 100644 --- a/src/git.h +++ b/src/git.h @@ -7,81 +7,60 @@ namespace GitFS { namespace Git { - struct Error { - int err; - int klass; - const char * message; - }; - void throwError(int err); - - template + template + [[noreturn]] void throwError(int err); + + template void gitSafe(int (*func)(P...), A ... p) { if (int _giterror = func(p...); _giterror != 0) { - throwError(_giterror); + throwError(_giterror); } } - [[noreturn]] void ErrorToSystemError(const Error & e); - template using TPtr = std::shared_ptr; - template + template auto gitSafeGet(int(*get)(R**, P...), void(*release)(R*), A ... p) { R * r = nullptr; - gitSafe(get, &r, p...); + gitSafe(get, &r, p...); return TPtr(r, release); } - template + template auto gitSafeGet(int(*get)(R**, P...), A ... p) { R * r = nullptr; - gitSafe(get, &r, p...); + gitSafe(get, &r, p...); return r; } - inline auto OidParse(const std::string_view & str) - { - git_oid oid; - gitSafe(git_oid_fromstrn, &oid, str.data(), str.length()); - return oid; - } + git_oid OidParse(const std::string_view & str); - inline auto RepositoryOpenBare(const std::string & path) - { - return gitSafeGet(git_repository_open_bare, git_repository_free, path.c_str()); - } - using RepositoryPtr = decltype(RepositoryOpenBare("")); + using RepositoryPtr = decltype(gitSafeGet( + git_repository_open_bare, git_repository_free, nullptr)); + RepositoryPtr RepositoryOpenBare(const std::string & path); - inline auto BlobLookup(const RepositoryPtr & repo, const git_oid & blob) - { - return gitSafeGet(git_blob_lookup, git_blob_free, repo.get(), &blob); - } - using BlobPtr = decltype(BlobLookup({}, {})); + using BlobPtr = decltype(gitSafeGet( + git_blob_lookup, git_blob_free, nullptr, nullptr)); + BlobPtr BlobLookup(const RepositoryPtr & repo, const git_oid & blob); - inline auto CommitLookup(const RepositoryPtr & repo, const git_oid & commitId) - { - return gitSafeGet(git_commit_lookup, git_commit_free, repo.get(), &commitId); - } - using CommitPtr = decltype(CommitLookup({}, {})); + using CommitPtr = decltype(gitSafeGet( + git_commit_lookup, git_commit_free, nullptr, nullptr)); + CommitPtr CommitLookup(const RepositoryPtr & repo, const git_oid & commitId); - inline auto TreeLookup(const RepositoryPtr & repo, const git_oid & treeId) - { - return gitSafeGet(git_tree_lookup, git_tree_free, repo.get(), &treeId); - } - using TreePtr = decltype(TreeLookup({}, {})); + using TreePtr = decltype(gitSafeGet( + git_tree_lookup, git_tree_free, nullptr, nullptr)); + TreePtr TreeLookup(const RepositoryPtr & repo, const git_oid & treeId); - inline auto TreeEntryByPath(const TreePtr & tree, const std::string & path) - { - return gitSafeGet(git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1); - } - using TreeEntryPtr = decltype(TreeEntryByPath({}, {})); + using TreeEntryPtr = decltype(gitSafeGet( + git_tree_entry_bypath, git_tree_entry_free, nullptr, nullptr)); + TreeEntryPtr TreeEntryByPath(const TreePtr & tree, const std::string & path); } } diff --git a/src/repo.cpp b/src/repo.cpp index 0db5b23..32fa401 100644 --- a/src/repo.cpp +++ b/src/repo.cpp @@ -58,11 +58,8 @@ GitFS::Repo::access(ReqEnv, ::std::string path, int mode, const ::Ice::Current&) return 0; } - catch (const Git::Error & e) { - if (e.err == GIT_ENOTFOUND) { - return ENOENT; - } - Git::ErrorToSystemError(e); + catch (const NetFS::SystemError & e) { + return e.syserrno; } } @@ -72,26 +69,21 @@ GitFS::Repo::getattr(ReqEnv, ::std::string path, const ::Ice::Current&) { if (path.empty()) throw NetFS::SystemError(EINVAL); - try { - NetFS::Attr a {}; - if (path == "/") { - a.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; - } - else { - auto entry = Git::TreeEntryByPath(tree, path); - a << *entry; - if (S_ISREG(git_tree_entry_filemode(entry.get()))) { - auto blob = Git::BlobLookup(repo, *git_tree_entry_id(entry.get())); - a << *blob; - } - } - a << *commit; - a.gid = a.uid = "root"; - return a; + NetFS::Attr a {}; + if (path == "/") { + a.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; } - catch (const Git::Error & e) { - Git::ErrorToSystemError(e); + else { + auto entry = Git::TreeEntryByPath(tree, path); + a << *entry; + if (S_ISREG(git_tree_entry_filemode(entry.get()))) { + auto blob = Git::BlobLookup(repo, *git_tree_entry_id(entry.get())); + a << *blob; + } } + a << *commit; + a.gid = a.uid = "root"; + return a; } @@ -100,16 +92,11 @@ GitFS::Repo::readlink(ReqEnv, ::std::string path, const ::Ice::Current&) { if (path.empty() || path == "/") throw NetFS::SystemError(EINVAL); - try { - auto e = Git::TreeEntryByPath(tree, path); - if (!S_ISLNK(git_tree_entry_filemode(e.get()))) throw NetFS::SystemError(EINVAL); - auto blob = Git::BlobLookup(repo, *git_tree_entry_id(e.get())); - auto n = static_cast(git_blob_rawcontent(blob.get())); - return { n, n + git_blob_rawsize(blob.get()) }; - } - catch (const Git::Error & e) { - Git::ErrorToSystemError(e); - } + auto e = Git::TreeEntryByPath(tree, path); + if (!S_ISLNK(git_tree_entry_filemode(e.get()))) throw NetFS::SystemError(EINVAL); + auto blob = Git::BlobLookup(repo, *git_tree_entry_id(e.get())); + auto n = static_cast(git_blob_rawcontent(blob.get())); + return { n, n + git_blob_rawsize(blob.get()) }; } -- cgit v1.2.3