blob: 092a069ad3f9c766a9767ba3d0842bae3940e42e [file] [log] [blame]
Kun Yi64dc05c2018-12-19 13:19:03 -08001#include "binarystore.hpp"
2
Kun Yi6baa7132019-01-08 21:21:02 -08003#include <algorithm>
4#include <blobs-ipmid/blobs.hpp>
5
Kun Yi64dc05c2018-12-19 13:19:03 -08006namespace binstore
7{
8
9std::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
18std::string BinaryStore::getBaseBlobId() const
19{
20 return baseBlobId_;
21}
22
23std::vector<std::string> BinaryStore::getBlobIds() const
24{
25 std::vector<std::string> result;
26 result.push_back(baseBlobId_);
27
Kun Yi0a940b92019-01-07 16:33:11 -080028 for (const auto& blob : blob_.blobs())
Kun Yi64dc05c2018-12-19 13:19:03 -080029 {
Kun Yi0a940b92019-01-07 16:33:11 -080030 result.push_back(blob.blob_id());
Kun Yi64dc05c2018-12-19 13:19:03 -080031 }
32
33 return result;
34}
35
36bool BinaryStore::openOrCreateBlob(const std::string& blobId, uint16_t flags)
37{
Kun Yi6baa7132019-01-08 21:21:02 -080038 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 Yi64dc05c2018-12-19 13:19:03 -080069}
70
71bool BinaryStore::deleteBlob(const std::string& blobId)
72{
73 return false;
74}
75
76std::vector<uint8_t> BinaryStore::read(uint32_t offset, uint32_t requestedSize)
77{
78 std::vector<std::uint8_t> result;
79 return result;
80}
81
82bool BinaryStore::write(uint32_t offset, const std::vector<uint8_t>& data)
83{
84 return false;
85}
86
87bool BinaryStore::commit()
88{
89 return false;
90}
91
92bool BinaryStore::close()
93{
Kun Yi6baa7132019-01-08 21:21:02 -080094 currentBlob_ = nullptr;
95 writable_ = false;
96 return true;
Kun Yi64dc05c2018-12-19 13:19:03 -080097}
98
99bool BinaryStore::stat()
100{
101 return false;
102}
103
104} // namespace binstore