File indexing completed on 2024-05-19 05:29:20

0001 /*
0002     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003     SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
0004 */
0005 
0006 #include <cassert>
0007 #include <chrono>
0008 #include <filesystem>
0009 #include <iostream>
0010 #include <string_view>
0011 
0012 using namespace std::chrono_literals;
0013 
0014 int main(int argc, char **argv)
0015 {
0016     if (argc != 2) {
0017         std::cerr << "Need cache path as argument\n";
0018         return 1;
0019     }
0020 
0021     const std::filesystem::path cachePath = argv[1];
0022     if (!std::filesystem::exists(cachePath)) {
0023         std::cerr << "Cache path doesn't exist " << cachePath << "\n";
0024         return 1;
0025     }
0026 
0027     for (const auto &path : std::filesystem::directory_iterator(cachePath)) {
0028         if (path.path().extension() != ".ini") {
0029             continue;
0030         }
0031         try {
0032             const auto lastModified = path.last_write_time();
0033             const auto now = decltype(lastModified)::clock().now();
0034             const auto age = now - lastModified;
0035             // Plenty of time so we won't take away the file from underneath drkonqi.
0036             if (age >= std::chrono::weeks(1)) {
0037                 std::filesystem::remove(path);
0038             }
0039         } catch (const std::filesystem::filesystem_error &error) {
0040             std::cerr << error.what();
0041             continue;
0042         }
0043     }
0044 }