blob: 82bd4bb92b008dc6bc91371a29977dd96c0ca25c [file] [log] [blame]
Deepak Kodihalli6620e982017-08-05 13:09:54 -05001#pragma once
2
Patrick Venturea680d1e2018-10-14 13:34:26 -07003#include "config.h"
4
Deepak Kodihalli6620e982017-08-05 13:09:54 -05005#include <cereal/archives/json.hpp>
Kun Yie6b21c72019-03-27 09:53:51 -07006#include <filesystem>
Deepak Kodihalli6620e982017-08-05 13:09:54 -05007#include <fstream>
Jayanth Othayoth86ad3c62017-09-19 22:18:45 -05008#include <phosphor-logging/log.hpp>
9
Brad Bishop7dfd08f2018-12-12 21:40:26 -050010namespace phosphor
11{
12namespace inventory
13{
14namespace manager
Deepak Kodihalli6620e982017-08-05 13:09:54 -050015{
16
Kun Yie6b21c72019-03-27 09:53:51 -070017namespace fs = std::filesystem;
Deepak Kodihalli6620e982017-08-05 13:09:54 -050018
Brad Bishopa2485502019-04-15 15:59:28 -040019namespace detail
20{
21inline 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 Bishop7dfd08f2018-12-12 21:40:26 -050031struct SerialOps
Deepak Kodihalli6620e982017-08-05 13:09:54 -050032{
Brad Bishop7dfd08f2018-12-12 21:40:26 -050033 /** @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 Kodihallib28990f2017-08-08 07:19:34 -050040 {
Brad Bishopa2485502019-04-15 15:59:28 -040041 auto p = detail::getStoragePath(path, iface);
42 fs::create_directories(p.parent_path());
Brad Bishop7dfd08f2018-12-12 21:40:26 -050043 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 Bishopa2485502019-04-15 15:59:28 -040056 auto p = detail::getStoragePath(path, iface);
57 fs::create_directories(p.parent_path());
Brad Bishop7dfd08f2018-12-12 21:40:26 -050058 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 {
78 fs::path p(PIM_PERSIST_PATH);
79 p /= path;
80 p /= iface;
81 try
Jayanth Othayoth86ad3c62017-09-19 22:18:45 -050082 {
Brad Bishop7dfd08f2018-12-12 21:40:26 -050083 if (fs::exists(p))
84 {
85 std::ifstream is(p, std::ios::in | std::ios::binary);
86 cereal::JSONInputArchive iarchive(is);
87 iarchive(object);
88 }
89 }
90 catch (cereal::Exception& e)
91 {
92 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
93 fs::remove(p);
Jayanth Othayoth86ad3c62017-09-19 22:18:45 -050094 }
95 }
Brad Bishop7dfd08f2018-12-12 21:40:26 -050096};
97} // namespace manager
98} // namespace inventory
99} // namespace phosphor