From 3c954e76ed24a067413309746f9347a24538c1f3 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Tue, 30 Aug 2022 15:51:29 -0400 Subject: [PATCH] Extend to allow vending of only portions of files. --- Storage/MassStorage/Formats/RawSectorDump.hpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Storage/MassStorage/Formats/RawSectorDump.hpp b/Storage/MassStorage/Formats/RawSectorDump.hpp index 60d0e7314..cb7fc19d6 100644 --- a/Storage/MassStorage/Formats/RawSectorDump.hpp +++ b/Storage/MassStorage/Formats/RawSectorDump.hpp @@ -17,12 +17,15 @@ namespace Storage { namespace MassStorage { -template class RawSectorDump: public MassStorageDevice { +template class RawSectorDump: public MassStorageDevice { public: - RawSectorDump(const std::string &file_name) : file_(file_name) { + RawSectorDump(const std::string &file_name, long offset = 0, long length = -1) : + file_(file_name), + file_size_((length == -1) ? long(file_.stats().st_size) : length), + file_start_(offset) + { // Is the file a multiple of sector_size bytes in size? - const auto file_size = size_t(file_.stats().st_size); - if(file_size % sector_size) throw std::exception(); + if(file_size_ % sector_size) throw std::exception(); } /* MassStorageDevices overrides. */ @@ -31,22 +34,23 @@ template class RawSectorDump: public MassStorageDevice { } size_t get_number_of_blocks() final { - return size_t(file_.stats().st_size) / sector_size; + return size_t(file_size_ / sector_size); } std::vector get_block(size_t address) final { - file_.seek(long(address * sector_size), SEEK_SET); + file_.seek(file_start_ + long(address * sector_size), SEEK_SET); return file_.read(sector_size); } void set_block(size_t address, const std::vector &contents) final { assert(contents.size() == sector_size); - file_.seek(long(address * sector_size), SEEK_SET); + file_.seek(file_start_ + long(address * sector_size), SEEK_SET); file_.write(contents); } private: FileHolder file_; + const long file_size_, file_start_; }; }