blob: 98a6264028b75ce21bd31d0dd750d15562fcd15c [file] [log] [blame]
Kun Yi64dc05c2018-12-19 13:19:03 -08001#include "binarystore.hpp"
2
Kun Yid2d8cb62019-01-10 15:06:12 -08003#include <unistd.h>
4
Kun Yi6baa7132019-01-08 21:21:02 -08005#include <algorithm>
6#include <blobs-ipmid/blobs.hpp>
Kun Yid2d8cb62019-01-10 15:06:12 -08007#include <cstdint>
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "binaryblob.pb.h"
13
14using std::size_t;
15using std::uint16_t;
16using std::uint32_t;
17using std::uint64_t;
18using std::uint8_t;
Kun Yi6baa7132019-01-08 21:21:02 -080019
Kun Yi64dc05c2018-12-19 13:19:03 -080020namespace binstore
21{
22
23std::unique_ptr<BinaryStoreInterface>
24 BinaryStore::createFromConfig(const std::string& baseBlobId,
Kun Yi2765b642019-01-16 11:11:24 -080025 std::unique_ptr<SysFile> file,
26 uint32_t maxSize)
Kun Yi64dc05c2018-12-19 13:19:03 -080027{
Kun Yi2765b642019-01-16 11:11:24 -080028 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 Yi64dc05c2018-12-19 13:19:03 -080039}
40
41std::string BinaryStore::getBaseBlobId() const
42{
43 return baseBlobId_;
44}
45
46std::vector<std::string> BinaryStore::getBlobIds() const
47{
48 std::vector<std::string> result;
49 result.push_back(baseBlobId_);
50
Kun Yi0a940b92019-01-07 16:33:11 -080051 for (const auto& blob : blob_.blobs())
Kun Yi64dc05c2018-12-19 13:19:03 -080052 {
Kun Yi0a940b92019-01-07 16:33:11 -080053 result.push_back(blob.blob_id());
Kun Yi64dc05c2018-12-19 13:19:03 -080054 }
55
56 return result;
57}
58
59bool BinaryStore::openOrCreateBlob(const std::string& blobId, uint16_t flags)
60{
Kun Yi6baa7132019-01-08 21:21:02 -080061 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 Yi64dc05c2018-12-19 13:19:03 -080092}
93
94bool BinaryStore::deleteBlob(const std::string& blobId)
95{
96 return false;
97}
98
99std::vector<uint8_t> BinaryStore::read(uint32_t offset, uint32_t requestedSize)
100{
Kun Yi6967b772019-02-22 13:48:12 -0800101 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 Yi64dc05c2018-12-19 13:19:03 -0800121 return result;
122}
123
124bool BinaryStore::write(uint32_t offset, const std::vector<uint8_t>& data)
125{
Kun Yi6967b772019-02-22 13:48:12 -0800126 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 Yi64dc05c2018-12-19 13:19:03 -0800151}
152
153bool BinaryStore::commit()
154{
155 return false;
156}
157
158bool BinaryStore::close()
159{
Kun Yi6baa7132019-01-08 21:21:02 -0800160 currentBlob_ = nullptr;
161 writable_ = false;
162 return true;
Kun Yi64dc05c2018-12-19 13:19:03 -0800163}
164
165bool BinaryStore::stat()
166{
167 return false;
168}
169
170} // namespace binstore