From: Joel Rosdahl Date: Tue, 10 Aug 2021 18:32:43 +0000 (+0200) Subject: feat: Support subsecond mtime/atime for --cleanup and --trim-dir X-Git-Tag: v4.4~25 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8310c6abde613558dd377ab5c1a70f3ad4635e9d;p=thirdparty%2Fccache.git feat: Support subsecond mtime/atime for --cleanup and --trim-dir --- 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",