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); |
| 18 | if (!blobInfoMap.try_emplace(info->blobId, std::move(info)).second) |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 19 | { |
| 20 | fprintf(stderr, "Ignoring duplicate config for %s\n", |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 21 | info->blobId.c_str()); |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 22 | } |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 23 | } |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | bool VersionBlobHandler::canHandleBlob(const std::string& path) |
| 27 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 28 | return blobInfoMap.find(path) != blobInfoMap.end(); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | std::vector<std::string> VersionBlobHandler::getBlobIds() |
| 32 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 33 | std::vector<std::string> ret; |
| 34 | for (const auto& [key, _] : blobInfoMap) |
| 35 | { |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 36 | ret.emplace_back(key); |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 37 | } |
| 38 | return ret; |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * deleteBlob - does nothing, always fails |
| 43 | */ |
| 44 | bool VersionBlobHandler::deleteBlob(const std::string& path) |
| 45 | { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | bool VersionBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta) |
| 50 | { |
| 51 | // TODO: stat should return the blob state and in the meta data information |
| 52 | // on whether a read is successful should be contained |
| 53 | // do things like determine if systemd target is triggered |
| 54 | // then check if file can be opened for read |
| 55 | return false; /* not yet implemented */ |
| 56 | } |
| 57 | |
| 58 | bool VersionBlobHandler::open(uint16_t session, uint16_t flags, |
| 59 | const std::string& path) |
| 60 | { |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 61 | /* only reads are supported, check if blob is handled and make sure |
| 62 | * the blob isn't already opened |
| 63 | */ |
| 64 | if (flags != blobs::read) |
| 65 | { |
| 66 | fprintf(stderr, "open %s fail: unsupported flags(0x%04X.)\n", |
| 67 | path.c_str(), flags); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 68 | return false; |
| 69 | } |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 70 | |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 71 | auto& v = *blobInfoMap.at(path); |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 72 | if (v.blobState == blobs::StateFlags::open_read) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 73 | { |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 74 | fprintf(stderr, "open %s fail: blob already opened for read\n", |
| 75 | path.c_str()); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 76 | return false; |
| 77 | } |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 78 | if (v.actions->onOpen->trigger() == false) |
| 79 | { |
| 80 | fprintf(stderr, "open %s fail: onOpen trigger failed\n", path.c_str()); |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 81 | return false; |
| 82 | } |
William A. Kennington III | 007c016 | 2020-12-23 16:36:22 -0800 | [diff] [blame] | 83 | |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 84 | v.blobState = blobs::StateFlags::open_read; |
William A. Kennington III | 007c016 | 2020-12-23 16:36:22 -0800 | [diff] [blame] | 85 | sessionToBlob[session] = &v; |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 86 | return true; |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | std::vector<uint8_t> VersionBlobHandler::read(uint16_t session, uint32_t offset, |
| 90 | uint32_t requestedSize) |
| 91 | { |
William A. Kennington III | 12e6e75 | 2020-12-23 20:47:29 -0800 | [diff] [blame] | 92 | auto pack = sessionToBlob.at(session); |
| 93 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 94 | /* onOpen trigger must be successful, otherwise potential |
| 95 | * for stale data to be read |
| 96 | */ |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 97 | if (pack->actions->onOpen->status() != ActionStatus::success) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 98 | { |
| 99 | fprintf(stderr, "read failed: onOpen trigger not successful\n"); |
| 100 | return {}; |
| 101 | } |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 102 | if (!pack->handler->open("don't care", std::ios::in)) |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 103 | { |
| 104 | fprintf(stderr, "read failed: file open unsuccessful blob=%s\n", |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 105 | pack->blobId.c_str()); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 106 | return {}; |
| 107 | } |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 108 | auto d = pack->handler->read(offset, requestedSize); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 109 | if (!d) |
| 110 | { |
| 111 | fprintf(stderr, "read failed: unable to read file for blob %s\n", |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 112 | pack->blobId.c_str()); |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 113 | pack->handler->close(); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 114 | return {}; |
| 115 | } |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 116 | pack->handler->close(); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 117 | return *d; |
| 118 | } |
| 119 | |
| 120 | bool VersionBlobHandler::close(uint16_t session) |
| 121 | { |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 122 | try |
| 123 | { |
William A. Kennington III | b45eb5e | 2020-12-23 11:47:05 -0800 | [diff] [blame] | 124 | auto& pack = *sessionToBlob.at(session); |
William A. Kennington III | 007c016 | 2020-12-23 16:36:22 -0800 | [diff] [blame] | 125 | pack.actions->onOpen->abort(); |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 126 | pack.blobState = static_cast<blobs::StateFlags>(0); |
| 127 | sessionToBlob.erase(session); |
| 128 | return true; |
| 129 | } |
| 130 | catch (const std::out_of_range& e) |
| 131 | { |
| 132 | return false; |
| 133 | } |
| 134 | } |
William A. Kennington III | c5b901d | 2020-12-23 17:07:20 -0800 | [diff] [blame] | 135 | |
| 136 | bool VersionBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta) |
| 137 | { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | bool VersionBlobHandler::expire(uint16_t session) |
| 142 | { |
| 143 | close(session); |
| 144 | return true; |
| 145 | } |
| 146 | |
Jason Ling | c78bfc8 | 2020-11-05 18:58:16 -0800 | [diff] [blame] | 147 | } // namespace ipmi_flash |