1 // Copyright 2021 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 #include "handler_config.hpp"
17 #include "image_handler.hpp"
18 #include "status.hpp"
19 #include "util.hpp"
20 
21 #include <blobs-ipmid/blobs.hpp>
22 
23 #include <cstdint>
24 #include <map>
25 #include <memory>
26 #include <optional>
27 #include <set>
28 #include <string>
29 #include <string_view>
30 #include <unordered_map>
31 #include <vector>
32 
33 namespace ipmi_flash
34 {
35 
36 class LogBlobHandler : public blobs::GenericBlobInterface
37 {
38   public:
39     struct ActionPack
40     {
41         /** Only file operation action supported currently */
42         std::unique_ptr<TriggerableActionInterface> onOpen;
43         std::unique_ptr<TriggerableActionInterface> onDelete;
44     };
45 
46     /**
47      * Create a LogBlobHandler.
48      *
49      * @param[in] configs - list of blob configurations to support
50      */
51     LogBlobHandler(std::vector<HandlerConfig<ActionPack>>&& configs);
52 
53     ~LogBlobHandler() = default;
54     LogBlobHandler(const LogBlobHandler&) = delete;
55     LogBlobHandler& operator=(const LogBlobHandler&) = delete;
56     LogBlobHandler(LogBlobHandler&&) = default;
57     LogBlobHandler& operator=(LogBlobHandler&&) = default;
58 
59     bool canHandleBlob(const std::string& path) override;
60     std::vector<std::string> getBlobIds() override;
61     bool deleteBlob(const std::string& path) override;
62     bool stat(const std::string&, blobs::BlobMeta* meta) override;
63     bool open(uint16_t session, uint16_t flags,
64               const std::string& path) override;
65     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
66                               uint32_t requestedSize) override;
write(uint16_t,uint32_t,const std::vector<uint8_t> &)67     bool write(uint16_t, uint32_t, const std::vector<uint8_t>&) override
68     {
69         return false; /* not supported */
70     };
writeMeta(uint16_t,uint32_t,const std::vector<uint8_t> &)71     bool writeMeta(uint16_t, uint32_t, const std::vector<uint8_t>&) override
72     {
73         return false; /* not supported */
74     }
commit(uint16_t,const std::vector<uint8_t> &)75     bool commit(uint16_t, const std::vector<uint8_t>&) override
76     {
77         return false; // not supported
78     }
79     bool close(uint16_t session) override;
80     bool stat(uint16_t session, blobs::BlobMeta* meta) override;
81     bool expire(uint16_t session) override;
82 
83   private:
84     struct SessionInfo;
85 
86     struct BlobInfo
87     {
88         Pinned<std::string> blobId;
89         std::unique_ptr<ActionPack> actions;
90         std::unique_ptr<ImageHandlerInterface> handler;
91         std::set<SessionInfo*> sessionsToUpdate;
92     };
93 
94     struct SessionInfo
95     {
96         BlobInfo* blob;
97 
98         // A cached copy of the version data shared by all clients for a single
99         // execution of the version retrieval action. This is is null until the
100         // TriggerableAction has completed. If the action is an error, the
101         // shared object is nullopt. Otherwise, contains a vector of the version
102         // data when successfully read.
103         std::shared_ptr<const std::optional<std::vector<uint8_t>>> data;
104     };
105 
106     std::unordered_map<std::string_view, std::unique_ptr<BlobInfo>> blobInfoMap;
107     std::unordered_map<uint16_t, std::unique_ptr<SessionInfo>> sessionInfoMap;
108 };
109 
110 } // namespace ipmi_flash
111