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