| 1 | // |
|---|---|
| 2 | // PurgeStrategy.cpp |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Logging |
| 6 | // Module: FileChannel |
| 7 | // |
| 8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/PurgeStrategy.h" |
| 16 | #include "Poco/Path.h" |
| 17 | #include "Poco/DirectoryIterator.h" |
| 18 | #include "Poco/Timestamp.h" |
| 19 | |
| 20 | |
| 21 | namespace Poco { |
| 22 | |
| 23 | |
| 24 | // |
| 25 | // PurgeStrategy |
| 26 | // |
| 27 | |
| 28 | |
| 29 | PurgeStrategy::PurgeStrategy() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | |
| 34 | PurgeStrategy::~PurgeStrategy() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void PurgeStrategy::list(const std::string& path, std::vector<File>& files) |
| 40 | { |
| 41 | Path p(path); |
| 42 | p.makeAbsolute(); |
| 43 | Path parent = p.parent(); |
| 44 | std::string baseName = p.getFileName(); |
| 45 | baseName.append("."); |
| 46 | |
| 47 | DirectoryIterator it(parent); |
| 48 | DirectoryIterator end; |
| 49 | while (it != end) |
| 50 | { |
| 51 | if (it.name().compare(0, baseName.size(), baseName) == 0) |
| 52 | { |
| 53 | files.push_back(*it); |
| 54 | } |
| 55 | ++it; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | // |
| 61 | // PurgeByAgeStrategy |
| 62 | // |
| 63 | |
| 64 | |
| 65 | PurgeByAgeStrategy::PurgeByAgeStrategy(const Timespan& age): _age(age) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | |
| 70 | PurgeByAgeStrategy::~PurgeByAgeStrategy() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void PurgeByAgeStrategy::purge(const std::string& path) |
| 76 | { |
| 77 | std::vector<File> files; |
| 78 | list(path, files); |
| 79 | for (std::vector<File>::iterator it = files.begin(); it != files.end(); ++it) |
| 80 | { |
| 81 | if (it->getLastModified().isElapsed(_age.totalMicroseconds())) |
| 82 | { |
| 83 | it->remove(); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | // |
| 90 | // PurgeByCountStrategy |
| 91 | // |
| 92 | |
| 93 | |
| 94 | PurgeByCountStrategy::PurgeByCountStrategy(int count): _count(count) |
| 95 | { |
| 96 | poco_assert(count > 0); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | PurgeByCountStrategy::~PurgeByCountStrategy() |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void PurgeByCountStrategy::purge(const std::string& path) |
| 106 | { |
| 107 | std::vector<File> files; |
| 108 | list(path, files); |
| 109 | while (files.size() > _count) |
| 110 | { |
| 111 | std::vector<File>::iterator it = files.begin(); |
| 112 | std::vector<File>::iterator purgeIt = it; |
| 113 | Timestamp purgeTS = purgeIt->getLastModified(); |
| 114 | ++it; |
| 115 | while (it != files.end()) |
| 116 | { |
| 117 | Timestamp md(it->getLastModified()); |
| 118 | if (md <= purgeTS) |
| 119 | { |
| 120 | purgeTS = md; |
| 121 | purgeIt = it; |
| 122 | } |
| 123 | ++it; |
| 124 | } |
| 125 | purgeIt->remove(); |
| 126 | files.erase(purgeIt); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void PurgeOneFileStrategy::purge(const std::string& path) |
| 132 | { |
| 133 | std::vector<File> files; |
| 134 | list(path, files); |
| 135 | |
| 136 | if (files.empty()) |
| 137 | { |
| 138 | File(path).setSize(0); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | auto purge_it = files.begin(); |
| 143 | auto it = files.begin(); |
| 144 | for (++it; it != files.end(); ++it) |
| 145 | if (it->getLastModified() < purge_it->getLastModified()) |
| 146 | purge_it = it; |
| 147 | |
| 148 | purge_it->remove(); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | } // namespace Poco |
| 154 |