aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Kavanagh <rak@rak.ac>2020-09-11 11:49:11 -0400
committerRyan Kavanagh <rak@rak.ac>2020-09-11 11:49:11 -0400
commitf065cbde7ec718abacee989efb5e5d6372dcfd50 (patch)
treea931a7deea7392a5c328d78ca44c108654749a01
parenttemporary file host & cleanup (diff)
Better error handling, and a verbose option
-rw-r--r--cleanup.c58
1 files changed, 39 insertions, 19 deletions
diff --git a/cleanup.c b/cleanup.c
index da9268e..429c8c2 100644
--- a/cleanup.c
+++ b/cleanup.c
@@ -22,6 +22,7 @@
#include <fts.h>
#include <err.h>
+#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
@@ -30,7 +31,7 @@
const double mind = 30.0;
const double maxd = 90.0;
const double max_size = 33554432; // 32MB
-char* paths[1] = {"/var/www/users/tmp"};
+char* paths[2] = {"/var/www/users/~tmp", NULL};
int
main(int argc, char * const argv[])
@@ -43,30 +44,49 @@ main(int argc, char * const argv[])
double file_age = 0;
time_t curr_time = time(NULL);
- file_system = fts_open(paths, 0, NULL);
+ int verbose, ch;
- if (file_system != NULL) {
- while ((node = fts_read(file_system)) != NULL) {
- switch (node->fts_info) {
- 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) * \
- pow((double) attr.st_size / (double) max_size - 1, 3.0);
+ verbose = 0;
+ while ((ch = getopt(argc, argv, "v")) != -1) {
+ switch (ch) {
+ case 'v':
+ verbose = 1;
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if ((file_system = fts_open(paths, 0, NULL)) == NULL)
+ errx(1, NULL);
- if (file_age >= cutoff) {
- // printf("%s age %fs cuttoff %fs size %lld B delete\n", \
- // node->fts_name, file_age, cutoff, attr.st_size);
- unlink(node->fts_name);
- }
- break;
- default:
- break;
+ while ((node = fts_read(file_system)) != NULL) {
+ if (node->fts_errno)
+ err(node->fts_errno, "%s", node->fts_path);
+
+ switch (node->fts_info) {
+ 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);
+ if (verbose)
+ printf("%s age %fs cuttoff %fs size %lldB\n",
+ node->fts_name, file_age, cutoff, attr.st_size);
+ if (file_age >= cutoff) {
+ unlink(node->fts_name);
+ if (verbose)
+ printf("%s deleted\n", node->fts_name);
}
+ break;
+ default:
+ break;
}
}
+ fts_close(file_system);
+
return 0;
}