Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 1 | #include "version_handler.hpp" |
| 2 | |
William A. Kennington III | cc7f385 | 2021-01-21 19:01:56 -0800 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <cstring> |
| 5 | #include <ios> |
| 6 | #include <limits> |
| 7 | #include <memory> |
| 8 | #include <optional> |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 12 | namespace ipmi_flash |
| 13 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 14 | |
| 15 | VersionBlobHandler::VersionBlobHandler( |
| 16 | std::vector<HandlerConfig<ActionPack>>&& configs) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 17 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 18 | for (auto& config : configs) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 19 | { |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 20 | auto info = std::make_unique<BlobInfo>(); |
| 21 | info->blobId = std::move(config.blobId); |
| 22 | info->actions = std::move(config.actions); |
| 23 | info->handler = std::move(config.handler); |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 24 | info->actions->onOpen->setCallback( |
| 25 | [infoP = info.get()](TriggerableActionInterface& tai) { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 26 | auto data = |
| 27 | std::make_shared<std::optional<std::vector<uint8_t>>>(); |
| 28 | do |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 29 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 30 | if (tai.status() != ActionStatus::success) |
| 31 | { |
| 32 | fprintf(stderr, "Version file unit failed for %s\n", |
| 33 | infoP->blobId.c_str()); |
| 34 | continue; |
| 35 | } |
| 36 | if (!infoP->handler->open("", std::ios::in)) |
| 37 | { |
| 38 | fprintf(stderr, "Opening version file failed for %s\n", |
| 39 | infoP->blobId.c_str()); |
| 40 | continue; |
| 41 | } |
| 42 | auto d = infoP->handler->read( |
| 43 | 0, std::numeric_limits<uint32_t>::max()); |
| 44 | infoP->handler->close(); |
| 45 | if (!d) |
| 46 | { |
| 47 | fprintf(stderr, "Reading version file failed for %s\n", |
| 48 | infoP->blobId.c_str()); |
| 49 | continue; |
| 50 | } |
| 51 | *data = std::move(d); |
| 52 | } while (false); |
| 53 | for (auto sessionP : infoP->sessionsToUpdate) |
Patrick Williams | 1038836 | 2023-05-10 07:51:09 -0500 | [diff] [blame] | 54 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 55 | sessionP->data = data; |
Patrick Williams | 1038836 | 2023-05-10 07:51:09 -0500 | [diff] [blame] | 56 | } |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 57 | infoP->sessionsToUpdate.clear(); |
| 58 | }); |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 59 | if (!blobInfoMap.try_emplace(info->blobId, std::move(info)).second) |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 60 | { |
| 61 | fprintf(stderr, "Ignoring duplicate config for %s\n", |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 62 | info->blobId.c_str()); |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 63 | } |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 64 | } |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool VersionBlobHandler::canHandleBlob(const std::string& path) |
| 68 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 69 | return blobInfoMap.find(path) != blobInfoMap.end(); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | std::vector<std::string> VersionBlobHandler::getBlobIds() |
| 73 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 74 | std::vector<std::string> ret; |
| 75 | for (const auto& [key, _] : blobInfoMap) |
| 76 | { |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 77 | ret.emplace_back(key); |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 78 | } |
| 79 | return ret; |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * deleteBlob - does nothing, always fails |
| 84 | */ |
Willy Tu | b487eb4 | 2021-09-16 21:44:43 -0700 | [diff] [blame] | 85 | bool VersionBlobHandler::deleteBlob(const std::string&) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 86 | { |
| 87 | return false; |
| 88 | } |
| 89 | |
Willy Tu | b487eb4 | 2021-09-16 21:44:43 -0700 | [diff] [blame] | 90 | bool VersionBlobHandler::stat(const std::string&, blobs::BlobMeta*) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 91 | { |
William A. Kennington III | eba0c34 | 2021-01-12 14:23:01 -0800 | [diff] [blame] | 92 | return false; |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool VersionBlobHandler::open(uint16_t session, uint16_t flags, |
| 96 | const std::string& path) |
| 97 | { |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 98 | /* only reads are supported, check if blob is handled and make sure |
| 99 | * the blob isn't already opened |
| 100 | */ |
| 101 | if (flags != blobs::read) |
| 102 | { |
| 103 | fprintf(stderr, "open %s fail: unsupported flags(0x%04X.)\n", |
| 104 | path.c_str(), flags); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 105 | return false; |
| 106 | } |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 107 | |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 108 | auto info = std::make_unique<SessionInfo>(); |
| 109 | info->blob = blobInfoMap.at(path).get(); |
| 110 | info->blob->sessionsToUpdate.emplace(info.get()); |
| 111 | if (info->blob->sessionsToUpdate.size() == 1 && |
| 112 | !info->blob->actions->onOpen->trigger()) |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 113 | { |
| 114 | fprintf(stderr, "open %s fail: onOpen trigger failed\n", path.c_str()); |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 115 | info->blob->sessionsToUpdate.erase(info.get()); |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 116 | return false; |
| 117 | } |
William A. Kennington III | 007c016 | 2020-12-23 16:36:22 -0800 | [diff] [blame] | 118 | |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 119 | sessionInfoMap[session] = std::move(info); |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 120 | return true; |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | std::vector<uint8_t> VersionBlobHandler::read(uint16_t session, uint32_t offset, |
| 124 | uint32_t requestedSize) |
| 125 | { |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 126 | auto& data = sessionInfoMap.at(session)->data; |
William A. Kennington III | 0674a6d | 2021-01-12 14:25:41 -0800 | [diff] [blame] | 127 | if (data == nullptr || !*data) |
| 128 | { |
| 129 | throw std::runtime_error("Version data not ready for read"); |
| 130 | } |
| 131 | if ((*data)->size() < offset) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 132 | { |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 133 | return {}; |
| 134 | } |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 135 | std::vector<uint8_t> ret( |
| 136 | std::min<size_t>(requestedSize, (*data)->size() - offset)); |
| 137 | std::memcpy(&ret[0], &(**data)[offset], ret.size()); |
| 138 | return ret; |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool VersionBlobHandler::close(uint16_t session) |
| 142 | { |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 143 | auto it = sessionInfoMap.find(session); |
| 144 | if (it == sessionInfoMap.end()) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 145 | { |
| 146 | return false; |
| 147 | } |
William A. Kennington III | 9936c45 | 2020-12-23 23:31:23 -0800 | [diff] [blame] | 148 | auto& info = *it->second; |
| 149 | info.blob->sessionsToUpdate.erase(&info); |
| 150 | if (info.blob->sessionsToUpdate.empty()) |
| 151 | { |
| 152 | info.blob->actions->onOpen->abort(); |
| 153 | } |
| 154 | sessionInfoMap.erase(it); |
| 155 | return true; |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 156 | } |
William A. Kennington III | c5b901d | 2020-12-23 17:07:20 -0800 | [diff] [blame] | 157 | |
| 158 | bool VersionBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta) |
| 159 | { |
William A. Kennington III | eba0c34 | 2021-01-12 14:23:01 -0800 | [diff] [blame] | 160 | const auto& data = sessionInfoMap.at(session)->data; |
| 161 | if (data == nullptr) |
| 162 | { |
| 163 | meta->blobState = blobs::StateFlags::committing; |
| 164 | meta->size = 0; |
| 165 | } |
| 166 | else if (!*data) |
| 167 | { |
| 168 | meta->blobState = blobs::StateFlags::commit_error; |
| 169 | meta->size = 0; |
| 170 | } |
| 171 | else |
| 172 | { |
Patrick Williams | 1038836 | 2023-05-10 07:51:09 -0500 | [diff] [blame] | 173 | meta->blobState = blobs::StateFlags::committed | |
| 174 | blobs::StateFlags::open_read; |
William A. Kennington III | eba0c34 | 2021-01-12 14:23:01 -0800 | [diff] [blame] | 175 | meta->size = (*data)->size(); |
| 176 | } |
| 177 | return true; |
William A. Kennington III | c5b901d | 2020-12-23 17:07:20 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | bool VersionBlobHandler::expire(uint16_t session) |
| 181 | { |
| 182 | close(session); |
| 183 | return true; |
| 184 | } |
| 185 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 186 | } // namespace ipmi_flash |