blob: 1e8475c27b24993bbee92d0c3de052d5fcb4a155 [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,
25 const std::string& sysfilePath,
26 uint32_t offset, uint32_t maxSize)
27{
28 // TODO: implement sysFile parsing
29 return std::make_unique<BinaryStore>(baseBlobId, 0, offset, maxSize);
30}
31
32std::string BinaryStore::getBaseBlobId() const
33{
34 return baseBlobId_;
35}
36
37std::vector<std::string> BinaryStore::getBlobIds() const
38{
39 std::vector<std::string> result;
40 result.push_back(baseBlobId_);
41
Kun Yi0a940b92019-01-07 16:33:11 -080042 for (const auto& blob : blob_.blobs())
Kun Yi64dc05c2018-12-19 13:19:03 -080043 {
Kun Yi0a940b92019-01-07 16:33:11 -080044 result.push_back(blob.blob_id());
Kun Yi64dc05c2018-12-19 13:19:03 -080045 }
46
47 return result;
48}
49
50bool BinaryStore::openOrCreateBlob(const std::string& blobId, uint16_t flags)
51{
Kun Yi6baa7132019-01-08 21:21:02 -080052 if (!(flags & blobs::OpenFlags::read))
53 {
54 return false;
55 }
56
57 if (currentBlob_ && (currentBlob_->blob_id() != blobId))
58 {
59 /* Already handling a different blob */
60 return false;
61 }
62
63 writable_ = flags & blobs::OpenFlags::write;
64
65 /* Iterate and find if there is an existing blob with this id.
66 * blobsPtr points to a BinaryBlob container with STL-like semantics*/
67 auto blobsPtr = blob_.mutable_blobs();
68 auto blobIt =
69 std::find_if(blobsPtr->begin(), blobsPtr->end(),
70 [&](const auto& b) { return b.blob_id() == blobId; });
71
72 if (blobIt != blobsPtr->end())
73 {
74 currentBlob_ = &(*blobIt);
75 return true;
76 }
77
78 /* Otherwise, create the blob and append it */
79 currentBlob_ = blob_.add_blobs();
80 currentBlob_->set_blob_id(blobId);
81
82 return true;
Kun Yi64dc05c2018-12-19 13:19:03 -080083}
84
85bool BinaryStore::deleteBlob(const std::string& blobId)
86{
87 return false;
88}
89
90std::vector<uint8_t> BinaryStore::read(uint32_t offset, uint32_t requestedSize)
91{
Kun Yi6967b772019-02-22 13:48:12 -080092 std::vector<uint8_t> result;
93
94 if (!currentBlob_)
95 {
96 return result;
97 }
98
99 auto dataPtr = currentBlob_->mutable_data();
100
101 /* If it is out of bound, return empty vector */
102 if (offset >= dataPtr->size())
103 {
104 return result;
105 }
106
107 uint32_t requestedEndPos = offset + requestedSize;
108
109 result = std::vector<uint8_t>(
110 dataPtr->begin() + offset,
111 std::min(dataPtr->begin() + requestedEndPos, dataPtr->end()));
Kun Yi64dc05c2018-12-19 13:19:03 -0800112 return result;
113}
114
115bool BinaryStore::write(uint32_t offset, const std::vector<uint8_t>& data)
116{
Kun Yi6967b772019-02-22 13:48:12 -0800117 if (!currentBlob_)
118 {
119 return false;
120 }
121
122 if (!writable_)
123 {
124 return false;
125 }
126
127 auto dataPtr = currentBlob_->mutable_data();
128
129 if (offset > dataPtr->size())
130 {
131 /* Will leave a gap with undefined data */
132 return false;
133 }
134
135 /* Copy (overwrite) the data */
136 if (offset + data.size() > dataPtr->size())
137 {
138 dataPtr->resize(offset + data.size()); // not enough space, extend
139 }
140 std::copy(data.begin(), data.end(), dataPtr->begin() + offset);
141 return true;
Kun Yi64dc05c2018-12-19 13:19:03 -0800142}
143
144bool BinaryStore::commit()
145{
146 return false;
147}
148
149bool BinaryStore::close()
150{
Kun Yi6baa7132019-01-08 21:21:02 -0800151 currentBlob_ = nullptr;
152 writable_ = false;
153 return true;
Kun Yi64dc05c2018-12-19 13:19:03 -0800154}
155
156bool BinaryStore::stat()
157{
158 return false;
159}
160
161} // namespace binstore