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