Added possibility to clean up cache_dir.
the build was successful Details

Fixes #2
This commit is contained in:
tastytea 2018-12-30 11:46:29 +01:00
parent 711accdc8f
commit f4d39e4dfc
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 54 additions and 3 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2)
project(compilescript
VERSION 0.2.1
VERSION 0.3.0
LANGUAGES CXX
)

View File

@ -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";
```

View File

@ -22,6 +22,8 @@
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <system_error>
#include <cstdlib>
#include <unistd.h>
#include <libconfig.h++>
@ -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<hours>(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;