Don't give up on backups if config.xml isn't there (#2185).
authorCarl Hetherington <cth@carlh.net>
Thu, 10 Feb 2022 20:06:43 +0000 (21:06 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 10 Feb 2022 20:16:09 +0000 (21:16 +0100)
Even if it isn't, we still want to try to back up other stuff.
Previously if copying config.xml threw an exception we'd just
give up.

src/lib/config.cc
test/config_test.cc

index 5496c0b3c788c9161e59eecc2bb6004adfacc190..0d70c16fc4d493cdd19e84ae1f9bb3d16c950d71 100644 (file)
@@ -227,7 +227,8 @@ Config::backup ()
                while (n < 100 && exists(add_number(path_to_copy, n))) {
                        ++n;
                }
-               copy_file(path_to_copy, add_number(path_to_copy, n));
+               boost::system::error_code ec;
+               copy_file(path_to_copy, add_number(path_to_copy, n), ec);
        };
 
        /* Make a backup copy of any config.xml, cinemas.xml, dkdm_recipients.xml that we might be about
@@ -235,18 +236,17 @@ Config::backup ()
         * 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 (...) {}
+       /* 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);
 }
 
 void
@@ -591,7 +591,7 @@ try
        }
 }
 catch (...) {
-       if (have_existing ("config.xml")) {
+       if (have_existing("config.xml") || have_existing("cinemas.xml") || have_existing("dkdm_recipients.xml")) {
                backup ();
                /* We have a config file but it didn't load */
                FailedToLoad ();
index 0e6a05ac04e384e420ecbc2d900ba3415e989cda..6a65deb2e1a95f638dee2bb991aae5c92a094a51 100644 (file)
 */
 
 
+#include "lib/cinema.h"
 #include "lib/config.h"
 #include "test.h"
 #include <boost/test/unit_test.hpp>
 #include <fstream>
 
 
+using std::list;
 using std::ofstream;
+using std::make_shared;
 using std::string;
 using boost::optional;
 
@@ -190,3 +193,26 @@ BOOST_AUTO_TEST_CASE (config_upgrade_test)
        BOOST_REQUIRE (!boost::filesystem::exists(dir / "2.16" / "cinemas.xml"));
 }
 
+
+BOOST_AUTO_TEST_CASE (config_keep_cinemas_if_making_new_config)
+{
+       boost::filesystem::path dir = "build/test/config_keep_cinemas_if_making_new_config";
+       Config::override_path = dir;
+       Config::drop ();
+       boost::filesystem::remove_all (dir);
+       boost::filesystem::create_directories (dir);
+
+       Config::instance()->write();
+
+       Config::instance()->add_cinema(make_shared<Cinema>("My Great Cinema", list<string>(), "", 0, 0));
+       Config::instance()->write();
+
+       boost::filesystem::copy_file (dir / "cinemas.xml", dir / "backup_for_test.xml");
+
+       Config::drop ();
+       boost::filesystem::remove (dir / "2.16" / "config.xml");
+       Config::instance();
+
+       check_text_file (dir / "backup_for_test.xml", dir / "cinemas.xml.1");
+}
+