| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 3 | #include "sys_file.hpp" | 
|  | 4 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 5 | #include <unistd.h> | 
|  | 6 |  | 
| Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame^] | 7 | #include <blobs-ipmid/blobs.hpp> | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 8 | #include <cstdint> | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 9 | #include <memory> | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 10 | #include <string> | 
|  | 11 | #include <vector> | 
|  | 12 |  | 
| Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 13 | #include "binaryblob.pb.h" | 
|  | 14 |  | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 15 | using std::size_t; | 
|  | 16 | using std::uint16_t; | 
|  | 17 | using std::uint32_t; | 
|  | 18 | using std::uint64_t; | 
|  | 19 | using std::uint8_t; | 
|  | 20 |  | 
|  | 21 | namespace binstore | 
|  | 22 | { | 
|  | 23 |  | 
|  | 24 | /** | 
|  | 25 | * @class BinaryStoreInterface is an abstraction for a storage location. | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 26 | *     Each instance would be uniquely identified by a baseId string. | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 27 | */ | 
|  | 28 | class BinaryStoreInterface | 
|  | 29 | { | 
|  | 30 | public: | 
|  | 31 | virtual ~BinaryStoreInterface() = default; | 
|  | 32 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 33 | /** | 
|  | 34 | * @returns baseId string of the storage. | 
|  | 35 | */ | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 36 | virtual std::string getBaseBlobId() const = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 37 |  | 
|  | 38 | /** | 
|  | 39 | * @returns List of all open blob IDs, plus the base. | 
|  | 40 | */ | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 41 | virtual std::vector<std::string> getBlobIds() const = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 42 |  | 
|  | 43 | /** | 
|  | 44 | * Opens a blob given its name. If there is no one, create one. | 
|  | 45 | * @param blobId: The blob id to operate on. | 
|  | 46 | * @param flags: Either read flag or r/w flag has to be specified. | 
|  | 47 | * @returns True if open/create successfully. | 
|  | 48 | */ | 
| Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 49 | virtual bool openOrCreateBlob(const std::string& blobId, | 
|  | 50 | uint16_t flags) = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 51 |  | 
|  | 52 | /** | 
|  | 53 | * Deletes a blob given its name. If there is no one, | 
|  | 54 | * @param blobId: The blob id to operate on. | 
|  | 55 | * @returns True if deleted. | 
|  | 56 | */ | 
| Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 57 | virtual bool deleteBlob(const std::string& blobId) = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 58 |  | 
|  | 59 | /** | 
|  | 60 | * Reads data from the currently opened blob. | 
|  | 61 | * @param offset: offset into the blob to read | 
|  | 62 | * @param requestedSize: how many bytes to read | 
|  | 63 | * @returns Bytes able to read. Returns empty if nothing can be read or | 
|  | 64 | *          if there is no open blob. | 
|  | 65 | */ | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 66 | virtual std::vector<uint8_t> read(uint32_t offset, | 
|  | 67 | uint32_t requestedSize) = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 68 |  | 
|  | 69 | /** | 
|  | 70 | * Writes data to the currently openend blob. | 
|  | 71 | * @param offset: offset into the blob to write | 
|  | 72 | * @param data: bytes to write | 
|  | 73 | * @returns True if able to write the entire data successfully | 
|  | 74 | */ | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 75 | virtual bool write(uint32_t offset, const std::vector<uint8_t>& data) = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 76 |  | 
|  | 77 | /** | 
| Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 78 | * Commits data to the persistent storage specified during blob init. | 
|  | 79 | * @returns True if able to write data to sysfile successfully | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 80 | */ | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 81 | virtual bool commit() = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 82 |  | 
|  | 83 | /** | 
| Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 84 | * Closes blob, which prevents further modifications. Uncommitted data will | 
|  | 85 | * be lost. | 
|  | 86 | * @returns True if able to close the blob successfully | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 87 | */ | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 88 | virtual bool close() = 0; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 89 |  | 
|  | 90 | /** | 
| Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame^] | 91 | * Returns blob stat flags. | 
|  | 92 | * @param meta: output stat flags. | 
|  | 93 | * @returns True if able to get the stat flags and write to *meta | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 94 | */ | 
| Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame^] | 95 | virtual bool stat(blobs::BlobMeta* meta) = 0; | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 96 | }; | 
|  | 97 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 98 | /** | 
|  | 99 | * @class BinaryStore instantiates a concrete implementation of | 
|  | 100 | *     BinaryStoreInterface. The dependency on file is injected through its | 
|  | 101 | *     constructor. | 
|  | 102 | */ | 
|  | 103 | class BinaryStore : public BinaryStoreInterface | 
|  | 104 | { | 
|  | 105 | public: | 
| Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame^] | 106 | /* |CommitState| differs slightly with |StateFlags| in blob.hpp, | 
|  | 107 | * and thus is defined in the OEM space (bit 8 - 15). User can call stat() | 
|  | 108 | * to query the |CommitState| of the blob path. */ | 
|  | 109 | enum CommitState | 
|  | 110 | { | 
|  | 111 | Dirty = (1 << 8), // In-memory data might not match persisted data | 
|  | 112 | Clean = (1 << 9), // In-memory data matches persisted data | 
|  | 113 | Uninitialized = (1 << 10), // Cannot find persisted data | 
|  | 114 | CommitError = (1 << 11)    // Error happened during committing | 
|  | 115 | }; | 
|  | 116 |  | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 117 | BinaryStore() = delete; | 
|  | 118 | BinaryStore(const std::string& baseBlobId, std::unique_ptr<SysFile> file, | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 119 | uint32_t maxSize) : | 
|  | 120 | baseBlobId_(baseBlobId), | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 121 | file_(std::move(file)), maxSize_(maxSize) | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 122 | { | 
| Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 123 | blob_.set_blob_base_id(baseBlobId_); | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 126 | ~BinaryStore() = default; | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 127 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 128 | BinaryStore(const BinaryStore&) = delete; | 
|  | 129 | BinaryStore& operator=(const BinaryStore&) = delete; | 
|  | 130 | BinaryStore(BinaryStore&&) = default; | 
|  | 131 | BinaryStore& operator=(BinaryStore&&) = default; | 
|  | 132 |  | 
|  | 133 | std::string getBaseBlobId() const override; | 
|  | 134 | std::vector<std::string> getBlobIds() const override; | 
|  | 135 | bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override; | 
|  | 136 | bool deleteBlob(const std::string& blobId) override; | 
|  | 137 | std::vector<uint8_t> read(uint32_t offset, uint32_t requestedSize) override; | 
|  | 138 | bool write(uint32_t offset, const std::vector<uint8_t>& data) override; | 
|  | 139 | bool commit() override; | 
|  | 140 | bool close() override; | 
| Kun Yi | 1a25e0d | 2020-05-11 12:28:53 -0700 | [diff] [blame^] | 141 | bool stat(blobs::BlobMeta* meta) override; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 142 |  | 
|  | 143 | /** | 
|  | 144 | * Helper factory method to create a BinaryStore instance | 
|  | 145 | * @param baseBlobId: base id for the created instance | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 146 | * @param sysFile: system file object for storing binary | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 147 | * @param maxSize: max size in bytes that this BinaryStore can expand to. | 
|  | 148 | *     Writing data more than allowed size will return failure. | 
|  | 149 | * @returns unique_ptr to constructed BinaryStore. Caller should take | 
|  | 150 | *     ownership of the instance. | 
|  | 151 | */ | 
|  | 152 | static std::unique_ptr<BinaryStoreInterface> | 
|  | 153 | createFromConfig(const std::string& baseBlobId, | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 154 | std::unique_ptr<SysFile> file, uint32_t maxSize); | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 155 |  | 
|  | 156 | private: | 
| Kun Yi | 97be3af | 2019-03-05 22:43:41 -0800 | [diff] [blame] | 157 | /* Load the serialized data from sysfile if commit state is dirty. | 
|  | 158 | * Returns False if encountered error when loading */ | 
|  | 159 | bool loadSerializedData(); | 
|  | 160 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 161 | std::string baseBlobId_; | 
| Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 162 | binaryblobproto::BinaryBlobBase blob_; | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 163 | binaryblobproto::BinaryBlob* currentBlob_ = nullptr; | 
| Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 164 | bool writable_ = false; | 
| Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame] | 165 | std::unique_ptr<SysFile> file_ = nullptr; | 
|  | 166 | uint32_t maxSize_; | 
| Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 167 | CommitState commitState_ = CommitState::Dirty; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 168 | }; | 
|  | 169 |  | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 170 | } // namespace binstore |