From a5425e3bf3cb0e0c798608f83813f9c56a9e2695 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 27 Jul 2022 18:40:49 +0200 Subject: [PATCH] database: write to temporary file and rename. This decreases the likelihood of ending up with a zero-byte (or missing) anope.db. Co-authored-by: Michael Stapelberg --- modules/database/db_flatfile.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/modules/database/db_flatfile.cpp b/modules/database/db_flatfile.cpp index 132616392..2d00162c5 100644 --- a/modules/database/db_flatfile.cpp +++ b/modules/database/db_flatfile.cpp @@ -312,10 +312,7 @@ class DBFlatFile : public Module, public Pipe else db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get("database", "anope.db"); - if (Anope::IsFile(db_name)) - rename(db_name.c_str(), (db_name + ".tmp").c_str()); - - std::fstream *fs = databases[s_type->GetOwner()] = new std::fstream(db_name.c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary); + std::fstream *fs = databases[s_type->GetOwner()] = new std::fstream((db_name + ".tmp").c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary); if (!fs->is_open()) Log(this) << "Unable to open " << db_name << " for writing"; @@ -349,14 +346,15 @@ class DBFlatFile : public Module, public Pipe this->Write("Unable to write database " + db_name); f->close(); - - if (Anope::IsFile((db_name + ".tmp").c_str())) - rename((db_name + ".tmp").c_str(), db_name.c_str()); } else { f->close(); - unlink((db_name + ".tmp").c_str()); +#ifdef _WIN32 + /* Windows rename() fails if the file already exists. */ + remove(db_name.c_str()); +#endif + rename((db_name + ".tmp").c_str(), db_name.c_str()); } delete f;