From c4cc2f1913b5b7d79ca2740bad6295a252017dea Mon Sep 17 00:00:00 2001 From: John Denker Date: Mon, 30 Jul 2012 14:35:05 -0700 Subject: start handling two (or more) directories --- tools/libltgrey.c | 75 ++++++++++++++++++++++++++----------------------------- tools/libltgrey.h | 47 ++++++++++++++++++++++++---------- tools/ltgrey.c | 12 ++++----- 3 files changed, 76 insertions(+), 58 deletions(-) diff --git a/tools/libltgrey.c b/tools/libltgrey.c index 4e92665..5b480ac 100644 --- a/tools/libltgrey.c +++ b/tools/libltgrey.c @@ -1,6 +1,6 @@ #include /* for exit(), getenv() */ #include -#include +#include /* for setw */ #include #include /* for stat(), getaddrinfo() */ @@ -22,15 +22,11 @@ #include /* for memset() */ #include /* for inet_ntop() */ -using namespace std; - -const int minute(60); -const int hour(60*minute); -const int day(24*hour); +#include "libltgrey.h" +#include "utils.h" +#include "qq_exit_codes.h" -const int minimum_age(15*minute); -const int maximum_age(32*day); -const int probation(4*hour); +using namespace std; #if 0 void exeunt(const int sts){ @@ -61,18 +57,6 @@ void exeunt(const int sts){ } #endif -#include /* for gettimeofday */ -#include /* for setw */ -#include /* for stat */ -#include /* for stat, creat */ -#include /* for stat, creat */ -#include /* for creat */ -#include /* for ofstream() */ - -#include "libltgrey.h" -#include "utils.h" -#include "qq_exit_codes.h" - // constructor whatsit::whatsit(const std::string name, const std::string _parent_dir) : parent_dir(_parent_dir), progname(name), mypid(getpid()), @@ -80,7 +64,7 @@ whatsit::whatsit(const std::string name, const std::string _parent_dir) { // expand the codes to make some names: # define foo(name) decode_40[name] = #name; - state_40_macro + box_state_macro # undef foo } @@ -182,9 +166,9 @@ int whatsit::doit(const int penalty, const int stain){ return(ex_syserr); } - maybe_mkdir(parent_dir, "parent dir"); - maybe_mkdir(parent_dir + "/quarante", "quarantine dir"); - maybe_mkdir(parent_dir + "/repute", "reputation dir"); +? maybe_mkdir(parent_dir, "parent dir"); +? maybe_mkdir(parent_dir + "/quarante", "quarantine dir"); +? maybe_mkdir(parent_dir + "/repute", "reputation dir"); ipname = dirname + "/" + ipbase; struct stat ipstat; @@ -402,16 +386,23 @@ done: return 0; } -state_40 whatsit::get40(const string mid){ +box_state whatsit::get_40(const string mid){ timeval junk[2]; - return get40(mid, junk); + return get_box(box_40, mid, junk); } -state_40 whatsit::get40(const string mid, timeval times[2]){ +box_state whatsit::get_rep(const string mid){ + timeval junk[2]; + return get_box(box_rep, mid, junk); +} + +box_state whatsit::get_box(const boxer box, + const string mid, timeval times[2]){ timeval now; gettimeofday(&now, NULL); times[0] = times[1] = now; // in case of early return - string fname = parent_dir + "/quarante/mid_" + purify(mid); + string fname = parent_dir + "/" + box.dir + + "/mid_" + purify(mid); //xxx cerr << ".... " << fname << endl; struct stat mid_stat; int rslt = stat(fname.c_str(), &mid_stat); @@ -428,13 +419,18 @@ state_40 whatsit::get40(const string mid, timeval times[2]){ times[upd_mod].tv_usec = 0; int mod_age = now.tv_sec - mid_stat.st_mtime; // int ac_age = now.tv_sec - mid_stat.st_atime; - if (mod_age < minimum_age) return green; - if (mod_age < probation) return ripe; + if (mod_age < box.min) return green; + if (mod_age < box.max) return ripe; return spoiled; } -int whatsit::set40(const string mid, const int shift){ - string fname = parent_dir + "/quarante/mid_" + purify(mid); +int whatsit::set_40(const string mid, const int shift){ + return set_box(box_40, mid, shift); +} + +int whatsit::set_box(const boxer box, const string mid, const int shift){ + string fname = parent_dir + "/" + box.dir + + "/mid_" + purify(mid); //xxx cerr << ".... " << fname << endl; int fd = creat(fname.c_str(), 0644); if (fd < 0){ @@ -471,15 +467,15 @@ int whatsit::set40(const string mid, const int shift){ return 0; } -void whatsit::scan40(const int copies){ +void whatsit::scan_box(const boxer box, const int copies){ timeval now; gettimeofday(&now, NULL); using namespace boost::filesystem; - string dir40 = parent_dir + "/quarante"; + string mydir = parent_dir + "/" + box.dir; - if (is_directory(dir40)) { - for (directory_iterator itr(dir40); itr!=directory_iterator(); ++itr) { + if (is_directory(mydir)) { + for (directory_iterator itr(mydir); itr!=directory_iterator(); ++itr) { string basename = itr->path().filename(); if (is_regular_file(itr->status())) { string tag("mid_"); @@ -488,7 +484,8 @@ void whatsit::scan40(const int copies){ cout << setw(20) << left << basename; // display filename only timeval times[2]; - state_40 rslt = get40(basename.substr(tag.length()), times); + box_state rslt = get_box(box_40, + basename.substr(tag.length()), times); int mod_age = now.tv_sec - times[upd_mod].tv_sec; int ac_age = now.tv_sec - times[upd_ac ].tv_sec; cout << setw(10) << right << time_out(mod_age) @@ -503,6 +500,6 @@ void whatsit::scan40(const int copies){ } else { // starting point is not a directory: - cout << (exists(dir40) ? "Found: " : "Not found: ") << dir40 << '\n'; + cout << (exists(mydir) ? "Found: " : "Not found: ") << mydir << '\n'; } } diff --git a/tools/libltgrey.h b/tools/libltgrey.h index 14ed144..9891256 100644 --- a/tools/libltgrey.h +++ b/tools/libltgrey.h @@ -3,12 +3,7 @@ #include #include -// beware: access illogically comes *before* modification -// in the array passed to utimes: -const int upd_ac(0); -const int upd_mod(1); - -#define state_40_macro \ +#define box_state_macro \ foo(unseen) \ foo(green) \ foo(ripe) \ @@ -18,10 +13,20 @@ foo(fail) // expand the codes to make some names: #define foo(name) name, typedef enum { - state_40_macro -} state_40; + box_state_macro +} box_state; #undef foo +class boxer{ +public: + std::string dir; + int min; + int max; + boxer(const std::string _dir, const int _min, const int _max) + : dir(_dir), min(_min), max(_max) + {} +}; + class whatsit{ public: std::string parent_dir; @@ -30,7 +35,7 @@ public: std::string suffix; std::string progid; int verbosity; - std::map decode_40; + std::map decode_40; whatsit(const std::string name, const std::string _parent_dir); int doit(const int penalty, const int stain); @@ -44,8 +49,24 @@ public: std::vector &checked); void dump(const std::string var); int maybe_mkdir(const std::string somedir, const std::string msg); - state_40 get40(const std::string mid); - state_40 get40(const std::string mid, timeval times[2]); - int set40(const std::string mid, const int shift); - void scan40(const int copies); + + int set_box(const boxer, const std::string mid, const int shift); + box_state get_box(const boxer, const std::string mid, timeval times[2]); + void scan_box(const boxer, const int copies); + + box_state get_40(const std::string mid); + int set_40(const std::string mid, const int shift); + box_state get_rep(const std::string mid); }; + +// beware: access illogically comes *before* modification +// in the array passed to utimes: +const int upd_ac(0); +const int upd_mod(1); + +const int minute(60); +const int hour(60*minute); +const int day(24*hour); + +const boxer box_40 ("quarante", 15*minute, 4*hour); +const boxer box_rep("repute", 0, 35*day); diff --git a/tools/ltgrey.c b/tools/ltgrey.c index 1494338..b707dc2 100644 --- a/tools/ltgrey.c +++ b/tools/ltgrey.c @@ -29,7 +29,7 @@ int main(int _argc, char** _argv){ int shift(0); int stain(0); int dns_mode(0); - string get40_mid; + string get_40_mid; string set40_mid; while (argc > 0) { string arg = argv[0]; argc--; argv++; @@ -46,7 +46,7 @@ int main(int _argc, char** _argv){ cerr << "Option '" << arg << "' requires an argument" << endl; exeunt(ex_syserr); } - get40_mid = *argv++; argc--; + get_40_mid = *argv++; argc--; } else if (prefix(arg, "-set40")) { if (!argc){ cerr << "Option '" << arg << "' requires an argument" << endl; @@ -88,18 +88,18 @@ int main(int _argc, char** _argv){ exeunt(foo.check_dns(ipvar, namevar)); } - if (get40_mid.length()){ - state_40 rslt = foo.get40(get40_mid); + if (get_40_mid.length()){ + box_state rslt = foo.get_40(get_40_mid); cerr << foo.decode_40[rslt] << endl; return 0; } if (set40_mid.length()){ - return foo.set40(set40_mid, shift); + return foo.set_40(set40_mid, shift); } if (scanmode) { - foo.scan40(copies); + foo.scan_box(box_40, copies); return 0; } -- cgit v1.2.3