Clean up only old temp files

refs #8615
This commit is contained in:
Alexander A. Klimov 2021-02-25 14:06:07 +01:00
parent aa4f53009e
commit 1f238724e4
4 changed files with 16 additions and 2 deletions

View File

@ -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);
}

View File

@ -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.

View File

@ -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);

View File

@ -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);
}