Let config/cinemas/dkdm_recipients take care of their own recovery.
authorCarl Hetherington <cth@carlh.net>
Sun, 4 Sep 2022 19:26:24 +0000 (21:26 +0200)
committerCarl Hetherington <cth@carlh.net>
Mon, 5 Sep 2022 21:52:03 +0000 (23:52 +0200)
This should mean that if one fails to load, but the others are OK,
we don't re-write everything.  I hope this has no bad side-effects
that I didn't think of.

It's particularly helpful in the case of #2319 where a failure to
load cinemas.xml (which was being created by another tool) caused
the config to be re-written, breaking all existing DKDMs.

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

index 94595f9f8ac88a78d7a9df26035dac5539799b7e..18c792223b020ff1c195f2fb0eca7c1b06e1f84e 100644 (file)
@@ -608,7 +608,7 @@ try
        _export.read(f.optional_node_child("Export"));
 }
 catch (...) {
        _export.read(f.optional_node_child("Export"));
 }
 catch (...) {
-       if (have_existing("config.xml") || have_existing("cinemas.xml") || have_existing("dkdm_recipients.xml")) {
+       if (have_existing("config.xml")) {
                backup ();
                /* We have a config file but it didn't load */
                FailedToLoad(LoadFailure::CONFIG);
                backup ();
                /* We have a config file but it didn't load */
                FailedToLoad(LoadFailure::CONFIG);
@@ -618,7 +618,7 @@ catch (...) {
        _signer_chain = create_certificate_chain ();
        /* And similar for decryption of KDMs */
        _decryption_chain = create_certificate_chain ();
        _signer_chain = create_certificate_chain ();
        /* And similar for decryption of KDMs */
        _decryption_chain = create_certificate_chain ();
-       write ();
+       write_config();
 }
 
 
 }
 
 
@@ -626,9 +626,15 @@ void
 Config::read_cinemas()
 {
        if (boost::filesystem::exists (_cinemas_file)) {
 Config::read_cinemas()
 {
        if (boost::filesystem::exists (_cinemas_file)) {
-               cxml::Document f ("Cinemas");
-               f.read_file (_cinemas_file);
-               read_cinemas (f);
+               try {
+                       cxml::Document f("Cinemas");
+                       f.read_file(_cinemas_file);
+                       read_cinemas(f);
+               } catch (...) {
+                       backup();
+                       FailedToLoad(LoadFailure::CINEMAS);
+                       write_cinemas();
+               }
        }
 }
 
        }
 }
 
@@ -637,9 +643,15 @@ void
 Config::read_dkdm_recipients()
 {
        if (boost::filesystem::exists (_dkdm_recipients_file)) {
 Config::read_dkdm_recipients()
 {
        if (boost::filesystem::exists (_dkdm_recipients_file)) {
-               cxml::Document f ("DKDMRecipients");
-               f.read_file (_dkdm_recipients_file);
-               read_dkdm_recipients (f);
+               try {
+                       cxml::Document f("DKDMRecipients");
+                       f.read_file(_dkdm_recipients_file);
+                       read_dkdm_recipients(f);
+               } catch (...) {
+                       backup();
+                       FailedToLoad(LoadFailure::DKDM_RECIPIENTS);
+                       write_dkdm_recipients();
+               }
        }
 }
 
        }
 }
 
index 687bea95676dc097268127e3cf884bf00fce101c..fc3a27601e4d763a646b744621652a98c3b4810a 100644 (file)
@@ -215,7 +215,7 @@ BOOST_AUTO_TEST_CASE (config_keep_cinemas_if_making_new_config)
        boost::filesystem::remove (dir / "2.16" / "config.xml");
        Config::instance();
 
        boost::filesystem::remove (dir / "2.16" / "config.xml");
        Config::instance();
 
-       check_text_file (dir / "backup_for_test.xml", dir / "cinemas.xml.1");
+       check_text_file(dir / "backup_for_test.xml", dir / "cinemas.xml");
 }
 
 
 }