From d718b0b34eda84744778fa12a01d5be5f03753d3 Mon Sep 17 00:00:00 2001 From: PilzAdam Date: Tue, 13 Aug 2013 19:15:06 +0200 Subject: [PATCH] Dont write directly to files but rather write and copy a tmp file --- src/ban.cpp | 17 +++++++++-------- src/environment.cpp | 35 ++++++++++++++++++----------------- src/filesys.cpp | 25 +++++++++++++++++++++++++ src/filesys.h | 2 ++ src/map.cpp | 28 +++++++++++++++------------- src/mapgen.cpp | 17 ++++++++++------- src/serverlist.cpp | 20 ++++++++------------ src/settings.h | 21 +++++++++++---------- src/subgame.cpp | 5 +++-- 9 files changed, 101 insertions(+), 69 deletions(-) diff --git a/src/ban.cpp b/src/ban.cpp index 75bae746..f9d32b60 100644 --- a/src/ban.cpp +++ b/src/ban.cpp @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "strfnd.h" #include "log.h" +#include "filesys.h" BanManager::BanManager(const std::string &banfilepath): m_banfilepath(banfilepath), @@ -76,20 +77,20 @@ void BanManager::save() { JMutexAutoLock lock(m_mutex); infostream<<"BanManager: saving to "<::iterator i = m_ips.begin(); i != m_ips.end(); i++) { - os<first<<"|"<second<<"\n"; + ss << i->first << "|" << i->second << "\n"; } + + if(!fs::safeWriteToFile(m_banfilepath, ss.str())) { + infostream<<"BanManager: failed saving to "<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 "<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 "<getName()<<" to " <serialize(ss); + if(!fs::safeWriteToFile(path, ss.str())) { - infostream<<"Failed to overwrite "<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 " - < #include #include +#include +#include #include "log.h" namespace fs @@ -684,5 +686,28 @@ std::string RemoveRelativePathComponents(std::string path) return path.substr(0, pos); } +bool safeWriteToFile(const std::string &path, const std::string &content) +{ + std::string tmp_file = path + ".~mt"; + + // Write to a tmp file + std::ofstream os(tmp_file.c_str(), std::ios::binary); + if (!os.good()) + return false; + os << content; + os.flush(); + os.close(); + if (os.fail()) + return false; + + // Copy file +#ifdef _WIN32 + remove(path.c_str()); + return (rename(tmp_file.c_str(), path.c_str()) == 0); +#else + return (rename(tmp_file.c_str(), path.c_str()) == 0); +#endif +} + } // namespace fs diff --git a/src/filesys.h b/src/filesys.h index d0bf400c..1b3659af 100644 --- a/src/filesys.h +++ b/src/filesys.h @@ -98,6 +98,8 @@ std::string RemoveLastPathComponent(std::string path, // this does not resolve symlinks and check for existence of directories. std::string RemoveRelativePathComponents(std::string path); +bool safeWriteToFile(const std::string &path, const std::string &content); + }//fs #endif diff --git a/src/map.cpp b/src/map.cpp index da20b5b0..331aa48d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -3490,20 +3490,21 @@ void ServerMap::saveMapMeta() createDirs(m_savedir); std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt"; - std::ofstream os(fullpath.c_str(), std::ios_base::binary); - if(os.good() == false) - { - infostream<<"ERROR: ServerMap::saveMapMeta(): " - <<"could not open"<setParamsToSettings(¶ms); - params.writeLines(os); + params.writeLines(ss); - os<<"[end_of_params]\n"; + ss<<"[end_of_params]\n"; + + if(!fs::safeWriteToFile(fullpath, ss.str())) + { + infostream<<"ERROR: ServerMap::saveMapMeta(): " + <<"could not write "<serialize(o, version); + sector->serialize(ss, version); + + if(!fs::safeWriteToFile(fullpath, ss.str())) + throw FileNotGoodException("Cannot write sector metafile"); sector->differs_from_disk = false; } diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 97d672b0..be65694a 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -36,6 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen_v6.h" #include "mapgen_v7.h" #include "util/serialize.h" +#include "filesys.h" FlagDesc flagdesc_mapgen[] = { {"trees", MG_TREES}, @@ -756,24 +757,26 @@ bool DecoSchematic::loadSchematicFile() { 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always */ void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) { - std::ofstream os(filename.c_str(), std::ios_base::binary); + std::ostringstream ss(std::ios_base::binary); - writeU32(os, MTSCHEM_FILE_SIGNATURE); // signature - writeU16(os, 2); // version - writeV3S16(os, size); // schematic size + writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature + writeU16(ss, 2); // version + writeV3S16(ss, size); // schematic size std::vector usednodes; int nodecount = size.X * size.Y * size.Z; build_nnlist_and_update_ids(schematic, nodecount, &usednodes); u16 numids = usednodes.size(); - writeU16(os, numids); // name count + writeU16(ss, numids); // name count for (int i = 0; i != numids; i++) - os << serializeString(ndef->get(usednodes[i]).name); // node names + ss << serializeString(ndef->get(usednodes[i]).name); // node names // compressed bulk node data - MapNode::serializeBulk(os, SER_FMT_VER_HIGHEST_WRITE, schematic, + MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schematic, nodecount, 2, 2, true); + + fs::safeWriteToFile(filename, ss.str()); } diff --git a/src/serverlist.cpp b/src/serverlist.cpp index bc09f6c0..4db5f3ec 100644 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -105,13 +105,11 @@ bool deleteEntry (ServerListSpec server) } std::string path = ServerList::getFilePath(); - std::ofstream stream (path.c_str()); - if (stream.is_open()) - { - stream< #include #include +#include "filesys.h" enum ValueType { @@ -308,14 +309,7 @@ public: // Write stuff back { - std::ofstream os(filename); - if(os.good() == false) - { - errorstream<<"Error opening configuration file" - " for writing: \"" - <second; infostream<<"Adding \""<