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 | { |
| 78 | std::vector<std::uint8_t> result; |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | bool BinaryStore::write(uint32_t offset, const std::vector<uint8_t>& data) |
| 83 | { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | bool BinaryStore::commit() |
| 88 | { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | bool BinaryStore::close() |
| 93 | { |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame^] | 94 | currentBlob_ = nullptr; |
| 95 | writable_ = false; |
| 96 | return true; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool BinaryStore::stat() |
| 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | } // namespace binstore |