Tidy up backing up of config files, improve the tests a little and fix it for the
[dcpomatic.git] / src / lib / config.cc
index 419436995dc829337bf59c0cf81a604c01741603..5496c0b3c788c9161e59eecc2bb6004adfacc190 100644 (file)
@@ -125,6 +125,9 @@ Config::set_defaults ()
 #ifdef DCPOMATIC_WINDOWS
        _win32_console = false;
 #endif
+       /* At the moment we don't write these files anywhere new after a version change, so they will be read from
+        * ~/.config/dcpomatic2 (or equivalent) and written back there.
+        */
        _cinemas_file = read_path ("cinemas.xml");
        _dkdm_recipients_file = read_path ("dkdm_recipients.xml");
        _show_hints_before_make_dcp = true;
@@ -212,16 +215,37 @@ Config::create_certificate_chain ()
 void
 Config::backup ()
 {
-       /* Make a copy of the configuration */
-       try {
+       using namespace boost::filesystem;
+
+       auto copy_adding_number = [](path const& path_to_copy) {
+
+               auto add_number = [](path const& path, int number) {
+                       return String::compose("%1.%2", path, number);
+               };
+
                int n = 1;
-               while (n < 100 && boost::filesystem::exists(write_path(String::compose("config.xml.%1", n)))) {
+               while (n < 100 && exists(add_number(path_to_copy, n))) {
                        ++n;
                }
+               copy_file(path_to_copy, add_number(path_to_copy, n));
+       };
 
-               boost::filesystem::copy_file(read_path("config.xml"), write_path(String::compose("config.xml.%1", n)));
-               boost::filesystem::copy_file(read_path("cinemas.xml"), write_path(String::compose("cinemas.xml.%1", n)));
-               boost::filesystem::copy_file(read_path("dkdm_recipients.xml"), write_path(String::compose("dkdm_recipients.xml.%1", n)));
+       /* Make a backup copy of any config.xml, cinemas.xml, dkdm_recipients.xml that we might be about
+        * to write over.  This is more intended for the situation where we have a corrupted config.xml,
+        * and decide to overwrite it with a new one (possibly losing important details in the corrupted
+        * file).  But we might as well back up the other files while we're about it.
+        */
+       try {
+               /* This uses the State::write_path stuff so, e.g. for a current version 2.16 we might copy
+                * ~/.config/dcpomatic2/2.16/config.xml to ~/.config/dcpomatic2/2.16/config.xml.1
+                */
+               copy_adding_number (config_write_file());
+
+               /* These do not use State::write_path, so whatever path is in the Config we will copy
+                * adding a number.
+                */
+               copy_adding_number (_cinemas_file);
+               copy_adding_number (_dkdm_recipients_file);
        } catch (...) {}
 }