summaryrefslogtreecommitdiff
path: root/src/environment.cpp
diff options
context:
space:
mode:
authorPilzAdam <pilzadam@minetest.net>2013-08-13 19:15:06 +0200
committerPilzAdam <pilzadam@minetest.net>2013-08-13 22:05:45 +0200
commitd718b0b34eda84744778fa12a01d5be5f03753d3 (patch)
tree6ee450e7f1078a114c71f73ead16310f13bbc00c /src/environment.cpp
parentc8930850e37dab9820049152a3e668a315a97560 (diff)
downloadminetest-d718b0b34eda84744778fa12a01d5be5f03753d3.tar.gz
minetest-d718b0b34eda84744778fa12a01d5be5f03753d3.tar.bz2
minetest-d718b0b34eda84744778fa12a01d5be5f03753d3.zip
Dont write directly to files but rather write and copy a tmp file
Diffstat (limited to 'src/environment.cpp')
-rw-r--r--src/environment.cpp35
1 files changed, 18 insertions, 17 deletions
diff --git a/src/environment.cpp b/src/environment.cpp
index 35983aaaf..eacc2a008 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -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)