diff --git a/internal/vfs/impl/file_proxy.hpp b/internal/vfs/impl/file_proxy.hpp index da65b89..5d0aeca 100644 --- a/internal/vfs/impl/file_proxy.hpp +++ b/internal/vfs/impl/file_proxy.hpp @@ -39,15 +39,12 @@ class TypedFileProxy } [[nodiscard]] std::shared_ptr origin() override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - if(auto* proxy = dynamic_cast(this->origin_.get()); proxy != nullptr) { - return proxy->origin(); - } - - return this->origin_; + auto rst = this->mutable_origin_(); + if(auto* proxy = dynamic_cast(rst.get()); proxy != nullptr) { + return proxy->origin(); } + + return rst; } [[nodiscard]] std::filesystem::perms perms() const override { @@ -55,11 +52,7 @@ class TypedFileProxy } void perms(std::filesystem::perms prms, std::filesystem::perm_options opts) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->perms(prms, opts); - } + return this->mutable_origin_()->perms(prms, opts); } bool operator==(File const& other) const override { @@ -75,14 +68,18 @@ class TypedFileProxy } void last_write_time(std::filesystem::file_time_type new_time) override { + return this->mutable_origin_()->last_write_time(new_time); + } + + protected: + std::shared_ptr>& mutable_origin_() { if constexpr(std::is_const_v) { throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); } else { - return this->origin_->last_write_time(new_time); + return this->origin_; } } - protected: std::shared_ptr origin_; }; @@ -97,11 +94,7 @@ class RegularFileProxy: public TypedFileProxy { } void resize(std::uintmax_t new_size) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->resize(new_size); - } + return this->mutable_origin_()->resize(new_size); } [[nodiscard]] std::shared_ptr open_read(std::ios_base::openmode mode) const override { @@ -109,11 +102,7 @@ class RegularFileProxy: public TypedFileProxy { } std::shared_ptr open_write(std::ios_base::openmode mode) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->open_write(mode); - } + return this->mutable_origin_()->open_write(mode); } }; @@ -137,75 +126,39 @@ class DirectoryProxy: public TypedFileProxy { } std::pair, bool> emplace_regular_file(std::string const& name) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->emplace_regular_file(name); - } + return this->mutable_origin_()->emplace_regular_file(name); } std::pair, bool> emplace_directory(std::string const& name) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->emplace_directory(name); - } + return this->mutable_origin_()->emplace_directory(name); } - std::pair, bool> emplace_symlink(std::string const& name, std::filesystem::path origin) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->emplace_symlink(name, std::move(origin)); - } + std::pair, bool> emplace_symlink(std::string const& name, std::filesystem::path target) override { + return this->mutable_origin_()->emplace_symlink(name, std::move(target)); } bool link(std::string const& name, std::shared_ptr file) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->link(name, std::move(file)); - } + return this->mutable_origin_()->link(name, std::move(file)); } bool unlink(std::string const& name) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->unlink(name); - } + return this->mutable_origin_()->unlink(name); } void mount(std::string const& name, std::shared_ptr file) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->mount(name, std::move(file)); - } + return this->mutable_origin_()->mount(name, std::move(file)); } void unmount(std::string const& name) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->unmount(name); - } + return this->mutable_origin_()->unmount(name); } std::uintmax_t erase(std::string const& name) override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->erase(name); - } + return this->mutable_origin_()->erase(name); } std::uintmax_t clear() override { - if constexpr(std::is_const_v) { - throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system)); - } else { - return this->origin_->clear(); - } + return this->mutable_origin_()->clear(); } [[nodiscard]] std::shared_ptr cursor() const override {