Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 1 | #include "binarystore.hpp" |
| 2 | |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | #include <blobs-ipmid/blobs.hpp> |
Kun Yi | d2d8cb6 | 2019-01-10 15:06:12 -0800 | [diff] [blame] | 7 | #include <cstdint> |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "binaryblob.pb.h" |
| 13 | |
| 14 | using std::size_t; |
| 15 | using std::uint16_t; |
| 16 | using std::uint32_t; |
| 17 | using std::uint64_t; |
| 18 | using std::uint8_t; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 19 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 20 | namespace binstore |
| 21 | { |
| 22 | |
| 23 | std::unique_ptr<BinaryStoreInterface> |
| 24 | BinaryStore::createFromConfig(const std::string& baseBlobId, |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 25 | std::unique_ptr<SysFile> file, |
| 26 | uint32_t maxSize) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 27 | { |
Kun Yi | 2765b64 | 2019-01-16 11:11:24 -0800 | [diff] [blame^] | 28 | if (baseBlobId.empty() || !file) |
| 29 | { |
| 30 | return nullptr; |
| 31 | } |
| 32 | |
| 33 | auto store = |
| 34 | std::make_unique<BinaryStore>(baseBlobId, std::move(file), maxSize); |
| 35 | |
| 36 | store->blob_.set_blob_base_id(store->baseBlobId_); |
| 37 | |
| 38 | return std::move(store); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | std::string BinaryStore::getBaseBlobId() const |
| 42 | { |
| 43 | return baseBlobId_; |
| 44 | } |
| 45 | |
| 46 | std::vector<std::string> BinaryStore::getBlobIds() const |
| 47 | { |
| 48 | std::vector<std::string> result; |
| 49 | result.push_back(baseBlobId_); |
| 50 | |
Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 51 | for (const auto& blob : blob_.blobs()) |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 52 | { |
Kun Yi | 0a940b9 | 2019-01-07 16:33:11 -0800 | [diff] [blame] | 53 | result.push_back(blob.blob_id()); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | bool BinaryStore::openOrCreateBlob(const std::string& blobId, uint16_t flags) |
| 60 | { |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 61 | if (!(flags & blobs::OpenFlags::read)) |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if (currentBlob_ && (currentBlob_->blob_id() != blobId)) |
| 67 | { |
| 68 | /* Already handling a different blob */ |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | writable_ = flags & blobs::OpenFlags::write; |
| 73 | |
| 74 | /* Iterate and find if there is an existing blob with this id. |
| 75 | * blobsPtr points to a BinaryBlob container with STL-like semantics*/ |
| 76 | auto blobsPtr = blob_.mutable_blobs(); |
| 77 | auto blobIt = |
| 78 | std::find_if(blobsPtr->begin(), blobsPtr->end(), |
| 79 | [&](const auto& b) { return b.blob_id() == blobId; }); |
| 80 | |
| 81 | if (blobIt != blobsPtr->end()) |
| 82 | { |
| 83 | currentBlob_ = &(*blobIt); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /* Otherwise, create the blob and append it */ |
| 88 | currentBlob_ = blob_.add_blobs(); |
| 89 | currentBlob_->set_blob_id(blobId); |
| 90 | |
| 91 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | bool BinaryStore::deleteBlob(const std::string& blobId) |
| 95 | { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | std::vector<uint8_t> BinaryStore::read(uint32_t offset, uint32_t requestedSize) |
| 100 | { |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 101 | std::vector<uint8_t> result; |
| 102 | |
| 103 | if (!currentBlob_) |
| 104 | { |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | auto dataPtr = currentBlob_->mutable_data(); |
| 109 | |
| 110 | /* If it is out of bound, return empty vector */ |
| 111 | if (offset >= dataPtr->size()) |
| 112 | { |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | uint32_t requestedEndPos = offset + requestedSize; |
| 117 | |
| 118 | result = std::vector<uint8_t>( |
| 119 | dataPtr->begin() + offset, |
| 120 | std::min(dataPtr->begin() + requestedEndPos, dataPtr->end())); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 121 | return result; |
| 122 | } |
| 123 | |
| 124 | bool BinaryStore::write(uint32_t offset, const std::vector<uint8_t>& data) |
| 125 | { |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame] | 126 | if (!currentBlob_) |
| 127 | { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if (!writable_) |
| 132 | { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | auto dataPtr = currentBlob_->mutable_data(); |
| 137 | |
| 138 | if (offset > dataPtr->size()) |
| 139 | { |
| 140 | /* Will leave a gap with undefined data */ |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | /* Copy (overwrite) the data */ |
| 145 | if (offset + data.size() > dataPtr->size()) |
| 146 | { |
| 147 | dataPtr->resize(offset + data.size()); // not enough space, extend |
| 148 | } |
| 149 | std::copy(data.begin(), data.end(), dataPtr->begin() + offset); |
| 150 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool BinaryStore::commit() |
| 154 | { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool BinaryStore::close() |
| 159 | { |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 160 | currentBlob_ = nullptr; |
| 161 | writable_ = false; |
| 162 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | bool BinaryStore::stat() |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | } // namespace binstore |