From f3e5d56001642c6cad6b359b98b94c701836485d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 21 Jul 2022 18:01:22 +0200 Subject: [PATCH] Dump state file atomically not to corrupt it by using fsync(2) before close(2) and rename(2). --- lib/base/configobject.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lib/base/configobject.cpp b/lib/base/configobject.cpp index d4592b4ef3a..144afa4a985 100644 --- a/lib/base/configobject.cpp +++ b/lib/base/configobject.cpp @@ -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" @@ -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()) { @@ -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)