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