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