summaryrefslogtreecommitdiffstats
path: root/serialize.hpp
blob: 82bd4bb92b008dc6bc91371a29977dd96c0ca25c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once

#include "config.h"

#include <cereal/archives/json.hpp>
#include <filesystem>
#include <fstream>
#include <phosphor-logging/log.hpp>

namespace phosphor
{
namespace inventory
{
namespace manager
{

namespace fs = std::filesystem;

namespace detail
{
inline fs::path getStoragePath(const std::string& path,
                               const std::string& iface)
{
    auto p = fs::path(PIM_PERSIST_PATH);
    p /= fs::path(path).relative_path();
    p /= fs::path(iface).relative_path();
    return p;
}
} // namespace detail

struct SerialOps
{
    /** @brief Serialize inventory item path
     *  Serializing only path for an empty interface to be consistent
     *  interfaces.
     *  @param[in] path - DBus object path
     *  @param[in] iface - Inventory interface name
     */
    static void serialize(const std::string& path, const std::string& iface)
    {
        auto p = detail::getStoragePath(path, iface);
        fs::create_directories(p.parent_path());
        std::ofstream os(p, std::ios::binary);
    }

    /** @brief Serialize inventory item
     *
     *  @param[in] path - DBus object path
     *  @param[in] iface - Inventory interface name
     *  @param[in] object - Object to be serialized
     */
    template <typename T>
    static void serialize(const std::string& path, const std::string& iface,
                          const T& object)
    {
        auto p = detail::getStoragePath(path, iface);
        fs::create_directories(p.parent_path());
        std::ofstream os(p, std::ios::binary);
        cereal::JSONOutputArchive oarchive(os);
        oarchive(object);
    }

    static void deserialize(const std::string&, const std::string&)
    {
        // This is intentionally a noop.
    }

    /** @brief Deserialize inventory item
     *
     *  @param[in] path - DBus object path
     *  @param[in] iface - Inventory interface name
     *  @param[in] object - Object to be serialized
     */
    template <typename T>
    static void deserialize(const std::string& path, const std::string& iface,
                            T& object)
    {
        fs::path p(PIM_PERSIST_PATH);
        p /= path;
        p /= iface;
        try
        {
            if (fs::exists(p))
            {
                std::ifstream is(p, std::ios::in | std::ios::binary);
                cereal::JSONInputArchive iarchive(is);
                iarchive(object);
            }
        }
        catch (cereal::Exception& e)
        {
            phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
            fs::remove(p);
        }
    }
};
} // namespace manager
} // namespace inventory
} // namespace phosphor
OpenPOWER on IntegriCloud