1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-31 18:07:14 +01:00
SqMod/vendor/POCO/Foundation/src/PurgeStrategy.cpp
Sandu Liviu Catalin 4a6bfc086c Major plugin refactor and cleanup.
Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
2021-01-30 08:51:39 +02:00

132 lines
1.9 KiB
C++

//
// PurgeStrategy.cpp
//
// Library: Foundation
// Package: Logging
// Module: FileChannel
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/PurgeStrategy.h"
#include "Poco/Path.h"
#include "Poco/DirectoryIterator.h"
#include "Poco/Timestamp.h"
namespace Poco {
//
// PurgeStrategy
//
PurgeStrategy::PurgeStrategy()
{
}
PurgeStrategy::~PurgeStrategy()
{
}
void PurgeStrategy::list(const std::string& path, std::vector<File>& files)
{
Path p(path);
p.makeAbsolute();
Path parent = p.parent();
std::string baseName = p.getFileName();
baseName.append(".");
DirectoryIterator it(parent);
DirectoryIterator end;
while (it != end)
{
if (it.name().compare(0, baseName.size(), baseName) == 0)
{
files.push_back(*it);
}
++it;
}
}
//
// PurgeByAgeStrategy
//
PurgeByAgeStrategy::PurgeByAgeStrategy(const Timespan& age): _age(age)
{
}
PurgeByAgeStrategy::~PurgeByAgeStrategy()
{
}
void PurgeByAgeStrategy::purge(const std::string& path)
{
std::vector<File> files;
list(path, files);
for (auto& f: files)
{
if (f.getLastModified().isElapsed(_age.totalMicroseconds()))
{
f.remove();
}
}
}
//
// PurgeByCountStrategy
//
PurgeByCountStrategy::PurgeByCountStrategy(int count): _count(count)
{
poco_assert(count > 0);
}
PurgeByCountStrategy::~PurgeByCountStrategy()
{
}
void PurgeByCountStrategy::purge(const std::string& path)
{
std::vector<File> files;
list(path, files);
while (files.size() > _count)
{
std::vector<File>::iterator it = files.begin();
std::vector<File>::iterator purgeIt = it;
Timestamp purgeTS = purgeIt->getLastModified();
++it;
while (it != files.end())
{
Timestamp md(it->getLastModified());
if (md <= purgeTS)
{
purgeTS = md;
purgeIt = it;
}
++it;
}
purgeIt->remove();
files.erase(purgeIt);
}
}
} // namespace Poco