blob: 884271c000f65266ff806e420ccc7dfbbdc1e534 [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#pragma once
2
Patrick Ventureaceb4ba2018-09-27 14:50:37 -07003#include <blobs-ipmid/blobs.hpp>
Patrick Ventureef3aead2018-09-12 08:53:29 -07004#include <ctime>
5#include <memory>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10namespace blobs
11{
12
13struct SessionInfo
14{
15 SessionInfo() = default;
16 SessionInfo(const std::string& path, GenericBlobInterface* handler,
17 uint16_t flags) :
18 blobId(path),
19 handler(handler), flags(flags)
20 {
21 }
22 ~SessionInfo() = default;
23
24 std::string blobId;
25 GenericBlobInterface* handler;
26 uint16_t flags;
27};
28
29class ManagerInterface
30{
31 public:
32 virtual ~ManagerInterface() = default;
33
34 virtual bool
35 registerHandler(std::unique_ptr<GenericBlobInterface> handler) = 0;
36
37 virtual uint32_t buildBlobList() = 0;
38
39 virtual std::string getBlobId(uint32_t index) = 0;
40
41 virtual bool open(uint16_t flags, const std::string& path,
42 uint16_t* session) = 0;
43
44 virtual bool stat(const std::string& path, struct BlobMeta* meta) = 0;
45
46 virtual bool stat(uint16_t session, struct BlobMeta* meta) = 0;
47
48 virtual bool commit(uint16_t session, const std::vector<uint8_t>& data) = 0;
49
50 virtual bool close(uint16_t session) = 0;
51
52 virtual std::vector<uint8_t> read(uint16_t session, uint32_t offset,
53 uint32_t requestedSize) = 0;
54
55 virtual bool write(uint16_t session, uint32_t offset,
56 const std::vector<uint8_t>& data) = 0;
57
58 virtual bool deleteBlob(const std::string& path) = 0;
59};
60
61/**
62 * Blob Manager used to store handlers and sessions.
63 */
64class BlobManager : public ManagerInterface
65{
66 public:
67 BlobManager()
68 {
69 next = static_cast<uint16_t>(std::time(nullptr));
70 };
71
72 ~BlobManager() = default;
73 /* delete copy constructor & assignment operator, only support move
74 * operations.
75 */
76 BlobManager(const BlobManager&) = delete;
77 BlobManager& operator=(const BlobManager&) = delete;
78 BlobManager(BlobManager&&) = default;
79 BlobManager& operator=(BlobManager&&) = default;
80
81 /**
82 * Register a handler. We own the pointer.
83 *
84 * @param[in] handler - a pointer to a blob handler.
85 * @return bool - true if registered.
86 */
87 bool
88 registerHandler(std::unique_ptr<GenericBlobInterface> handler) override;
89
90 /**
91 * Builds the blobId list for enumeration.
92 *
93 * @return lowest value returned is 0, otherwise the number of
94 * blobIds.
95 */
96 uint32_t buildBlobList() override;
97
98 /**
99 * Grabs the blobId for the indexed blobId.
100 *
101 * @param[in] index - the index into the blobId cache.
102 * @return string - the blobId or empty string on failure.
103 */
104 std::string getBlobId(uint32_t index) override;
105
106 /**
107 * Attempts to open the file specified and associates with a session.
108 *
109 * @param[in] flags - the flags to pass to open.
110 * @param[in] path - the file path to open.
111 * @param[in,out] session - pointer to store the session on success.
112 * @return bool - true if able to open.
113 */
114 bool open(uint16_t flags, const std::string& path,
115 uint16_t* session) override;
116
117 /**
118 * Attempts to retrieve a BlobMeta for the specified path.
119 *
120 * @param[in] path - the file path for stat().
121 * @param[in,out] meta - a pointer to store the metadata.
122 * @return bool - true if able to retrieve the information.
123 */
124 bool stat(const std::string& path, struct BlobMeta* meta) override;
125
126 /**
127 * Attempts to retrieve a BlobMeta for a given session.
128 *
129 * @param[in] session - the session for this command.
130 * @param[in,out] meta - a pointer to store the metadata.
131 * @return bool - true if able to retrieve the information.
132 */
133 bool stat(uint16_t session, struct BlobMeta* meta) override;
134
135 /**
136 * Attempt to commit a blob for a given session.
137 *
138 * @param[in] session - the session for this command.
139 * @param[in] data - an optional commit blob.
140 * @return bool - true if the commit succeeds.
141 */
142 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
143
144 /**
145 * Attempt to close a session. If the handler returns a failure
146 * in closing, the session is kept open.
147 *
148 * @param[in] session - the session for this command.
149 * @return bool - true if the session was closed.
150 */
151 bool close(uint16_t session) override;
152
153 /**
154 * Attempt to read bytes from the blob. If there's a failure, such as
155 * an invalid offset it'll just return 0 bytes.
156 *
157 * @param[in] session - the session for this command.
158 * @param[in] offset - the offset from which to read.
159 * @param[in] requestedSize - the number of bytes to try and read.
160 * @return the bytes read.
161 */
162 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
163 uint32_t requestedSize) override;
164
165 /**
166 * Attempt to write to a blob. The manager does not track whether
167 * the session opened the file for writing.
168 *
169 * @param[in] session - the session for this command.
170 * @param[in] offset - the offset into the blob to write.
171 * @param[in] data - the bytes to write to the blob.
172 * @return bool - true if the write succeeded.
173 */
174 bool write(uint16_t session, uint32_t offset,
175 const std::vector<uint8_t>& data) override;
176
177 /**
178 * Attempt to delete a blobId. This method will just call the
179 * handler, which will return failure if the blob doesn't support
180 * deletion. This command will also fail if there are any open
181 * sessions against the specific blob.
182 *
183 * In the case where they specify a folder, such as /blob/skm where
184 * the "real" blobIds are /blob/skm/1, or /blob/skm/2, the manager
185 * may see there are on open sessions to that specific path and will
186 * call the handler. In this case, the handler is responsible for
187 * handling any checks or logic.
188 *
189 * @param[in] path - the blobId path.
190 * @return bool - true if delete was successful.
191 */
192 bool deleteBlob(const std::string& path) override;
193
194 /**
195 * Attempts to return a valid unique session id.
196 *
197 * @param[in,out] - pointer to the session.
198 * @return bool - true if able to allocate.
199 */
200 bool getSession(uint16_t* session);
201
202 /**
203 * Given a file path will return first handler to answer that it owns
204 * it.
205 *
206 * @param[in] path - the file path.
207 * @return pointer to the handler or nullptr if not found.
208 */
209 GenericBlobInterface* getHandler(const std::string& path);
210
211 /**
212 * Given a session id will return associated handler.
213 *
214 * @param[in] session - the session.
215 * @return pointer to the handler or nullptr if not found.
216 */
217 GenericBlobInterface* getHandler(uint16_t session);
218
219 /**
220 * Given a session id will return associated metadata, including
221 * the handler and the flags passed into open.
222 *
223 * @param[in] session - the session.
224 * @return pointer to the information or nullptr if not found.
225 */
226 SessionInfo* getSessionInfo(uint16_t session);
227
228 /**
229 * Given a session id will return associated path.
230 *
231 * @param[in] session - the session.
232 * @return the path or "" on failure.
233 */
234 std::string getPath(uint16_t session) const;
235
236 private:
237 void incrementOpen(const std::string& path);
238 void decrementOpen(const std::string& path);
239 int getOpen(const std::string& path) const;
240
241 /* The next session ID to use */
242 uint16_t next;
243 /* Temporary list of blobIds used for enumeration. */
244 std::vector<std::string> ids;
245 /* List of Blob handler. */
246 std::vector<std::unique_ptr<GenericBlobInterface>> handlers;
247 /* Mapping of session ids to blob handlers and the path used with open.
248 */
249 std::unordered_map<uint16_t, SessionInfo> sessions;
250 /* Mapping of open blobIds */
251 std::unordered_map<std::string, int> openFiles;
252};
Patrick Ventureb3e07e22018-09-27 15:11:57 -0700253
254/**
255 * @brief Gets a handle to the BlobManager.
256 *
257 * @return a pointer to the BlobManager instance.
258 */
Patrick Venture73eb6872018-10-01 18:37:34 -0700259ManagerInterface* getBlobManager();
Patrick Ventureb3e07e22018-09-27 15:11:57 -0700260
Patrick Ventureef3aead2018-09-12 08:53:29 -0700261} // namespace blobs