1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Dont write directly to files but rather write and copy a tmp file

This commit is contained in:
PilzAdam 2013-08-13 19:15:06 +02:00
parent c8930850e3
commit d718b0b34e
9 changed files with 101 additions and 69 deletions

View file

@ -437,13 +437,13 @@ void ServerEnvironment::serializePlayers(const std::string &savedir)
if(player->checkModified())
{
// Open file and serialize
std::ofstream os(path.c_str(), std::ios_base::binary);
if(os.good() == false)
std::ostringstream ss(std::ios_base::binary);
player->serialize(ss);
if(!fs::safeWriteToFile(path, ss.str()))
{
infostream<<"Failed to overwrite "<<path<<std::endl;
infostream<<"Failed to write "<<path<<std::endl;
continue;
}
player->serialize(os);
saved_players.insert(player);
} else {
saved_players.insert(player);
@ -493,13 +493,13 @@ void ServerEnvironment::serializePlayers(const std::string &savedir)
/*infostream<<"Saving player "<<player->getName()<<" to "
<<path<<std::endl;*/
// Open file and serialize
std::ofstream os(path.c_str(), std::ios_base::binary);
if(os.good() == false)
std::ostringstream ss(std::ios_base::binary);
player->serialize(ss);
if(!fs::safeWriteToFile(path, ss.str()))
{
infostream<<"Failed to overwrite "<<path<<std::endl;
infostream<<"Failed to write "<<path<<std::endl;
continue;
}
player->serialize(os);
saved_players.insert(player);
}
}
@ -581,19 +581,20 @@ void ServerEnvironment::saveMeta(const std::string &savedir)
std::string path = savedir + "/env_meta.txt";
// Open file and serialize
std::ofstream os(path.c_str(), std::ios_base::binary);
if(os.good() == false)
{
infostream<<"ServerEnvironment::saveMeta(): Failed to open "
<<path<<std::endl;
throw SerializationError("Couldn't save env meta");
}
std::ostringstream ss(std::ios_base::binary);
Settings args;
args.setU64("game_time", m_game_time);
args.setU64("time_of_day", getTimeOfDay());
args.writeLines(os);
os<<"EnvArgsEnd\n";
args.writeLines(ss);
ss<<"EnvArgsEnd\n";
if(!fs::safeWriteToFile(path, ss.str()))
{
infostream<<"ServerEnvironment::saveMeta(): Failed to write "
<<path<<std::endl;
throw SerializationError("Couldn't save env meta");
}
}
void ServerEnvironment::loadMeta(const std::string &savedir)