blob: 764b9fb690fabab14dbc7a366285fcf1cd930bea [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{
Kun Yi6967b772019-02-22 13:48:12 -080078 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 Yi64dc05c2018-12-19 13:19:03 -080098 return result;
99}
100
101bool BinaryStore::write(uint32_t offset, const std::vector<uint8_t>& data)
102{
Kun Yi6967b772019-02-22 13:48:12 -0800103 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 Yi64dc05c2018-12-19 13:19:03 -0800128}
129
130bool BinaryStore::commit()
131{
132 return false;
133}
134
135bool BinaryStore::close()
136{
Kun Yi6baa7132019-01-08 21:21:02 -0800137 currentBlob_ = nullptr;
138 writable_ = false;
139 return true;
Kun Yi64dc05c2018-12-19 13:19:03 -0800140}
141
142bool BinaryStore::stat()
143{
144 return false;
145}
146
147} // namespace binstore