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 ++++++++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 39 deletions(-) (limited to 'tools/libltgrey.c') 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'; } } -- cgit v1.2.3