-
Notifications
You must be signed in to change notification settings - Fork 24
/
serialize.cpp
164 lines (143 loc) · 4.18 KB
/
serialize.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "config.h"
#include "serialize.hpp"
#include <cereal/archives/json.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/server.hpp>
#include <filesystem>
#include <fstream>
#include <system_error>
namespace phosphor
{
namespace software
{
namespace updater
{
PHOSPHOR_LOG2_USING;
namespace fs = std::filesystem;
const std::string priorityName = "priority";
const std::string purposeName = "purpose";
void storePriority(const std::string& flashId, uint8_t priority)
{
std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId;
if (!fs::is_directory(path, ec))
{
if (fs::exists(path, ec))
{
// Delete if it's a non-directory file
warning("Removing non-directory file: {PATH}", "PATH", path);
fs::remove_all(path, ec);
}
fs::create_directories(path, ec);
}
path = path / priorityName;
std::ofstream os(path.c_str());
cereal::JSONOutputArchive oarchive(os);
oarchive(cereal::make_nvp(priorityName, priority));
}
void storePurpose(const std::string& flashId, VersionPurpose purpose)
{
std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId;
if (!fs::is_directory(path, ec))
{
if (fs::exists(path, ec))
{
// Delete if it's a non-directory file
warning("Removing non-directory file: {PATH}", "PATH", path);
fs::remove_all(path, ec);
}
fs::create_directories(path, ec);
}
path = path / purposeName;
std::ofstream os(path.c_str());
cereal::JSONOutputArchive oarchive(os);
oarchive(cereal::make_nvp(purposeName, purpose));
}
bool restorePriority(const std::string& flashId, uint8_t& priority)
{
std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId / priorityName;
if (fs::exists(path, ec))
{
std::ifstream is(path.c_str(), std::ios::in);
try
{
cereal::JSONInputArchive iarchive(is);
iarchive(cereal::make_nvp(priorityName, priority));
return true;
}
catch (const cereal::Exception& e)
{
fs::remove_all(path, ec);
}
}
// Find the mtd device "u-boot-env" to retrieve the environment variables
std::ifstream mtdDevices("/proc/mtd");
std::string device;
std::string devicePath;
try
{
while (std::getline(mtdDevices, device))
{
if (device.find("u-boot-env") != std::string::npos)
{
devicePath = "/dev/" + device.substr(0, device.find(':'));
break;
}
}
if (!devicePath.empty())
{
std::ifstream input(devicePath.c_str());
std::string envVars;
std::getline(input, envVars);
std::string versionVar = flashId + "=";
auto varPosition = envVars.find(versionVar);
if (varPosition != std::string::npos)
{
// Grab the environment variable for this flashId. These
// variables follow the format "flashId=priority\0"
auto var = envVars.substr(varPosition);
priority = std::stoi(var.substr(versionVar.length()));
return true;
}
}
}
catch (const std::exception& e)
{
error("Error during processing: {ERROR}", "ERROR", e);
}
return false;
}
bool restorePurpose(const std::string& flashId, VersionPurpose& purpose)
{
std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId / purposeName;
if (fs::exists(path, ec))
{
std::ifstream is(path.c_str(), std::ios::in);
try
{
cereal::JSONInputArchive iarchive(is);
iarchive(cereal::make_nvp(purposeName, purpose));
return true;
}
catch (const cereal::Exception& e)
{
fs::remove_all(path, ec);
}
}
return false;
}
void removePersistDataDirectory(const std::string& flashId)
{
std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId;
if (fs::exists(path, ec))
{
fs::remove_all(path, ec);
}
}
} // namespace updater
} // namespace software
} // namespace phosphor