Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame^] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #include "post_code.hpp" |
| 17 | std::vector<uint64_t> PostCode::getPostCodes(uint16_t index) |
| 18 | { |
| 19 | std::vector<uint64_t> codes; |
| 20 | |
| 21 | if (currentBootCycleIndex() == index) |
| 22 | return postCodes; |
| 23 | deserializePostCodes(fs::path(strPostCodeListPath + std::to_string(index)), |
| 24 | codes); |
| 25 | return codes; |
| 26 | } |
| 27 | void PostCode::savePostCodes(uint64_t code) |
| 28 | { |
| 29 | postCodes.push_back(code); |
| 30 | serialize(fs::path(PostCodeListPath)); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | fs::path PostCode::serialize(const std::string& path) |
| 35 | { |
| 36 | try |
| 37 | { |
| 38 | uint16_t index = currentBootCycleIndex(); |
| 39 | fs::path fullPath(path + strCurrentBootCycleIndexName); |
| 40 | std::ofstream os(fullPath.c_str(), std::ios::binary); |
| 41 | cereal::JSONOutputArchive oarchive(os); |
| 42 | oarchive(index); |
| 43 | |
| 44 | std::ofstream osPostCodes( |
| 45 | (path + std::to_string(currentBootCycleIndex())).c_str(), |
| 46 | std::ios::binary); |
| 47 | cereal::JSONOutputArchive oarchivePostCodes(osPostCodes); |
| 48 | oarchivePostCodes(postCodes); |
| 49 | } |
| 50 | catch (cereal::Exception& e) |
| 51 | { |
| 52 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 53 | return ""; |
| 54 | } |
| 55 | catch (const fs::filesystem_error& e) |
| 56 | { |
| 57 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 58 | return ""; |
| 59 | } |
| 60 | return path; |
| 61 | } |
| 62 | |
| 63 | bool PostCode::deserialize(const fs::path& path, uint16_t& index) |
| 64 | { |
| 65 | try |
| 66 | { |
| 67 | if (fs::exists(path)) |
| 68 | { |
| 69 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 70 | cereal::JSONInputArchive iarchive(is); |
| 71 | iarchive(index); |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | catch (cereal::Exception& e) |
| 77 | { |
| 78 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 79 | return false; |
| 80 | } |
| 81 | catch (const fs::filesystem_error& e) |
| 82 | { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | bool PostCode::deserializePostCodes(const fs::path& path, |
| 90 | std::vector<uint64_t>& codes) |
| 91 | { |
| 92 | try |
| 93 | { |
| 94 | if (fs::exists(path)) |
| 95 | { |
| 96 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 97 | cereal::JSONInputArchive iarchive(is); |
| 98 | iarchive(codes); |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | catch (cereal::Exception& e) |
| 104 | { |
| 105 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 106 | return false; |
| 107 | } |
| 108 | catch (const fs::filesystem_error& e) |
| 109 | { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |