Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 15f0f94 | 2020-07-09 09:38:18 -0700 | [diff] [blame] | 3 | #include "binarystore_interface.hpp" |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 4 | #include "sys_file.hpp" |
| 5 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 8 | #include <blobs-ipmid/blobs.hpp> |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 9 | #include <cstdint> |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame^] | 10 | #include <map> |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 11 | #include <memory> |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 12 | #include <optional> |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | using std::size_t; |
| 17 | using std::uint16_t; |
| 18 | using std::uint32_t; |
| 19 | using std::uint64_t; |
| 20 | using std::uint8_t; |
| 21 | |
| 22 | namespace binstore |
| 23 | { |
| 24 | |
| 25 | /** |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 26 | * @class BinaryStore instantiates a concrete implementation of |
| 27 | * BinaryStoreInterface. The dependency on file is injected through its |
| 28 | * constructor. |
| 29 | */ |
| 30 | class BinaryStore : public BinaryStoreInterface |
| 31 | { |
| 32 | public: |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 33 | /* |CommitState| differs slightly with |StateFlags| in blob.hpp, |
| 34 | * and thus is defined in the OEM space (bit 8 - 15). User can call stat() |
| 35 | * to query the |CommitState| of the blob path. */ |
| 36 | enum CommitState |
| 37 | { |
| 38 | Dirty = (1 << 8), // In-memory data might not match persisted data |
| 39 | Clean = (1 << 9), // In-memory data matches persisted data |
| 40 | Uninitialized = (1 << 10), // Cannot find persisted data |
| 41 | CommitError = (1 << 11) // Error happened during committing |
| 42 | }; |
| 43 | |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 44 | BinaryStore() = delete; |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 45 | BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file, |
| 46 | std::optional<uint32_t> maxSize = std::nullopt) : |
| 47 | baseBlobId_(baseBlobId), |
| 48 | file_(std::move(file)), maxSize(maxSize) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 52 | BinaryStore(std::unique_ptr<SysFile> file, bool readOnly = false, |
| 53 | std::optional<uint32_t> maxSize = std::nullopt) : |
| 54 | readOnly_{readOnly}, |
| 55 | file_(std::move(file)), maxSize(maxSize) |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 59 | ~BinaryStore() = default; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 60 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 61 | BinaryStore(const BinaryStore&) = delete; |
| 62 | BinaryStore& operator=(const BinaryStore&) = delete; |
| 63 | BinaryStore(BinaryStore&&) = default; |
| 64 | BinaryStore& operator=(BinaryStore&&) = default; |
| 65 | |
| 66 | std::string getBaseBlobId() const override; |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 67 | bool setBaseBlobId(const std::string& baseBlobId) override; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 68 | std::vector<std::string> getBlobIds() const override; |
| 69 | bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override; |
| 70 | bool deleteBlob(const std::string& blobId) override; |
| 71 | std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override; |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 72 | std::vector<uint8_t> readBlob(const std::string& blobId) const override; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 73 | bool write(uint32_t offset, const std::vector<uint8_t>& data) override; |
| 74 | bool commit() override; |
| 75 | bool close() override; |
Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame] | 76 | bool stat(blobs::BlobMeta* meta) override; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Helper factory method to create a BinaryStore instance |
| 80 | * @param baseBlobId: base id for the created instance |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 81 | * @param sysFile: system file object for storing binary |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 82 | * @returns unique_ptr to constructed BinaryStore. Caller should take |
| 83 | * ownership of the instance. |
| 84 | */ |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 85 | static std::unique_ptr<BinaryStoreInterface> createFromConfig( |
| 86 | const std::string& baseBlobId, std::unique_ptr<SysFile> file, |
| 87 | std::optional<uint32_t> maxSize = std::nullopt, |
| 88 | std::optional<std::string> aliasBlobBaseId = std::nullopt); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 89 | |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Helper factory method to create a BinaryStore instance |
| 92 | * This function should be used with existing stores. It reads |
| 93 | * the baseBlobId name from the storage. |
| 94 | * @param sysFile: system file object for storing binary |
| 95 | * @param readOnly: if true, open the store in read only mode |
| 96 | * @returns unique_ptr to constructed BinaryStore. |
| 97 | */ |
| 98 | static std::unique_ptr<BinaryStoreInterface> |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 99 | createFromFile(std::unique_ptr<SysFile> file, bool readOnly = true, |
| 100 | std::optional<uint32_t> maxSize = std::nullopt); |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 101 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 102 | private: |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 103 | /* Load the serialized data from sysfile if commit state is dirty. |
| 104 | * Returns False if encountered error when loading */ |
Willy Tu | 7f10780 | 2023-11-06 23:05:25 -0800 | [diff] [blame] | 105 | bool loadSerializedData( |
| 106 | std::optional<std::string> aliasBlobBaseId = std::nullopt); |
Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 107 | |
William A. Kennington III | 7e14586 | 2024-02-01 15:56:33 -0800 | [diff] [blame^] | 108 | std::map<std::string, std::vector<std::uint8_t>> blobs_; |
| 109 | std::string baseBlobId_, currentBlob_; |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 110 | /* True if current blob is writable */ |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 111 | bool writable_ = false; |
Maksym Sloyko | eb27411 | 2021-10-27 23:48:32 +0000 | [diff] [blame] | 112 | /* True if the entire store (not just individual blobs) is read only */ |
| 113 | bool readOnly_ = false; |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 114 | std::unique_ptr<SysFile> file_ = nullptr; |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 115 | CommitState commitState_ = CommitState::Dirty; |
Willy Tu | 67391d5 | 2021-12-07 19:53:49 -0800 | [diff] [blame] | 116 | std::optional<uint32_t> maxSize; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 119 | } // namespace binstore |