From 8310c6abde613558dd377ab5c1a70f3ad4635e9d Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 10 Aug 2021 20:32:43 +0200 Subject: [PATCH] feat: Support subsecond mtime/atime for --cleanup and --trim-dir --- src/core/mainoptions.cpp | 10 +++++----- src/storage/primary/PrimaryStorage_cleanup.cpp | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 2c301d970..a5ccf6eeb 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -218,11 +218,11 @@ trim_dir(const std::string& dir, }); std::sort(files.begin(), files.end(), [&](const auto& f1, const auto& f2) { - if (trim_lru_mtime) { - return f1.stat.mtime() < f2.stat.mtime(); - } else { - return f1.stat.atime() < f2.stat.atime(); - } + const auto ts_1 = trim_lru_mtime ? f1.stat.mtim() : f1.stat.atim(); + const auto ts_2 = trim_lru_mtime ? f2.stat.mtim() : f2.stat.atim(); + const auto ns_1 = 1'000'000'000ULL * ts_1.tv_sec + ts_1.tv_nsec; + const auto ns_2 = 1'000'000'000ULL * ts_2.tv_sec + ts_2.tv_nsec; + return ns_1 < ns_2; }); uint64_t size_after = size_before; diff --git a/src/storage/primary/PrimaryStorage_cleanup.cpp b/src/storage/primary/PrimaryStorage_cleanup.cpp index fd89cf035..6f3790f9c 100644 --- a/src/storage/primary/PrimaryStorage_cleanup.cpp +++ b/src/storage/primary/PrimaryStorage_cleanup.cpp @@ -126,7 +126,11 @@ PrimaryStorage::clean_dir(const std::string& subdir, // Sort according to modification time, oldest first. std::sort(files.begin(), files.end(), [](const auto& f1, const auto& f2) { - return f1.lstat().mtime() < f2.lstat().mtime(); + const auto ts_1 = f1.lstat().mtim(); + const auto ts_2 = f2.lstat().mtim(); + const auto ns_1 = 1'000'000'000ULL * ts_1.tv_sec + ts_1.tv_nsec; + const auto ns_2 = 1'000'000'000ULL * ts_2.tv_sec + ts_2.tv_nsec; + return ns_1 < ns_2; }); LOG("Before cleanup: {:.0f} KiB, {:.0f} files", -- 2.47.2