Browse Source

Write to temp settings.json file and overwrite, in case Rack crashes while writing it.

tags/v2.3.0
Andrew Belt 1 year ago
parent
commit
57c99b4cd9
1 changed files with 8 additions and 4 deletions
  1. +8
    -4
      src/settings.cpp

+ 8
- 4
src/settings.cpp View File

@@ -8,6 +8,7 @@
#include <context.hpp> #include <context.hpp>
#include <patch.hpp> #include <patch.hpp>
#include <asset.hpp> #include <asset.hpp>
#include <system.hpp>




namespace rack { namespace rack {
@@ -491,14 +492,17 @@ void save(std::string path) {
json_t* rootJ = toJson(); json_t* rootJ = toJson();
if (!rootJ) if (!rootJ)
return; return;
DEFER({json_decref(rootJ);});


FILE* file = std::fopen(path.c_str(), "w");
std::string tmpPath = path + ".tmp";
FILE* file = std::fopen(tmpPath.c_str(), "w");
if (!file) if (!file)
return; return;
DEFER({std::fclose(file);});


json_dumpf(rootJ, file, JSON_INDENT(2)); json_dumpf(rootJ, file, JSON_INDENT(2));
json_decref(rootJ);
std::fclose(file);
system::remove(path);
system::rename(tmpPath, path);
} }


void load(std::string path) { void load(std::string path) {
@@ -515,9 +519,9 @@ void load(std::string path) {
json_t* rootJ = json_loadf(file, 0, &error); json_t* rootJ = json_loadf(file, 0, &error);
if (!rootJ) if (!rootJ)
throw Exception("Settings file has invalid JSON at %d:%d %s", error.line, error.column, error.text); throw Exception("Settings file has invalid JSON at %d:%d %s", error.line, error.column, error.text);
DEFER({json_decref(rootJ);});


fromJson(rootJ); fromJson(rootJ);
json_decref(rootJ);
} }






Loading…
Cancel
Save