CommonLib
Loading...
Searching...
No Matches
FileSystem.inl
Go to the documentation of this file.
1#include <CommonLib.h>
2
3namespace hedgedev::csl::ut::filesystem
4{
5 inline bool IsNeighbour(const std::filesystem::path& in_rPathA, const std::filesystem::path& in_rPathB)
6 {
7 return string::Compare(in_rPathA.parent_path().c_str(), in_rPathB.parent_path().c_str(), false);
8 }
9
10 template <typename T>
11 inline std::error_code TruncateFiles(const std::filesystem::path& in_rPath, std::string_view in_extension, size_t in_max, T&& in_rrCompare)
12 {
13 std::error_code result{};
14
15 if (!std::filesystem::exists(in_rPath, result) || !std::filesystem::is_directory(in_rPath, result))
16 return result;
17
18 std::vector<std::filesystem::directory_entry> files{};
19
20 for (const auto& entry : std::filesystem::directory_iterator(in_rPath))
21 {
22 if (!entry.is_regular_file(result))
23 continue;
24
25 if (entry.path().extension() != in_extension)
26 continue;
27
28 files.push_back(entry);
29 }
30
31 if (files.size() <= in_max)
32 return result;
33
34 std::sort(files.begin(), files.end(), in_rrCompare);
35
36 auto truncateLength = files.size() - in_max;
37
38 for (size_t i = 0; i < truncateLength; i++)
39 std::filesystem::remove(files[i].path(), result);
40
41 return result;
42 }
43
44 inline std::error_code TruncateFilesByAge(const std::filesystem::path& in_rPath, std::string_view in_extension, size_t in_max)
45 {
46 std::error_code result{};
47
48 // Sort files by oldest to newest.
49 return TruncateFiles(in_rPath, in_extension, in_max, [&](const auto& a, const auto& b)
50 {
51 return std::filesystem::last_write_time(a, result) < std::filesystem::last_write_time(b, result);
52 });
53 }
54}
Definition FileSystem.inl:4
std::error_code TruncateFilesByAge(const std::filesystem::path &in_rPath, std::string_view in_extension, size_t in_max)
Definition FileSystem.inl:44
std::error_code TruncateFiles(const std::filesystem::path &in_rPath, std::string_view in_extension, size_t in_max, T &&in_rrCompare)
Definition FileSystem.inl:11
bool IsNeighbour(const std::filesystem::path &in_rPathA, const std::filesystem::path &in_rPathB)
Definition FileSystem.inl:5
Definition String.h:8