blob: ace13c4cffd6bb939d8f14e4deaa686d69cede49 [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>
George Liucf5d0242022-04-14 14:39:47 +08006#include <phosphor-logging/lg2.hpp>
Brad Bishopa83db302020-12-06 14:51:23 -05007
Kun Yie6b21c72019-03-27 09:53:51 -07008#include <filesystem>
Deepak Kodihalli6620e982017-08-05 13:09:54 -05009#include <fstream>
Jayanth Othayoth86ad3c62017-09-19 22:18:45 -050010
Brad Bishop7dfd08f2018-12-12 21:40:26 -050011namespace phosphor
12{
13namespace inventory
14{
15namespace manager
Deepak Kodihalli6620e982017-08-05 13:09:54 -050016{
17
Kun Yie6b21c72019-03-27 09:53:51 -070018namespace fs = std::filesystem;
Deepak Kodihalli6620e982017-08-05 13:09:54 -050019
Brad Bishopa2485502019-04-15 15:59:28 -040020namespace detail
21{
22inline 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 Bishop7dfd08f2018-12-12 21:40:26 -050032struct SerialOps
Deepak Kodihalli6620e982017-08-05 13:09:54 -050033{
Brad Bishop7dfd08f2018-12-12 21:40:26 -050034 /** @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 Kodihallib28990f2017-08-08 07:19:34 -050041 {
Brad Bishopa2485502019-04-15 15:59:28 -040042 auto p = detail::getStoragePath(path, iface);
43 fs::create_directories(p.parent_path());
Brad Bishop7dfd08f2018-12-12 21:40:26 -050044 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 Bishopa2485502019-04-15 15:59:28 -040057 auto p = detail::getStoragePath(path, iface);
58 fs::create_directories(p.parent_path());
Brad Bishop7dfd08f2018-12-12 21:40:26 -050059 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 Spinlerded627c2019-04-19 13:44:47 -050079 auto p = detail::getStoragePath(path, iface);
Brad Bishop7dfd08f2018-12-12 21:40:26 -050080 try
Jayanth Othayoth86ad3c62017-09-19 22:18:45 -050081 {
Brad Bishop7dfd08f2018-12-12 21:40:26 -050082 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 Williams3e2d9642021-10-06 12:35:04 -050089 catch (const cereal::Exception& e)
Brad Bishop7dfd08f2018-12-12 21:40:26 -050090 {
George Liucf5d0242022-04-14 14:39:47 +080091 lg2::error("Deserialization failed: {ERROR}", "ERROR", e);
Brad Bishop7dfd08f2018-12-12 21:40:26 -050092 fs::remove(p);
Jayanth Othayoth86ad3c62017-09-19 22:18:45 -050093 }
94 }
Brad Bishop7dfd08f2018-12-12 21:40:26 -050095};
96} // namespace manager
97} // namespace inventory
98} // namespace phosphor