blob: 8c63264f54e625e057f886ec3cf45f85ccfc2d92 [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{
18 if(!fs::is_directory(PERSIST_DIR))
19 {
20 fs::create_directory(PERSIST_DIR);
21 }
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
29 if(fs::is_directory(PNOR_RW_PREFIX + versionId))
30 {
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 }
51 catch(cereal::RapidJSONException& e)
52 {
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 }
67 catch(cereal::RapidJSONException& e)
68 {
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