Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 5 | #include <cereal/archives/json.hpp> |
George Liu | cf5d024 | 2022-04-14 14:39:47 +0800 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 7 | |
Kun Yi | e6b21c7 | 2019-03-27 09:53:51 -0700 | [diff] [blame] | 8 | #include <filesystem> |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 9 | #include <fstream> |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 10 | |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace inventory |
| 14 | { |
| 15 | namespace manager |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 16 | { |
| 17 | |
Kun Yi | e6b21c7 | 2019-03-27 09:53:51 -0700 | [diff] [blame] | 18 | namespace fs = std::filesystem; |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 19 | |
Brad Bishop | a248550 | 2019-04-15 15:59:28 -0400 | [diff] [blame] | 20 | namespace detail |
| 21 | { |
| 22 | inline fs::path getStoragePath(const std::string& path, |
| 23 | const std::string& iface) |
| 24 | { |
| 25 | auto p = fs::path(PIM_PERSIST_PATH); |
| 26 | p /= fs::path(path).relative_path(); |
| 27 | p /= fs::path(iface).relative_path(); |
| 28 | return p; |
| 29 | } |
| 30 | } // namespace detail |
| 31 | |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 32 | struct SerialOps |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 33 | { |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 34 | /** @brief Serialize inventory item path |
| 35 | * Serializing only path for an empty interface to be consistent |
| 36 | * interfaces. |
| 37 | * @param[in] path - DBus object path |
| 38 | * @param[in] iface - Inventory interface name |
| 39 | */ |
| 40 | static void serialize(const std::string& path, const std::string& iface) |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 41 | { |
Brad Bishop | a248550 | 2019-04-15 15:59:28 -0400 | [diff] [blame] | 42 | auto p = detail::getStoragePath(path, iface); |
| 43 | fs::create_directories(p.parent_path()); |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 44 | std::ofstream os(p, std::ios::binary); |
| 45 | } |
| 46 | |
| 47 | /** @brief Serialize inventory item |
| 48 | * |
| 49 | * @param[in] path - DBus object path |
| 50 | * @param[in] iface - Inventory interface name |
| 51 | * @param[in] object - Object to be serialized |
| 52 | */ |
| 53 | template <typename T> |
| 54 | static void serialize(const std::string& path, const std::string& iface, |
| 55 | const T& object) |
| 56 | { |
Brad Bishop | a248550 | 2019-04-15 15:59:28 -0400 | [diff] [blame] | 57 | auto p = detail::getStoragePath(path, iface); |
| 58 | fs::create_directories(p.parent_path()); |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 59 | std::ofstream os(p, std::ios::binary); |
| 60 | cereal::JSONOutputArchive oarchive(os); |
| 61 | oarchive(object); |
| 62 | } |
| 63 | |
| 64 | static void deserialize(const std::string&, const std::string&) |
| 65 | { |
| 66 | // This is intentionally a noop. |
| 67 | } |
| 68 | |
| 69 | /** @brief Deserialize inventory item |
| 70 | * |
| 71 | * @param[in] path - DBus object path |
| 72 | * @param[in] iface - Inventory interface name |
| 73 | * @param[in] object - Object to be serialized |
| 74 | */ |
| 75 | template <typename T> |
| 76 | static void deserialize(const std::string& path, const std::string& iface, |
| 77 | T& object) |
| 78 | { |
Matt Spinler | ded627c | 2019-04-19 13:44:47 -0500 | [diff] [blame] | 79 | auto p = detail::getStoragePath(path, iface); |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 80 | try |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 81 | { |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 82 | if (fs::exists(p)) |
| 83 | { |
| 84 | std::ifstream is(p, std::ios::in | std::ios::binary); |
| 85 | cereal::JSONInputArchive iarchive(is); |
| 86 | iarchive(object); |
| 87 | } |
| 88 | } |
Patrick Williams | 3e2d964 | 2021-10-06 12:35:04 -0500 | [diff] [blame] | 89 | catch (const cereal::Exception& e) |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 90 | { |
George Liu | cf5d024 | 2022-04-14 14:39:47 +0800 | [diff] [blame] | 91 | lg2::error("Deserialization failed: {ERROR}", "ERROR", e); |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 92 | fs::remove(p); |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
Brad Bishop | 7dfd08f | 2018-12-12 21:40:26 -0500 | [diff] [blame] | 95 | }; |
| 96 | } // namespace manager |
| 97 | } // namespace inventory |
| 98 | } // namespace phosphor |