aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Kavanagh <rak@rak.ac>2020-09-11 12:04:51 -0400
committerRyan Kavanagh <rak@rak.ac>2020-09-11 12:04:51 -0400
commit539836ef78a77b7741f04d43b38eae667b62fead (patch)
tree1bac1cb52f40afb01a25937f7966cb2ce914ebaf
parentptmp: handle files in other directories (diff)
Add a dryrun flag and treat times as days instead of seconds
-rw-r--r--cleanup.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/cleanup.c b/cleanup.c
index 429c8c2..3dd6e36 100644
--- a/cleanup.c
+++ b/cleanup.c
@@ -44,14 +44,18 @@ main(int argc, char * const argv[])
double file_age = 0;
time_t curr_time = time(NULL);
- int verbose, ch;
+ int verbose, dryrun, ch;
verbose = 0;
- while ((ch = getopt(argc, argv, "v")) != -1) {
+ dryrun = 0;
+ while ((ch = getopt(argc, argv, "vn")) != -1) {
switch (ch) {
case 'v':
verbose = 1;
break;
+ case 'n':
+ dryrun = 1;
+ break;
}
}
argc -= optind;
@@ -68,14 +72,15 @@ main(int argc, char * const argv[])
case FTS_F:
if (stat(node->fts_name, &attr) == -1)
err(1, NULL);
- file_age = difftime(curr_time, attr.st_mtime);
- cutoff = mind + (mind - maxd) * 86400 *
- pow((double) attr.st_size / (double) max_size - 1, 3.0);
+ file_age = difftime(curr_time, attr.st_mtime) / 86400.0;
+ cutoff = mind + (mind - maxd) *
+ pow((double) attr.st_size / (double) max_size - 1, 3.0);
if (verbose)
- printf("%s age %fs cuttoff %fs size %lldB\n",
+ printf("%s age %fd cuttoff %fd size %lldB\n",
node->fts_name, file_age, cutoff, attr.st_size);
if (file_age >= cutoff) {
- unlink(node->fts_name);
+ if (!dryrun)
+ unlink(node->fts_name);
if (verbose)
printf("%s deleted\n", node->fts_name);
}