blob: ac1b8ac0903223b7750c2846f130ca10c75383f4 [file] [log] [blame]
Michael Tritz60bc20f2017-07-29 23:32:21 -05001#include "config.h"
2#include <experimental/filesystem>
Michael Tritz5c907112017-08-02 15:25:51 -05003#include <cereal/archives/json.hpp>
Michael Tritz60bc20f2017-07-29 23:32:21 -05004#include <fstream>
5#include "serialize.hpp"
6
7namespace openpower
8{
9namespace software
10{
11namespace updater
12{
13
14namespace fs = std::experimental::filesystem;
15
16void storeToFile(std::string versionId, uint8_t priority)
17{
Gunnar Millse7ff6452017-09-21 16:19:36 -050018 if (!fs::is_directory(PERSIST_DIR))
Michael Tritz60bc20f2017-07-29 23:32:21 -050019 {
Michael Tritz48d9a4e2017-09-18 14:30:46 -050020 fs::create_directories(PERSIST_DIR);
Michael Tritz60bc20f2017-07-29 23:32:21 -050021 }
Michael Tritz60bc20f2017-07-29 23:32:21 -050022
Michael Tritz4fb952c2017-08-13 14:55:04 -050023 // store one copy in /var/lib/obmc/openpower-pnor-code-mgmt/[versionId]
24 auto varPath = PERSIST_DIR + versionId;
25 std::ofstream varOutput(varPath.c_str());
26 cereal::JSONOutputArchive varArchive(varOutput);
27 varArchive(cereal::make_nvp("priority", priority));
28
Gunnar Millse7ff6452017-09-21 16:19:36 -050029 if (fs::is_directory(PNOR_RW_PREFIX + versionId))
Michael Tritz4fb952c2017-08-13 14:55:04 -050030 {
31 // store another copy in /media/pnor-rw-[versionId]/[versionId]
32 auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId;
33 std::ofstream rwOutput(rwPath.c_str());
34 cereal::JSONOutputArchive rwArchive(rwOutput);
35 rwArchive(cereal::make_nvp("priority", priority));
36 }
Michael Tritz60bc20f2017-07-29 23:32:21 -050037}
38
Michael Tritz36417922017-08-04 14:00:29 -050039bool restoreFromFile(std::string versionId, uint8_t& priority)
Michael Tritz60bc20f2017-07-29 23:32:21 -050040{
Michael Tritz4fb952c2017-08-13 14:55:04 -050041 auto varPath = PERSIST_DIR + versionId;
42 if (fs::exists(varPath))
Michael Tritz60bc20f2017-07-29 23:32:21 -050043 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050044 std::ifstream varInput(varPath.c_str(), std::ios::in);
Michael Tritz36417922017-08-04 14:00:29 -050045 try
46 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050047 cereal::JSONInputArchive varArchive(varInput);
48 varArchive(cereal::make_nvp("priority", priority));
Michael Tritz36417922017-08-04 14:00:29 -050049 return true;
50 }
Gunnar Millse7ff6452017-09-21 16:19:36 -050051 catch (cereal::RapidJSONException& e)
Michael Tritz36417922017-08-04 14:00:29 -050052 {
Michael Tritz4fb952c2017-08-13 14:55:04 -050053 fs::remove(varPath);
54 }
55 }
56
57 auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId;
58 if (fs::exists(rwPath))
59 {
60 std::ifstream rwInput(rwPath.c_str(), std::ios::in);
61 try
62 {
63 cereal::JSONInputArchive rwArchive(rwInput);
64 rwArchive(cereal::make_nvp("priority", priority));
65 return true;
66 }
Gunnar Millse7ff6452017-09-21 16:19:36 -050067 catch (cereal::RapidJSONException& e)
Michael Tritz4fb952c2017-08-13 14:55:04 -050068 {
69 fs::remove(rwPath);
Michael Tritz36417922017-08-04 14:00:29 -050070 }
Michael Tritz60bc20f2017-07-29 23:32:21 -050071 }
Michael Tritz36417922017-08-04 14:00:29 -050072 return false;
Michael Tritz60bc20f2017-07-29 23:32:21 -050073}
74
75void removeFile(std::string versionId)
76{
77 std::string path = PERSIST_DIR + versionId;
78 if (fs::exists(path))
79 {
80 fs::remove(path);
81 }
82}
83
84} // namespace updater
85} // namespace software
86} // namespace openpower