Avoid configurations xml lost if having disk space issue, see #4709
This commit is contained in:
parent
d6a3e48d03
commit
de09566b4a
@ -27,6 +27,7 @@
|
||||
#include "io/xml_node.hpp"
|
||||
#include "online/online_player_profile.hpp"
|
||||
#include "race/race_manager.hpp"
|
||||
#include "utils/file_utils.hpp"
|
||||
#include "utils/log.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
|
||||
@ -250,7 +251,8 @@ void PlayerManager::save()
|
||||
std::string filename = file_manager->getUserConfigFile("players.xml");
|
||||
try
|
||||
{
|
||||
UTFWriter players_file(filename.c_str(), false);
|
||||
// Save to a new file and rename later to avoid disk space problem, see #4709
|
||||
UTFWriter players_file((filename + "new").c_str(), false);
|
||||
|
||||
players_file << "<?xml version=\"1.0\"?>\n";
|
||||
players_file << "<players version=\"1\" >\n";
|
||||
@ -269,6 +271,8 @@ void PlayerManager::save()
|
||||
}
|
||||
players_file << "</players>\n";
|
||||
players_file.close();
|
||||
file_manager->removeFile(filename);
|
||||
FileUtils::renameU8Path(filename + "new", filename);
|
||||
}
|
||||
catch (std::runtime_error& e)
|
||||
{
|
||||
|
@ -742,10 +742,13 @@ void UserConfig::saveConfig()
|
||||
try
|
||||
{
|
||||
std::string s = ss.str();
|
||||
std::ofstream configfile(FileUtils::getPortableWritingPath(filename),
|
||||
// Save to a new file and rename later to avoid disk space problem, see #4709
|
||||
std::ofstream configfile(FileUtils::getPortableWritingPath(filename + "new"),
|
||||
std::ofstream::out);
|
||||
configfile << ss.rdbuf();
|
||||
configfile.close();
|
||||
file_manager->removeFile(filename);
|
||||
FileUtils::renameU8Path(filename + "new", filename);
|
||||
}
|
||||
catch (std::runtime_error& e)
|
||||
{
|
||||
|
@ -545,11 +545,11 @@ bool DeviceManager::load()
|
||||
// -----------------------------------------------------------------------------
|
||||
void DeviceManager::save()
|
||||
{
|
||||
static std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME);
|
||||
std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME);
|
||||
if(UserConfigParams::logMisc()) Log::info("Device manager","Serializing input.xml...");
|
||||
|
||||
|
||||
std::ofstream configfile(FileUtils::getPortableWritingPath(filepath));
|
||||
// Save to a new file and rename later to avoid disk space problem, see #4709
|
||||
std::ofstream configfile(FileUtils::getPortableWritingPath(filepath + "new"));
|
||||
|
||||
if(!configfile.is_open())
|
||||
{
|
||||
@ -558,29 +558,40 @@ void DeviceManager::save()
|
||||
}
|
||||
|
||||
|
||||
configfile << "<input version=\"" << INPUT_FILE_VERSION << "\">\n\n";
|
||||
|
||||
configfile << "<!--\n"
|
||||
<< "Event 1 : Keyboard button press\n"
|
||||
<< " 'id' indicates which button, as defined by irrlicht's EKEY_CODE enum\n"
|
||||
<< "Event 2 : Gamepad stick motion\n"
|
||||
<< " 'id' indicates which stick, starting from 0\n"
|
||||
<< " 'direction' 0 means negative, 1 means positive\n"
|
||||
<< "Event 3 : Gamepad button press\n"
|
||||
<< " 'id' indicates which button, starting from 0\n"
|
||||
<< "-->\n\n";
|
||||
|
||||
for(unsigned int n=0; n<m_keyboard_configs.size(); n++)
|
||||
try
|
||||
{
|
||||
m_keyboard_configs[n].save(configfile);
|
||||
configfile << "<input version=\"" << INPUT_FILE_VERSION << "\">\n\n";
|
||||
|
||||
configfile << "<!--\n"
|
||||
<< "Event 1 : Keyboard button press\n"
|
||||
<< " 'id' indicates which button, as defined by irrlicht's EKEY_CODE enum\n"
|
||||
<< "Event 2 : Gamepad stick motion\n"
|
||||
<< " 'id' indicates which stick, starting from 0\n"
|
||||
<< " 'direction' 0 means negative, 1 means positive\n"
|
||||
<< "Event 3 : Gamepad button press\n"
|
||||
<< " 'id' indicates which button, starting from 0\n"
|
||||
<< "-->\n\n";
|
||||
|
||||
for(unsigned int n=0; n<m_keyboard_configs.size(); n++)
|
||||
{
|
||||
m_keyboard_configs[n].save(configfile);
|
||||
}
|
||||
for(unsigned int n=0; n<m_gamepad_configs.size(); n++)
|
||||
{
|
||||
m_gamepad_configs[n].save(configfile);
|
||||
}
|
||||
|
||||
configfile << "</input>\n";
|
||||
configfile.close();
|
||||
file_manager->removeFile(filepath);
|
||||
FileUtils::renameU8Path(filepath + "new", filepath);
|
||||
}
|
||||
for(unsigned int n=0; n<m_gamepad_configs.size(); n++)
|
||||
catch (std::exception& e)
|
||||
{
|
||||
m_gamepad_configs[n].save(configfile);
|
||||
Log::error("Device manager", "Failed to write %s to %s, "
|
||||
"because %s", INPUT_FILE_NAME, filepath.c_str(), e.what());
|
||||
}
|
||||
|
||||
configfile << "</input>\n";
|
||||
configfile.close();
|
||||
if(UserConfigParams::logMisc()) Log::info("Device manager","Serialization complete.");
|
||||
} // save
|
||||
|
||||
|
@ -186,10 +186,13 @@ void writeServerConfigToDisk()
|
||||
const std::string& config_xml = getServerConfigXML();
|
||||
try
|
||||
{
|
||||
// Save to a new file and rename later to avoid disk space problem, see #4709
|
||||
std::ofstream configfile(FileUtils::getPortableWritingPath(
|
||||
g_server_config_path), std::ofstream::out);
|
||||
g_server_config_path + "new"), std::ofstream::out);
|
||||
configfile << config_xml;
|
||||
configfile.close();
|
||||
file_manager->removeFile(g_server_config_path);
|
||||
FileUtils::renameU8Path(g_server_config_path + "new", g_server_config_path);
|
||||
}
|
||||
catch (std::runtime_error& e)
|
||||
{
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "io/utf_writer.hpp"
|
||||
#include "race/race_manager.hpp"
|
||||
#include "utils/constants.hpp"
|
||||
#include "utils/file_utils.hpp"
|
||||
#include "utils/log.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
@ -150,7 +151,8 @@ void HighscoreManager::saveHighscores()
|
||||
|
||||
try
|
||||
{
|
||||
UTFWriter highscore_file(m_filename.c_str(), false);
|
||||
// Save to a new file and rename later to avoid disk space problem, see #4709
|
||||
UTFWriter highscore_file((m_filename + "new").c_str(), false);
|
||||
highscore_file << "<?xml version=\"1.0\"?>\n";
|
||||
highscore_file << "<highscores version=\"" << CURRENT_HSCORE_FILE_VERSION << "\">\n";
|
||||
|
||||
@ -160,11 +162,13 @@ void HighscoreManager::saveHighscores()
|
||||
}
|
||||
highscore_file << "</highscores>\n";
|
||||
highscore_file.close();
|
||||
file_manager->removeFile(m_filename);
|
||||
FileUtils::renameU8Path(m_filename + "new", m_filename);
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
Log::error("Highscore Manager","Problems saving highscores in '%s'\n", m_filename.c_str());
|
||||
puts(e.what());
|
||||
Log::error("Highscore Manager","Problems saving highscores in '%s' because %s",
|
||||
m_filename.c_str(), e.what());
|
||||
m_can_write=false;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user