diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2019-07-22 20:08:39 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2019-07-22 20:08:39 +0100 |
commit | 6168d85a886286a43b55a0cd6739a1e1e3987d90 (patch) | |
tree | 36360a309a20b6ab344f25ec80b902d2e27fbad5 | |
parent | Basically all the core functionality (diff) | |
download | netfs-gitfs-6168d85a886286a43b55a0cd6739a1e1e3987d90.tar.bz2 netfs-gitfs-6168d85a886286a43b55a0cd6739a1e1e3987d90.tar.xz netfs-gitfs-6168d85a886286a43b55a0cd6739a1e1e3987d90.zip |
Unline git helpers and throw NetFS exceptions
-rw-r--r-- | src/blob.cpp | 13 | ||||
-rw-r--r-- | src/blob.h | 1 | ||||
-rw-r--r-- | src/dir.cpp | 11 | ||||
-rw-r--r-- | src/git.cpp | 62 | ||||
-rw-r--r-- | src/git.h | 71 | ||||
-rw-r--r-- | src/repo.cpp | 53 |
6 files changed, 99 insertions, 112 deletions
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<const char *>(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 { @@ -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<NetFS::SystemError>(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<NetFS::ConfigError>(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<NetFS::ConfigError>(git_oid_fromstrn, &oid, str.data(), str.length()); + return oid; + } + + RepositoryPtr RepositoryOpenBare(const std::string & path) + { + return gitSafeGet<NetFS::ConfigError>( + git_repository_open_bare, git_repository_free, path.c_str()); + } + + BlobPtr BlobLookup(const RepositoryPtr & repo, const git_oid & blob) + { + return gitSafeGet<NetFS::SystemError>( + git_blob_lookup, git_blob_free, repo.get(), &blob); + } + + CommitPtr CommitLookup(const RepositoryPtr & repo, const git_oid & commitId) + { + return gitSafeGet<NetFS::ConfigError>( + git_commit_lookup, git_commit_free, repo.get(), &commitId); } + TreePtr TreeLookup(const RepositoryPtr & repo, const git_oid & treeId) + { + return gitSafeGet<NetFS::SystemError>( + git_tree_lookup, git_tree_free, repo.get(), &treeId); + } + + TreeEntryPtr TreeEntryByPath(const TreePtr & tree, const std::string & path) + { + return gitSafeGet<NetFS::SystemError>( + git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1); + } } } @@ -7,81 +7,60 @@ namespace GitFS { namespace Git { - struct Error { - int err; - int klass; - const char * message; - }; - void throwError(int err); - - template<typename ... P, typename ... A> + template<typename E> + [[noreturn]] void throwError(int err); + + template<typename E, typename ... P, typename ... A> void gitSafe(int (*func)(P...), A ... p) { if (int _giterror = func(p...); _giterror != 0) { - throwError(_giterror); + throwError<E>(_giterror); } } - [[noreturn]] void ErrorToSystemError(const Error & e); - template<typename T> using TPtr = std::shared_ptr<T>; - template<typename R, typename ... P, typename ... A> + template<typename E, typename R, typename ... P, typename ... A> auto gitSafeGet(int(*get)(R**, P...), void(*release)(R*), A ... p) { R * r = nullptr; - gitSafe(get, &r, p...); + gitSafe<E>(get, &r, p...); return TPtr<R>(r, release); } - template<typename R, typename ... P, typename ... A> + template<typename E, typename R, typename ... P, typename ... A> auto gitSafeGet(int(*get)(R**, P...), A ... p) { R * r = nullptr; - gitSafe(get, &r, p...); + gitSafe<E>(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<std::exception>( + 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<std::exception>( + 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<std::exception>( + 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<std::exception>( + 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<std::exception>( + 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<const ::std::string::value_type *>(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<const ::std::string::value_type *>(git_blob_rawcontent(blob.get())); + return { n, n + git_blob_rawsize(blob.get()) }; } |