From 97afc4dfd6de9b2b3d7ac0516c722c43b25ec100 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Tue, 19 May 2020 19:47:34 +0200 Subject: Safe config save This prevents corrupt rc-config files in case the disk is full. This is a safeguard, since Ardour does not start if the main config file is corrupt. --- libs/ardour/rc_configuration.cc | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/libs/ardour/rc_configuration.cc b/libs/ardour/rc_configuration.cc index 432001d43e..87ddbdfa61 100644 --- a/libs/ardour/rc_configuration.cc +++ b/libs/ardour/rc_configuration.cc @@ -35,6 +35,7 @@ #include "ardour/disk_reader.h" #include "ardour/disk_writer.h" #include "ardour/control_protocol_manager.h" +#include "ardour/filename_extensions.h" #include "ardour/filesystem_paths.h" #include "ardour/port.h" #include "ardour/rc_configuration.h" @@ -149,15 +150,24 @@ int RCConfiguration::save_state() { const std::string rcfile = Glib::build_filename (user_config_directory(), user_config_file_name); + const std::string tmpfile = rcfile + temp_suffix; + + XMLTree tree; + tree.set_root (&get_state()); + if (!tree.write (tmpfile.c_str())){ + error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg; + if (g_remove (tmpfile.c_str()) != 0) { + error << string_compose(_("Could not remove temporary config file at path \"%1\" (%2)"), tmpfile, g_strerror (errno)) << endmsg; + } + return -1; + } - // this test seems bogus? - if (!rcfile.empty()) { - XMLTree tree; - tree.set_root (&get_state()); - if (!tree.write (rcfile.c_str())){ - error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg; - return -1; + if (::g_rename (tmpfile.c_str(), rcfile.c_str()) != 0) { + error << string_compose (_("Could not rename temporary config file %1 to %2 (%3)"), tmpfile, rcfile, g_strerror(errno)) << endmsg; + if (g_remove (tmpfile.c_str()) != 0) { + error << string_compose(_("Could not remove temporary config file at path \"%1\" (%2)"), tmpfile, g_strerror (errno)) << endmsg; } + return -1; } return 0; -- cgit v1.2.3