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