summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2018-12-25 02:05:06 +0000
committerCarl Hetherington <cth@carlh.net>2018-12-25 02:05:06 +0000
commite6c52dcb012b441f2b0d3ba2345d91f2658644d2 (patch)
tree71037a588c6439aef5f2ee254d03a48489212435 /src
parent58b7779d3b5563077898fe5745802fd9a753523d (diff)
Fix failure to re-create config when it is bad (due to attempt to find a link
in the bad file). Improve config saving with low disk space; don't corrupt an existing file if we can't save a new one.
Diffstat (limited to 'src')
-rw-r--r--src/lib/config.cc28
1 files changed, 19 insertions, 9 deletions
diff --git a/src/lib/config.cc b/src/lib/config.cc
index eb2671365..9dd6f638e 100644
--- a/src/lib/config.cc
+++ b/src/lib/config.cc
@@ -982,13 +982,15 @@ Config::write_config () const
try {
string const s = doc.write_to_string_formatted ();
- boost::filesystem::path const cf = config_file ();
- FILE* f = fopen_boost (cf, "w");
+ boost::filesystem::path tmp (string(config_file().string()).append(".tmp"));
+ FILE* f = fopen_boost (tmp, "w");
if (!f) {
- throw FileError (_("Could not open file for writing"), cf);
+ throw FileError (_("Could not open file for writing"), tmp);
}
- checked_fwrite (s.c_str(), s.length(), f, cf);
+ checked_fwrite (s.c_str(), s.length(), f, tmp);
fclose (f);
+ boost::filesystem::remove (config_file());
+ boost::filesystem::rename (tmp, config_file());
} catch (xmlpp::exception& e) {
string s = e.what ();
trim (s);
@@ -1008,7 +1010,9 @@ Config::write_cinemas () const
}
try {
- doc.write_to_file_formatted (_cinemas_file.string ());
+ doc.write_to_file_formatted (_cinemas_file.string() + ".tmp");
+ boost::filesystem::remove (_cinemas_file);
+ boost::filesystem::rename (_cinemas_file.string() + ".tmp", _cinemas_file);
} catch (xmlpp::exception& e) {
string s = e.what ();
trim (s);
@@ -1233,10 +1237,16 @@ Config::config_file ()
}
/* See if there's a link */
- f.read_file (main);
- optional<string> link = f.optional_string_child("Link");
- if (link) {
- return *link;
+ try {
+ f.read_file (main);
+ optional<string> link = f.optional_string_child("Link");
+ if (link) {
+ return *link;
+ }
+ } catch (xmlpp::exception& e) {
+ /* There as a problem reading the main configuration file,
+ so there can't be a link.
+ */
}
return main;