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