From c250f7cf2d8bb7a35772033db3d2aa07d6fd3c82 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 26 Feb 2018 13:14:31 +0000 Subject: [PATCH 1/1] Make a backup of configuration when we load in a Version 2 file, and bump our config file version to 3. --- src/lib/config.cc | 37 +++++++++++++++++++++++-------------- src/lib/config.h | 3 +++ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/lib/config.cc b/src/lib/config.cc index 503ca9c5f..ec5b05afa 100644 --- a/src/lib/config.cc +++ b/src/lib/config.cc @@ -64,6 +64,7 @@ using boost::algorithm::trim; using dcp::raw_convert; Config* Config::_instance = 0; +int const Config::_current_version = 3; boost::signals2::signal Config::FailedToLoad; boost::signals2::signal Config::Warning; boost::optional Config::test_path; @@ -180,6 +181,21 @@ Config::create_certificate_chain () ); } +void +Config::backup () +{ + /* Make a copy of the configuration */ + try { + int n = 1; + while (n < 100 && boost::filesystem::exists(path(String::compose("config.xml.%1", n)))) { + ++n; + } + + boost::filesystem::copy_file(path("config.xml", false), path(String::compose("config.xml.%1", n), false)); + boost::filesystem::copy_file(path("cinemas.xml", false), path(String::compose("cinemas.xml.%1", n), false)); + } catch (...) {} +} + void Config::read () try @@ -188,6 +204,10 @@ try f.read_file (config_file ()); optional version = f.optional_number_child ("Version"); + if (version && *version < _current_version) { + /* Back up the old config before we re-write it in a back-incompatible way */ + backup (); + } if (f.optional_number_child("NumLocalEncodingThreads")) { _master_encoding_threads = _server_encoding_threads = f.optional_number_child("NumLocalEncodingThreads").get(); @@ -400,18 +420,7 @@ try } catch (...) { if (have_existing ("config.xml")) { - - /* Make a copy of the configuration */ - try { - int n = 1; - while (n < 100 && boost::filesystem::exists(path(String::compose("config.xml.%1", n)))) { - ++n; - } - - boost::filesystem::copy_file(path("config.xml", false), path(String::compose("config.xml.%1", n), false)); - boost::filesystem::copy_file(path("cinemas.xml", false), path(String::compose("cinemas.xml.%1", n), false)); - } catch (...) {} - + backup (); /* We have a config file but it didn't load */ FailedToLoad (); } @@ -476,8 +485,8 @@ Config::write_config () const xmlpp::Document doc; xmlpp::Element* root = doc.create_root_node ("Config"); - /* [XML] Version The version number of the configuration file format; currently 2. */ - root->add_child("Version")->add_child_text ("2"); + /* [XML] Version The version number of the configuration file format */ + root->add_child("Version")->add_child_text (String::compose ("%1", _current_version)); /* [XML] MasterEncodingThreads Number of encoding threads to use when running as master. */ root->add_child("MasterEncodingThreads")->add_child_text (raw_convert (_master_encoding_threads)); /* [XML] ServerEncodingThreads Number of encoding threads to use when running as server. */ diff --git a/src/lib/config.h b/src/lib/config.h index d50e585e3..90ebb0b33 100644 --- a/src/lib/config.h +++ b/src/lib/config.h @@ -729,6 +729,7 @@ private: boost::shared_ptr create_certificate_chain (); boost::filesystem::path directory_or (boost::optional dir, boost::filesystem::path a) const; void add_to_history_internal (std::vector& h, boost::filesystem::path p); + void backup (); template void maybe_set (T& member, T new_value, Property prop = OTHER) { @@ -844,6 +845,8 @@ private: int _frames_in_memory_multiplier; boost::optional _decode_reduction; + static int const _current_version; + /** Singleton instance, or 0 */ static Config* _instance; }; -- 2.30.2