Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "image_handler.hpp" |
| 4 | |
| 5 | #include <cstdint> |
| 6 | #include <fstream> |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 11 | namespace ipmi_flash |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 12 | { |
| 13 | |
| 14 | class FileHandler : public ImageHandlerInterface |
| 15 | { |
| 16 | public: |
| 17 | /** |
| 18 | * Create a FileHandler. This object is basically a filewriter. |
| 19 | * |
| 20 | * @param[in] filename - file to use for the contents, fully |
| 21 | * qualified file system path. |
| 22 | */ |
| 23 | explicit FileHandler(const std::string& filename) : filename(filename) |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 24 | {} |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 25 | |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 26 | bool open(const std::string& path, |
| 27 | std::ios_base::openmode mode = std::ios::out) override; |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 28 | void close() override; |
| 29 | bool write(std::uint32_t offset, |
| 30 | const std::vector<std::uint8_t>& data) override; |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 31 | virtual std::optional<std::vector<uint8_t>> |
| 32 | read(std::uint32_t offset, std::uint32_t size) override; |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 33 | int getSize() override; |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 34 | |
| 35 | private: |
| 36 | /** the active hash path, ignore. */ |
| 37 | std::string path; |
| 38 | |
| 39 | /** The file handle. */ |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 40 | std::fstream file; |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 41 | |
| 42 | /** The filename (including path) to use to write bytes. */ |
| 43 | std::string filename; |
| 44 | }; |
| 45 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 46 | } // namespace ipmi_flash |