mirror of https://github.com/Icinga/icinga2.git
Dump state file atomically not to corrupt it
by using fsync(2) before close(2) and rename(2).
This commit is contained in:
parent
600fb0e3c2
commit
c9d6eecc7f
|
@ -1,5 +1,6 @@
|
||||||
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
|
#include "base/atomic-file.hpp"
|
||||||
#include "base/configobject.hpp"
|
#include "base/configobject.hpp"
|
||||||
#include "base/configobject-ti.cpp"
|
#include "base/configobject-ti.cpp"
|
||||||
#include "base/configtype.hpp"
|
#include "base/configtype.hpp"
|
||||||
|
@ -468,13 +469,7 @@ void ConfigObject::DumpObjects(const String& filename, int attributeTypes)
|
||||||
Log(LogWarning, "ConfigObject") << DiagnosticInformation(ex);
|
Log(LogWarning, "ConfigObject") << DiagnosticInformation(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::fstream fp;
|
AtomicFile fp (filename, 0600);
|
||||||
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"));
|
|
||||||
|
|
||||||
StdioStream::Ptr sfp = new StdioStream(&fp, false);
|
StdioStream::Ptr sfp = new StdioStream(&fp, false);
|
||||||
|
|
||||||
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
||||||
|
@ -502,10 +497,7 @@ void ConfigObject::DumpObjects(const String& filename, int attributeTypes)
|
||||||
}
|
}
|
||||||
|
|
||||||
sfp->Close();
|
sfp->Close();
|
||||||
|
fp.Commit();
|
||||||
fp.close();
|
|
||||||
|
|
||||||
Utility::RenameFile(tempFilename, filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigObject::RestoreObject(const String& message, int attributeTypes)
|
void ConfigObject::RestoreObject(const String& message, int attributeTypes)
|
||||||
|
|
Loading…
Reference in New Issue