From fca0f83e2ab85960552b584818dd45ab72778133 Mon Sep 17 00:00:00 2001 From: Jason Phan Date: Wed, 30 Dec 2020 23:12:02 -0600 Subject: [PATCH] memory: Add FileOffset back --- vmm/memory/detail/guest.hpp | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/vmm/memory/detail/guest.hpp b/vmm/memory/detail/guest.hpp index ac3751e..01d3066 100644 --- a/vmm/memory/detail/guest.hpp +++ b/vmm/memory/detail/guest.hpp @@ -72,4 +72,55 @@ class MemoryRegionAddress : public Address size_type m_addr{}; }; +// The starting point of a file which backs a GuestMemoryRegion. +class FileOffset +{ + private: + std::shared_ptr m_fstream; + long m_start{}; + public: + FileOffset(const char* filename, std::ios_base::openmode mode, + const long start={}) + : m_fstream{std::make_shared(filename, mode)}, + m_start{start} + { + if (start) { + // TODO (?): Only call these if the appropriate openmode is set. + m_fstream->seekg(start); + m_fstream->seekp(start); + } + } + + explicit FileOffset(const char* filename, const long start={}) + : FileOffset(filename, std::ios_base::in | std::ios_base::out, start) {} + + explicit FileOffset(const std::string& filename, const long start=0) + : FileOffset(filename.c_str(), start) {} + + FileOffset(const std::string& filename, + const std::ios_base::openmode mode, + const long start={}) + : FileOffset(filename.c_str(), mode, start) {} + + explicit FileOffset(const std::filesystem::path& filename, const long start=0) + : FileOffset(filename.c_str(), start) {} + + FileOffset(const std::filesystem::path& filename, + const std::ios_base::openmode mode, + const long start={}) + : FileOffset(filename.c_str(), mode, start) {} + + // TODO: thread-safety + [[nodiscard]] auto data() const -> std::basic_filebuf* + { + return m_fstream->rdbuf(); + } + + [[nodiscard]] constexpr auto start() const noexcept -> long + { + return m_start; + } +}; + } // vmm::memory::detail -- 2.45.2