Skip to content
Snippets Groups Projects
Commit 05f819a1 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add a -t (test mode) option, to run in read-only mode

parent 8cf79516
Branches
Tags
No related merge requests found
Pipeline #7034 passed
...@@ -20,14 +20,13 @@ ...@@ -20,14 +20,13 @@
****************************************************************************/ ****************************************************************************/
#include "trash.h" #include "trash.h"
//#include "datewrap.h"
//#include "xpdatetime.h"
//#include "ini_file.h"
//#include "scfglib.h"
#include "findstr.h" #include "findstr.h"
#include "nopen.h" #include "nopen.h"
bool test = false;
int verbosity = 0; int verbosity = 0;
int max_age = 0;
const char* prot = NULL;
int maint(const char* fname) int maint(const char* fname)
{ {
...@@ -37,16 +36,17 @@ int maint(const char* fname) ...@@ -37,16 +36,17 @@ int maint(const char* fname)
perror(fname); perror(fname);
return -1; return -1;
} }
FILE* fp = fnopen(NULL, fname, O_WRONLY|O_TRUNC); FILE* fp = fnopen(NULL, test ? _PATH_DEVNULL : fname, O_WRONLY|O_TRUNC);
if(fp == NULL) { if(fp == NULL) {
perror(fname); perror(test ? _PATH_DEVNULL : fname);
return -2; return -2;
} }
time_t now = time(NULL); time_t now = time(NULL);
for(int i = 0; list[i] != NULL; ++i) { for(int i = 0; list[i] != NULL; ++i) {
struct trash trash; struct trash trash;
char item[256]; char item[256];
if(!trash_parse_details(list[i], &trash, item, sizeof item)) { if(!trash_parse_details(list[i], &trash, item, sizeof item)
|| (prot != NULL && stricmp(trash.prot, prot) != 0)) {
fputs(list[i], fp); fputs(list[i], fp);
continue; continue;
} }
...@@ -60,17 +60,34 @@ int maint(const char* fname) ...@@ -60,17 +60,34 @@ int maint(const char* fname)
++removed; ++removed;
continue; continue;
} }
if(max_age) {
int age = now - trash.added;
if(age > 0 && (age/=(24*60*60)) > max_age) {
if(verbosity > 0)
printf("%s is %d days old", item, age);
++removed;
continue;
}
}
fputs(list[i], fp); fputs(list[i], fp);
} }
fclose(fp); fclose(fp);
strListFree(&list); strListFree(&list);
if(removed || verbosity > 0)
printf("%d items %sremoved from %s\n"
,removed, test ? "would have been " : "", fname);
return removed; return removed;
} }
int usage(const char* prog) int usage(const char* prog)
{ {
printf("usage: %s [-v][...] /path/to/file1.can [/path/to/file2.can][...]\n" printf("\nusage: %s [-opt][...] /path/to/file1.can [/path/to/file2.can][...]\n"
,getfname(prog)); ,getfname(prog));
printf("\noptions:\n");
printf(" -a<days> specify maximum age of filter items, in days\n");
printf(" -p<prot> only manage filters for specified protocol\n");
printf(" -t run in test (read-only) mode\n");
printf(" -v increase verbosity of output\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -84,9 +101,21 @@ int main(int argc, const char** argv) ...@@ -84,9 +101,21 @@ int main(int argc, const char** argv)
const char* arg = argv[i]; const char* arg = argv[i];
if(*arg != '-') if(*arg != '-')
continue; continue;
switch(arg[1]) { ++arg;
switch(*arg) {
case 't':
test = true;
break;
case 'v': case 'v':
++verbosity; do {
++verbosity;
} while(*(++arg) == 'v');
break;
case 'a':
max_age = atoi(++arg);
break;
case 'p':
prot = ++arg;
break; break;
default: default:
return usage(argv[0]); return usage(argv[0]);
...@@ -102,6 +131,6 @@ int main(int argc, const char** argv) ...@@ -102,6 +131,6 @@ int main(int argc, const char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
total += removed; total += removed;
} }
printf("%d total items removed\n", total); printf("%d total items %sremoved\n", total, test ? "would have been " : "");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment