Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include <experimental/filesystem> |
Michael Tritz | 5c90711 | 2017-08-02 15:25:51 -0500 | [diff] [blame] | 3 | #include <cereal/archives/json.hpp> |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 4 | #include <fstream> |
| 5 | #include "serialize.hpp" |
| 6 | |
| 7 | namespace openpower |
| 8 | { |
| 9 | namespace software |
| 10 | { |
| 11 | namespace updater |
| 12 | { |
| 13 | |
| 14 | namespace fs = std::experimental::filesystem; |
| 15 | |
| 16 | void storeToFile(std::string versionId, uint8_t priority) |
| 17 | { |
| 18 | if(!fs::is_directory(PERSIST_DIR)) |
| 19 | { |
| 20 | fs::create_directory(PERSIST_DIR); |
| 21 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 22 | |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 23 | // 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 Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 39 | bool restoreFromFile(std::string versionId, uint8_t& priority) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 40 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 41 | auto varPath = PERSIST_DIR + versionId; |
| 42 | if (fs::exists(varPath)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 43 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 44 | std::ifstream varInput(varPath.c_str(), std::ios::in); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 45 | try |
| 46 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 47 | cereal::JSONInputArchive varArchive(varInput); |
| 48 | varArchive(cereal::make_nvp("priority", priority)); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | catch(cereal::RapidJSONException& e) |
| 52 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 53 | 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 Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 70 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 71 | } |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 72 | return false; |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void 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 |