1 #pragma once
2 #include "handler_config.hpp"
3 #include "image_handler.hpp"
4 #include "status.hpp"
5 #include "util.hpp"
6 
7 #include <blobs-ipmid/blobs.hpp>
8 
9 #include <cstdint>
10 #include <map>
11 #include <memory>
12 #include <optional>
13 #include <set>
14 #include <string>
15 #include <string_view>
16 #include <unordered_map>
17 #include <vector>
18 
19 namespace ipmi_flash
20 {
21 
22 class VersionBlobHandler : public blobs::GenericBlobInterface
23 {
24   public:
25     struct ActionPack
26     {
27         /** Only file operation action supported currently */
28         std::unique_ptr<TriggerableActionInterface> onOpen;
29     };
30 
31     /**
32      * Create a VersionBlobHandler.
33      *
34      * @param[in] configs - list of blob configurations to support
35      */
36     VersionBlobHandler(std::vector<HandlerConfig<ActionPack>>&& configs);
37 
38     ~VersionBlobHandler() = default;
39     VersionBlobHandler(const VersionBlobHandler&) = delete;
40     VersionBlobHandler& operator=(const VersionBlobHandler&) = delete;
41     VersionBlobHandler(VersionBlobHandler&&) = default;
42     VersionBlobHandler& operator=(VersionBlobHandler&&) = default;
43 
44     bool canHandleBlob(const std::string& path) override;
45     std::vector<std::string> getBlobIds() override;
46     bool deleteBlob(const std::string& path) override;
47     bool stat(const std::string&, blobs::BlobMeta* meta) override;
48     bool open(uint16_t session, uint16_t flags,
49               const std::string& path) override;
50     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
51                               uint32_t requestedSize) override;
52     bool write(uint16_t session, uint32_t offset,
53                const std::vector<uint8_t>& data) override
54     {
55         return false; /* not supported */
56     };
57     bool writeMeta(uint16_t session, uint32_t offset,
58                    const std::vector<uint8_t>& data) override
59     {
60         return false; /* not supported */
61     }
62     bool commit(uint16_t session, const std::vector<uint8_t>& data) override
63     {
64         return false; // not supported
65     }
66     bool close(uint16_t session) override;
67     bool stat(uint16_t session, blobs::BlobMeta* meta) override;
68     bool expire(uint16_t session) override;
69 
70   private:
71     struct SessionInfo;
72 
73     struct BlobInfo
74     {
75         Pinned<std::string> blobId;
76         std::unique_ptr<ActionPack> actions;
77         std::unique_ptr<ImageHandlerInterface> handler;
78         std::set<SessionInfo*> sessionsToUpdate;
79     };
80 
81     struct SessionInfo
82     {
83         BlobInfo* blob;
84 
85         // A cached copy of the version data shared by all clients for a single
86         // execution of the version retrieval action. This is is null until the
87         // TriggerableAction has completed. If the action is an error, the
88         // shared object is nullopt. Otherwise, contains a vector of the version
89         // data when successfully read.
90         std::shared_ptr<const std::optional<std::vector<uint8_t>>> data;
91     };
92 
93     std::unordered_map<std::string_view, std::unique_ptr<BlobInfo>> blobInfoMap;
94     std::unordered_map<uint16_t, std::unique_ptr<SessionInfo>> sessionInfoMap;
95 };
96 
97 } // namespace ipmi_flash
98