From d01d261f0e76cd928e7bad107c3ccc60c54eb751 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Tue, 6 Jul 2021 21:12:30 +0200 Subject: [PATCH] Safe ui-config file saving This prevents corrupt ui-config files in case the disk is full. This is a safeguard, since Ardour does not start if the config file is corrupt. --- gtk2_ardour/ui_config.cc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/gtk2_ardour/ui_config.cc b/gtk2_ardour/ui_config.cc index b2d302646f..df7c19b8cc 100644 --- a/gtk2_ardour/ui_config.cc +++ b/gtk2_ardour/ui_config.cc @@ -43,6 +43,7 @@ #include "pbd/unwind.h" #include "pbd/xml++.h" +#include "ardour/filename_extensions.h" #include "ardour/filesystem_paths.h" #include "ardour/search_paths.h" #include "ardour/revision.h" @@ -462,13 +463,24 @@ UIConfiguration::save_state() if (_dirty) { std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name); + std::string tmp = rcfile + temp_suffix; XMLTree tree; - tree.set_root (&get_state()); - if (!tree.write (rcfile.c_str())){ - error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg; + if (!tree.write (tmp.c_str())){ + error << string_compose (_("Config file %1 not saved"), tmp) << endmsg; + if (g_remove (tmp.c_str()) != 0) { + error << string_compose(_("Could not remove temporary ui-config file \"%1\" (%2)"), tmp, g_strerror (errno)) << endmsg; + } + return -1; + } + + if (::g_rename (tmp.c_str(), rcfile.c_str()) != 0) { + error << string_compose (_("could not rename temporary ui-config file %1 to %2 (%3)"), tmp, rcfile, g_strerror(errno)) << endmsg; + if (g_remove (tmp.c_str()) != 0) { + error << string_compose(_("Could not remove temporary ui-config file \"%1\" (%2)"), tmp, g_strerror (errno)) << endmsg; + } return -1; }