diff --git a/include/dwarfs/file_access.h b/include/dwarfs/file_access.h index 9dd002ca..f1a0b14c 100644 --- a/include/dwarfs/file_access.h +++ b/include/dwarfs/file_access.h @@ -49,11 +49,20 @@ class file_access { virtual ~file_access() = default; virtual bool exists(std::filesystem::path const& path) const = 0; + + virtual std::unique_ptr + open_input(std::filesystem::path const& path) const = 0; virtual std::unique_ptr open_input(std::filesystem::path const& path, std::error_code& ec) const = 0; + + virtual std::unique_ptr + open_input_binary(std::filesystem::path const& path) const = 0; virtual std::unique_ptr open_input_binary(std::filesystem::path const& path, std::error_code& ec) const = 0; + + virtual std::unique_ptr + open_output_binary(std::filesystem::path const& path) const = 0; virtual std::unique_ptr open_output_binary(std::filesystem::path const& path, std::error_code& ec) const = 0; diff --git a/src/dwarfs/file_access_generic.cpp b/src/dwarfs/file_access_generic.cpp index db5106b7..68803170 100644 --- a/src/dwarfs/file_access_generic.cpp +++ b/src/dwarfs/file_access_generic.cpp @@ -23,6 +23,8 @@ #include #include +#include + #ifdef _WIN32 #include #endif @@ -103,6 +105,17 @@ class file_access_generic : public file_access { return rv; } + std::unique_ptr + open_input(std::filesystem::path const& path) const override { + std::error_code ec; + auto rv = open_input(path, ec); + if (ec) { + throw std::system_error(ec, + fmt::format("open_input('{}')", path.string())); + } + return rv; + } + std::unique_ptr open_input_binary(std::filesystem::path const& path, std::error_code& ec) const override { @@ -113,6 +126,17 @@ class file_access_generic : public file_access { return rv; } + std::unique_ptr + open_input_binary(std::filesystem::path const& path) const override { + std::error_code ec; + auto rv = open_input_binary(path, ec); + if (ec) { + throw std::system_error( + ec, fmt::format("open_input_binary('{}')", path.string())); + } + return rv; + } + std::unique_ptr open_output_binary(std::filesystem::path const& path, std::error_code& ec) const override { @@ -122,6 +146,17 @@ class file_access_generic : public file_access { } return rv; } + + std::unique_ptr + open_output_binary(std::filesystem::path const& path) const override { + std::error_code ec; + auto rv = open_output_binary(path, ec); + if (ec) { + throw std::system_error( + ec, fmt::format("open_output_binary('{}')", path.string())); + } + return rv; + } }; } // namespace diff --git a/test/test_helpers.h b/test/test_helpers.h index 92e7de54..30cfb993 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -168,14 +168,23 @@ class test_terminal : public terminal { class test_file_access : public file_access { public: bool exists(std::filesystem::path const& path) const override; + std::unique_ptr open_input(std::filesystem::path const& path, std::error_code& ec) const override; + std::unique_ptr + open_input(std::filesystem::path const& path) const override; + std::unique_ptr open_input_binary(std::filesystem::path const& path, std::error_code& ec) const override; + std::unique_ptr + open_input_binary(std::filesystem::path const& path) const override; + std::unique_ptr open_output_binary(std::filesystem::path const& path, std::error_code& ec) const override; + std::unique_ptr + open_output_binary(std::filesystem::path const& path) const override; void set_file(std::filesystem::path const& path, std::string contents) const; std::optional get_file(std::filesystem::path const& path) const; diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index 4f59d046..fda8b0db 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -84,12 +84,33 @@ test_file_access::open_input(std::filesystem::path const& path, return nullptr; } +std::unique_ptr +test_file_access::open_input(std::filesystem::path const& path) const { + std::error_code ec; + auto rv = open_input(path, ec); + if (ec) { + throw std::system_error(ec, fmt::format("open_input('{}')", path.string())); + } + return rv; +} + std::unique_ptr test_file_access::open_input_binary(std::filesystem::path const& path, std::error_code& ec) const { return open_input(path, ec); } +std::unique_ptr +test_file_access::open_input_binary(std::filesystem::path const& path) const { + std::error_code ec; + auto rv = open_input_binary(path, ec); + if (ec) { + throw std::system_error( + ec, fmt::format("open_input_binary('{}')", path.string())); + } + return rv; +} + std::unique_ptr test_file_access::open_output_binary(std::filesystem::path const& path, std::error_code& ec) const { @@ -100,6 +121,17 @@ test_file_access::open_output_binary(std::filesystem::path const& path, return rv; } +std::unique_ptr +test_file_access::open_output_binary(std::filesystem::path const& path) const { + std::error_code ec; + auto rv = open_output_binary(path, ec); + if (ec) { + throw std::system_error( + ec, fmt::format("open_output_binary('{}')", path.string())); + } + return rv; +} + void test_file_access::set_file(std::filesystem::path const& path, std::string content) const { files_[path] = std::move(content);