Added MappingFileSystemAdapter and adjusted some more stuff in the VFS module.
This commit is contained in:
parent
8f2cee4968
commit
6407c5ca09
@ -30,13 +30,6 @@ namespace mijin
|
||||
// public functions
|
||||
//
|
||||
|
||||
std::vector<fs::path> OSFileSystemAdapter::getRoots()
|
||||
{
|
||||
return {
|
||||
"/" // TODO: other OSs
|
||||
};
|
||||
}
|
||||
|
||||
fs::path OSFileSystemAdapter::getHomeFolder()
|
||||
{
|
||||
return getKnownFolder(KnownFolder::USER_HOME);
|
||||
|
@ -79,7 +79,6 @@ class FileSystemAdapter
|
||||
public:
|
||||
virtual ~FileSystemAdapter() = default;
|
||||
|
||||
[[nodiscard]] virtual std::vector<fs::path> getRoots() = 0;
|
||||
[[nodiscard]] virtual fs::path getHomeFolder() = 0;
|
||||
[[nodiscard]] virtual std::vector<FileInfo> listFiles(const fs::path& folder) = 0;
|
||||
[[nodiscard]] virtual FileInfo getFileInfo(const fs::path& file) = 0;
|
||||
@ -92,7 +91,6 @@ public:
|
||||
class OSFileSystemAdapter : public FileSystemAdapter
|
||||
{
|
||||
public:
|
||||
std::vector<fs::path> getRoots() override;
|
||||
fs::path getHomeFolder() override;
|
||||
std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
||||
FileInfo getFileInfo(const fs::path& file) override;
|
||||
@ -153,6 +151,30 @@ inline std::string formatFileSize(std::size_t sizeInBytes)
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
namespace vfs_pipe
|
||||
{
|
||||
template<typename TReal, typename TAdapter = FileSystemAdapter>
|
||||
struct Builder
|
||||
{
|
||||
using adapter_t = TAdapter;
|
||||
|
||||
operator std::unique_ptr<FileSystemAdapter>()
|
||||
{
|
||||
return static_cast<TReal&>(*this).build();
|
||||
}
|
||||
};
|
||||
|
||||
struct OSBuilder : Builder<OSBuilder, OSFileSystemAdapter>
|
||||
{
|
||||
std::unique_ptr<OSFileSystemAdapter> build()
|
||||
{
|
||||
return std::make_unique<OSFileSystemAdapter>();
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
inline OSBuilder os() noexcept { return {}; }
|
||||
}
|
||||
} // namespace mijin
|
||||
|
||||
template<>
|
||||
|
175
source/mijin/virtual_filesystem/mapping.hpp
Normal file
175
source/mijin/virtual_filesystem/mapping.hpp
Normal file
@ -0,0 +1,175 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(MIJIN_VIRTUAL_FILESYSTEM_MAPPING_HPP_INCLUDED)
|
||||
#define MIJIN_VIRTUAL_FILESYSTEM_MAPPING_HPP_INCLUDED 1
|
||||
|
||||
#include <algorithm>
|
||||
#include "./filesystem.hpp"
|
||||
#include "../internal/common.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
|
||||
//
|
||||
// public defines
|
||||
//
|
||||
|
||||
//
|
||||
// public constants
|
||||
//
|
||||
|
||||
//
|
||||
// public types
|
||||
//
|
||||
|
||||
template<typename TWrapped>
|
||||
class MappingFileSystemAdapter : public FileSystemAdapter
|
||||
{
|
||||
private:
|
||||
TWrapped wrapped_;
|
||||
fs::path root_;
|
||||
public:
|
||||
template<typename... TArgs>
|
||||
explicit MappingFileSystemAdapter(fs::path root, TArgs&&... args)
|
||||
: wrapped_(std::forward<TArgs>(args)...), root_(std::move(root)) {}
|
||||
MappingFileSystemAdapter(const MappingFileSystemAdapter&) = default;
|
||||
MappingFileSystemAdapter(MappingFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
|
||||
|
||||
MappingFileSystemAdapter& operator=(const MappingFileSystemAdapter&) = default;
|
||||
MappingFileSystemAdapter& operator=(MappingFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
|
||||
|
||||
fs::path getHomeFolder() override;
|
||||
std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
||||
FileInfo getFileInfo(const fs::path& file) override;
|
||||
Optional<fs::path> getNativePath(const fs::path& file) override;
|
||||
StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
||||
private:
|
||||
bool adjustPath(fs::path& path) const MIJIN_NOEXCEPT;
|
||||
};
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
|
||||
template<typename TWrapped>
|
||||
fs::path MappingFileSystemAdapter<TWrapped>::getHomeFolder()
|
||||
{
|
||||
return "/";
|
||||
}
|
||||
|
||||
template<typename TWrapped>
|
||||
std::vector<FileInfo> MappingFileSystemAdapter<TWrapped>::listFiles(const fs::path& folder)
|
||||
{
|
||||
fs::path adjusted = folder;
|
||||
if (!adjustPath(adjusted))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<FileInfo> result;
|
||||
result = wrapped_.listFiles(adjusted);
|
||||
for (FileInfo& fileInfo : result)
|
||||
{
|
||||
fileInfo.path = root_ / fileInfo.path;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename TWrapped>
|
||||
FileInfo MappingFileSystemAdapter<TWrapped>::getFileInfo(const fs::path& file)
|
||||
{
|
||||
fs::path adjusted = file;
|
||||
if (!adjustPath(adjusted))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
return wrapped_.getFileInfo(adjusted);
|
||||
}
|
||||
|
||||
template<typename TWrapped>
|
||||
Optional<fs::path> MappingFileSystemAdapter<TWrapped>::getNativePath(const fs::path& file)
|
||||
{
|
||||
fs::path adjusted = file;
|
||||
if (!adjustPath(adjusted))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
return wrapped_.getNativePath(adjusted);
|
||||
}
|
||||
|
||||
template<typename TWrapped>
|
||||
StreamError MappingFileSystemAdapter<TWrapped>::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||
{
|
||||
fs::path adjusted = path;
|
||||
if (!adjustPath(adjusted))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
return wrapped_.open(adjusted, mode, outStream);
|
||||
}
|
||||
|
||||
template<typename TWrapped>
|
||||
bool MappingFileSystemAdapter<TWrapped>::adjustPath(fs::path& path) const MIJIN_NOEXCEPT
|
||||
{
|
||||
if (!path.is_absolute())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// checks whether path starts with root
|
||||
auto [itRoot, itPath] = std::mismatch(root_.begin(), root_.end(), path.begin(), path.end());
|
||||
if (itRoot != root_.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::error_code error;
|
||||
fs::path wrapped = fs::relative(path, root_, error);
|
||||
|
||||
if (wrapped.empty() || error)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
path = std::move(wrapped);
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace vfs_pipe
|
||||
{
|
||||
template<typename TBase>
|
||||
struct MappingBuilder : Builder<MappingBuilder<TBase>, MappingFileSystemAdapter<typename TBase::adapter_t>>
|
||||
{
|
||||
fs::path root;
|
||||
TBase base;
|
||||
|
||||
std::unique_ptr<MappingFileSystemAdapter<typename TBase::adapter_t>> build()
|
||||
{
|
||||
return std::make_unique<MappingFileSystemAdapter<typename TBase::adapter_t>>(root, std::move(*base.build()));
|
||||
}
|
||||
};
|
||||
|
||||
struct MappingOptions
|
||||
{
|
||||
fs::path root;
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
inline MappingOptions map_to(fs::path root) noexcept
|
||||
{
|
||||
return {.root = std::move(root) };
|
||||
}
|
||||
|
||||
template<typename TBase>
|
||||
[[nodiscard]]
|
||||
MappingBuilder<TBase> operator|(TBase other, MappingOptions options) noexcept
|
||||
{
|
||||
return {.root = std::move(options.root), .base = std::move(other) };
|
||||
}
|
||||
}
|
||||
} // namespace mijin
|
||||
|
||||
#endif // !defined(MIJIN_VIRTUAL_FILESYSTEM_MAPPING_HPP_INCLUDED)
|
@ -38,7 +38,6 @@ public:
|
||||
RelativeFileSystemAdapter& operator=(const RelativeFileSystemAdapter&) = default;
|
||||
RelativeFileSystemAdapter& operator=(RelativeFileSystemAdapter&&) MIJIN_NOEXCEPT = default;
|
||||
|
||||
std::vector<fs::path> getRoots() override;
|
||||
fs::path getHomeFolder() override;
|
||||
std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
||||
FileInfo getFileInfo(const fs::path& file) override;
|
||||
@ -52,12 +51,6 @@ private:
|
||||
// public functions
|
||||
//
|
||||
|
||||
template<typename TWrapped>
|
||||
std::vector<fs::path> RelativeFileSystemAdapter<TWrapped>::getRoots()
|
||||
{
|
||||
return { root_ };
|
||||
}
|
||||
|
||||
template<typename TWrapped>
|
||||
fs::path RelativeFileSystemAdapter<TWrapped>::getHomeFolder()
|
||||
{
|
||||
@ -106,6 +99,39 @@ fs::path RelativeFileSystemAdapter<TWrapped>::appendPath(const fs::path& other)
|
||||
}
|
||||
return combinedPath;
|
||||
}
|
||||
|
||||
namespace vfs_pipe
|
||||
{
|
||||
template<typename TBase>
|
||||
struct RelativeBuilder : Builder<RelativeBuilder<TBase>, RelativeFileSystemAdapter<typename TBase::adapter_t>>
|
||||
{
|
||||
fs::path root;
|
||||
TBase base;
|
||||
|
||||
std::unique_ptr<RelativeFileSystemAdapter<typename TBase::adapter_t>> build()
|
||||
{
|
||||
return std::make_unique<RelativeFileSystemAdapter<typename TBase::adapter_t>>(root, std::move(*base.build()));
|
||||
}
|
||||
};
|
||||
|
||||
struct RelativeOptions
|
||||
{
|
||||
fs::path root;
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
inline RelativeOptions relative_to(fs::path root) noexcept
|
||||
{
|
||||
return {.root = std::move(root) };
|
||||
}
|
||||
|
||||
template<typename TBase>
|
||||
[[nodiscard]]
|
||||
RelativeBuilder<TBase> operator|(TBase other, RelativeOptions options) noexcept
|
||||
{
|
||||
return {.root = std::move(options.root), .base = std::move(other) };
|
||||
}
|
||||
}
|
||||
} // namespace mijin
|
||||
|
||||
#endif // !defined(MIJIN_VIRTUAL_FILESYSTEM_RELATIVE_HPP_INCLUDED)
|
||||
|
@ -30,24 +30,6 @@ namespace mijin
|
||||
// public functions
|
||||
//
|
||||
|
||||
std::vector<fs::path> StackedFileSystemAdapter::getRoots()
|
||||
{
|
||||
std::vector<fs::path> roots;
|
||||
|
||||
for (auto& adapter : adapters_)
|
||||
{
|
||||
for (const fs::path& root : adapter->getRoots())
|
||||
{
|
||||
auto it = std::find(roots.begin(), roots.end(), root);
|
||||
if (it == roots.end()) {
|
||||
roots.push_back(root);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return roots;
|
||||
}
|
||||
|
||||
fs::path StackedFileSystemAdapter::getHomeFolder()
|
||||
{
|
||||
if (adapters_.empty()) {
|
||||
@ -105,13 +87,28 @@ Optional<fs::path> StackedFileSystemAdapter::getNativePath(const fs::path& file)
|
||||
|
||||
StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||
{
|
||||
// try to open existing files first
|
||||
for (auto& adapter : adapters_)
|
||||
{
|
||||
const FileInfo fileInfo = adapter->getFileInfo(path);
|
||||
if (fileInfo.exists) {
|
||||
if (fileInfo.exists)
|
||||
{
|
||||
return adapter->open(path, mode, outStream);
|
||||
}
|
||||
}
|
||||
|
||||
// if that doesn't work we attempt to write, try creating it
|
||||
if (mode == FileOpenMode::WRITE || mode == FileOpenMode::READ_WRITE)
|
||||
{
|
||||
for (auto& adapter : adapters_)
|
||||
{
|
||||
const StreamError error = adapter->open(path, mode, outStream);
|
||||
if (error == StreamError::SUCCESS)
|
||||
{
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
return StreamError::IO_ERROR;
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,6 @@ class StackedFileSystemAdapter : public FileSystemAdapter
|
||||
private:
|
||||
std::vector<std::unique_ptr<FileSystemAdapter>> adapters_;
|
||||
public:
|
||||
std::vector<fs::path> getRoots() override;
|
||||
fs::path getHomeFolder() override;
|
||||
std::vector<FileInfo> listFiles(const fs::path& folder) override;
|
||||
FileInfo getFileInfo(const fs::path& file) override;
|
||||
|
Loading…
x
Reference in New Issue
Block a user