16620e98dSDeepak Kodihalli #pragma once 26620e98dSDeepak Kodihalli 3a680d1efSPatrick Venture #include "config.h" 4a680d1efSPatrick Venture 56620e98dSDeepak Kodihalli #include <cereal/archives/json.hpp> 6e6b21c74SKun Yi #include <filesystem> 76620e98dSDeepak Kodihalli #include <fstream> 886ad3c67SJayanth Othayoth #include <phosphor-logging/log.hpp> 986ad3c67SJayanth Othayoth 107dfd08f8SBrad Bishop namespace phosphor 117dfd08f8SBrad Bishop { 127dfd08f8SBrad Bishop namespace inventory 137dfd08f8SBrad Bishop { 147dfd08f8SBrad Bishop namespace manager 156620e98dSDeepak Kodihalli { 166620e98dSDeepak Kodihalli 17e6b21c74SKun Yi namespace fs = std::filesystem; 186620e98dSDeepak Kodihalli 19a2485504SBrad Bishop namespace detail 20a2485504SBrad Bishop { 21a2485504SBrad Bishop inline fs::path getStoragePath(const std::string& path, 22a2485504SBrad Bishop const std::string& iface) 23a2485504SBrad Bishop { 24a2485504SBrad Bishop auto p = fs::path(PIM_PERSIST_PATH); 25a2485504SBrad Bishop p /= fs::path(path).relative_path(); 26a2485504SBrad Bishop p /= fs::path(iface).relative_path(); 27a2485504SBrad Bishop return p; 28a2485504SBrad Bishop } 29a2485504SBrad Bishop } // namespace detail 30a2485504SBrad Bishop 317dfd08f8SBrad Bishop struct SerialOps 327dfd08f8SBrad Bishop { 337dfd08f8SBrad Bishop /** @brief Serialize inventory item path 347dfd08f8SBrad Bishop * Serializing only path for an empty interface to be consistent 357dfd08f8SBrad Bishop * interfaces. 367dfd08f8SBrad Bishop * @param[in] path - DBus object path 377dfd08f8SBrad Bishop * @param[in] iface - Inventory interface name 387dfd08f8SBrad Bishop */ 397dfd08f8SBrad Bishop static void serialize(const std::string& path, const std::string& iface) 407dfd08f8SBrad Bishop { 41a2485504SBrad Bishop auto p = detail::getStoragePath(path, iface); 42a2485504SBrad Bishop fs::create_directories(p.parent_path()); 437dfd08f8SBrad Bishop std::ofstream os(p, std::ios::binary); 447dfd08f8SBrad Bishop } 456620e98dSDeepak Kodihalli 466620e98dSDeepak Kodihalli /** @brief Serialize inventory item 476620e98dSDeepak Kodihalli * 486620e98dSDeepak Kodihalli * @param[in] path - DBus object path 496620e98dSDeepak Kodihalli * @param[in] iface - Inventory interface name 506620e98dSDeepak Kodihalli * @param[in] object - Object to be serialized 516620e98dSDeepak Kodihalli */ 526620e98dSDeepak Kodihalli template <typename T> 537dfd08f8SBrad Bishop static void serialize(const std::string& path, const std::string& iface, 547dfd08f8SBrad Bishop const T& object) 556620e98dSDeepak Kodihalli { 56a2485504SBrad Bishop auto p = detail::getStoragePath(path, iface); 57a2485504SBrad Bishop fs::create_directories(p.parent_path()); 586620e98dSDeepak Kodihalli std::ofstream os(p, std::ios::binary); 596620e98dSDeepak Kodihalli cereal::JSONOutputArchive oarchive(os); 606620e98dSDeepak Kodihalli oarchive(object); 616620e98dSDeepak Kodihalli } 626620e98dSDeepak Kodihalli 637dfd08f8SBrad Bishop static void deserialize(const std::string&, const std::string&) 64fa23d704SMarri Devender Rao { 657dfd08f8SBrad Bishop // This is intentionally a noop. 66fa23d704SMarri Devender Rao } 67fa23d704SMarri Devender Rao 68b28990f3SDeepak Kodihalli /** @brief Deserialize inventory item 69b28990f3SDeepak Kodihalli * 70b28990f3SDeepak Kodihalli * @param[in] path - DBus object path 71b28990f3SDeepak Kodihalli * @param[in] iface - Inventory interface name 72b28990f3SDeepak Kodihalli * @param[in] object - Object to be serialized 73b28990f3SDeepak Kodihalli */ 74b28990f3SDeepak Kodihalli template <typename T> 757dfd08f8SBrad Bishop static void deserialize(const std::string& path, const std::string& iface, 767dfd08f8SBrad Bishop T& object) 77b28990f3SDeepak Kodihalli { 78*ded627c4SMatt Spinler auto p = detail::getStoragePath(path, iface); 7986ad3c67SJayanth Othayoth try 8086ad3c67SJayanth Othayoth { 81b28990f3SDeepak Kodihalli if (fs::exists(p)) 82b28990f3SDeepak Kodihalli { 83b28990f3SDeepak Kodihalli std::ifstream is(p, std::ios::in | std::ios::binary); 84b28990f3SDeepak Kodihalli cereal::JSONInputArchive iarchive(is); 85b28990f3SDeepak Kodihalli iarchive(object); 86b28990f3SDeepak Kodihalli } 87b28990f3SDeepak Kodihalli } 8886ad3c67SJayanth Othayoth catch (cereal::Exception& e) 8986ad3c67SJayanth Othayoth { 907dfd08f8SBrad Bishop phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 9186ad3c67SJayanth Othayoth fs::remove(p); 9286ad3c67SJayanth Othayoth } 9386ad3c67SJayanth Othayoth } 947dfd08f8SBrad Bishop }; 957dfd08f8SBrad Bishop } // namespace manager 967dfd08f8SBrad Bishop } // namespace inventory 977dfd08f8SBrad Bishop } // namespace phosphor 98