Skip to content

Commit

Permalink
Dump state file atomically not to corrupt it
Browse files Browse the repository at this point in the history
by using fsync(2) before close(2) and rename(2).
  • Loading branch information
Al2Klimov committed Jul 25, 2022
1 parent 86d7f15 commit 42edd2e
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions lib/base/configobject.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/atomic-file.hpp"
#include "base/configobject.hpp"
#include "base/configobject-ti.cpp"
#include "base/configtype.hpp"
Expand Down Expand Up @@ -468,13 +469,7 @@ void ConfigObject::DumpObjects(const String& filename, int attributeTypes)
Log(LogWarning, "ConfigObject") << DiagnosticInformation(ex);
}

std::fstream fp;
String tempFilename = Utility::CreateTempFile(filename + ".tmp.XXXXXX", 0600, fp);
fp.exceptions(std::ofstream::failbit | std::ofstream::badbit);

if (!fp)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));

AtomicFile fp (filename, 0600);
StdioStream::Ptr sfp = new StdioStream(&fp, false);

for (const Type::Ptr& type : Type::GetAllTypes()) {
Expand Down Expand Up @@ -502,10 +497,7 @@ void ConfigObject::DumpObjects(const String& filename, int attributeTypes)
}

sfp->Close();

fp.close();

Utility::RenameFile(tempFilename, filename);
fp.Commit();
}

void ConfigObject::RestoreObject(const String& message, int attributeTypes)
Expand Down

0 comments on commit 42edd2e

Please sign in to comment.