diff --git a/CMakeLists.txt b/CMakeLists.txt index b2ca95d..8f5073a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(compilescript - VERSION 0.2.1 + VERSION 0.3.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 1a3d9ba..d0151a6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ scripts. By default it uses g++. It compiles the source file, stores the binary in `${XDG_CACHE_HOME}/compilescript/` and executes it. If the binary in cache is -newer than the source file, the compilation is skipped. +newer than the source file, the compilation is skipped. You can delete old cache +files with `--cleanup`. ## Usage @@ -35,6 +36,7 @@ The configuration file is in `${XDG_CONFIG_HOME}/compilescript.cfg`. ```CFG compiler = "g++"; +clean_after_hours = 720; cache_dir = "/home/user/.cache/compilescript"; ``` diff --git a/src/main.cpp b/src/main.cpp index 0dd3e28..587bea2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -36,9 +38,12 @@ #endif using std::cerr; using std::endl; +using std::chrono::system_clock; +using std::chrono::hours; string compiler = "g++"; fs::path cache_dir; +int clean_after_hours = 30 * 24; void read_settings() { @@ -60,6 +65,17 @@ void read_settings() need_save = true; } + if (cfg.exists("clean_after_hours")) + { + cfg.lookupValue("clean_after_hours", clean_after_hours); + } + else + { + cfg.add("clean_after_hours", + libconfig::Setting::TypeInt) = clean_after_hours; + need_save = true; + } + if (cfg.exists("cache_dir")) { cache_dir = cfg["cache_dir"].c_str(); @@ -83,15 +99,48 @@ void read_settings() } } +void cleanup() +{ + system_clock::time_point now = system_clock::now(); + for (const fs::directory_entry &entry + : fs::recursive_directory_iterator(cache_dir)) + { + if (fs::is_regular_file(entry)) + { + auto diff = now - fs::last_write_time(entry); + if (std::chrono::duration_cast(diff).count() + > clean_after_hours) + { + fs::path current_path = entry.path(); + std::error_code e; + + while (fs::remove(current_path, e)) + { + current_path = current_path.parent_path(); + if (current_path == cache_dir) + { + break; + } + } + } + } + } +} + int main(int argc, char *argv[]) { read_settings(); if (argc <= 1) { - cerr << "usage: " << argv[0] << " file [arguments]\n"; + cerr << "usage: " << argv[0] << " [file|--cleanup] [arguments]\n"; return 1; } + if (string(argv[1]) == "--cleanup") + { + cleanup(); + return 0; + } const fs::path original = fs::canonical(argv[1]); const fs::path source = cache_dir / original;