diff --git a/lib/base/configobject.cpp b/lib/base/configobject.cpp index fba5d1549..605ad7ce6 100644 --- a/lib/base/configobject.cpp +++ b/lib/base/configobject.cpp @@ -469,7 +469,7 @@ void ConfigObject::DumpObjects(const String& filename, int attributeTypes) << "Dumping program state to file '" << filename << "'"; try { - Utility::Glob(filename + ".tmp.*", &Utility::Remove, GlobFile); + Utility::Glob(filename + ".tmp.*", &Utility::RemoveIfOld, GlobFile); } catch (const std::exception& ex) { Log(LogWarning, "ConfigObject") << DiagnosticInformation(ex); } diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 196e7d5b6..461a96b54 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -719,6 +719,19 @@ void Utility::RemoveDirRecursive(const String& path) (void)fs::remove_all(fs::path(path.Begin(), path.End())); } +void Utility::RemoveIfOld(const String& path) +{ + namespace fs = boost::filesystem; + + fs::path p (path.Begin(), path.End()); + boost::system::error_code ec; + auto modTime (fs::last_write_time(p, ec)); + + if (!ec && modTime < GetTime() - 24 * 60 * 60) { + (void)fs::remove(p); + } +} + /* * Copies a source file to a target location. * Caller must ensure that the target's base directory exists and is writable. diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index c2d1a0857..2bb8eec9e 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -116,6 +116,7 @@ public: static void Remove(const String& path); static void RemoveDirRecursive(const String& path); + static void RemoveIfOld(const String& path); static void CopyFile(const String& source, const String& target); static void RenameFile(const String& source, const String& target); diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index e6ddf6e7b..2f804696b 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -165,7 +165,7 @@ void IcingaApplication::DumpModifiedAttributes() String path = Configuration::ModAttrPath; try { - Utility::Glob(path + ".tmp.*", &Utility::Remove, GlobFile); + Utility::Glob(path + ".tmp.*", &Utility::RemoveIfOld, GlobFile); } catch (const std::exception& ex) { Log(LogWarning, "IcingaApplication") << DiagnosticInformation(ex); }