From: Jan-Pascal van Best Date: Mon, 29 Aug 2011 08:07:09 +0000 (+0200) Subject: Refactoring: move work to spacecounter class X-Git-Tag: 0.1~2 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=f9334c8a697b5f939c062e2f8ee3d8dc5fa6d989;p=backupusage.git Refactoring: move work to spacecounter class --- diff --git a/.gitignore b/.gitignore index 6dd1022..34b4101 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ backupusage +*.o diff --git a/Makefile b/Makefile index b8c673d..44df599 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ # libboost-filesystem.dev # libboost-regex-dev # libboost-program-options-dev +# ... and maybe more, see #includes LIBS=-lboost_program_options -lboost_filesystem -lboost_regex @@ -10,5 +11,16 @@ all: backupusage test: backupusage ./backupusage --limit=1000000 > result.txt -backupusage: backupusage.cc +backupusage: backupusage.o spacecounter.o g++ $(LIBS) $^ -o $@ + +backupusage.o: backupusage.cc + g++ -c $^ -o $@ + +spacecounter.o: spacecounter.cc + g++ -c $^ -o $@ + +clean: + rm -f *.o backupusage + +# vim:noexpandtab diff --git a/backupusage.cc b/backupusage.cc index 2143ce0..fd6844a 100644 --- a/backupusage.cc +++ b/backupusage.cc @@ -1,13 +1,18 @@ +#include +#include #include #include -#include #include #include #include -#include #include #include #include +#include +#include +#include + +#include "spacecounter.hh" using namespace std; using namespace boost::filesystem; @@ -15,14 +20,10 @@ using namespace boost::algorithm; namespace po = boost::program_options; -set inodes; -map dirs; +//set inodes; +//map dirs; -boost::regex prefix_re; - -path strip_path( const path& dir ) { - return erase_regex_copy(dir.string(), prefix_re ); -} +// boost::regex prefix_re; string ToHumanReadable(long x) { @@ -46,50 +47,22 @@ string ToHumanReadable(long x) return (boost::format("%6.1fGB") % (x / GB)).str(); } } - - -void handle_file( const path& dir, const path& p ) { - struct stat buf; - if ( lstat(p.string().c_str(), &buf )) { - cout << "error" << endl; - } else { - // cout << "inode: " << buf.st_ino << "; size: " << buf.st_size << endl; - if (inodes.find( buf.st_ino ) == inodes.end() ) { - inodes.insert( buf.st_ino ); - dirs[strip_path(dir)] += buf.st_size; - // cout << "New inode, size of " << strip_path(dir) << " is now " << dirs[strip_path(dir)] << endl; - } else { - // cout << "Old inode" << endl; - } - } -} - -void handle_dir( const path& dir ) { - // cout << dir << endl; - if ( dirs.find(strip_path(dir)) == dirs.end() ) { - dirs[strip_path(dir)] = 0; - } - directory_iterator i(dir), dir_end; - for(;i!= dir_end; ++i) { - if ( is_directory( *i ) ) { - handle_dir( *i ); - } else { - handle_file( dir, *i ); - } - } -} - + int main( int argc, char** argv ) { - string start_dir; + // string start_dir; + string base_dir; string prefix; + string pc_list; long size_limit; // Declare the supported options. po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") - ("start-dir,s", po::value(&start_dir)->default_value("/var/lib/backuppc/pc/nogrod"), "Starting directory") - ("prefix,p", po::value(&prefix)->default_value("/var/lib/backuppc/pc/nogrod/[0-9]*/(f%2f|f)"), "Prefix to remove for counting (regular expression)") + ("base-dir,d", po::value(&base_dir)->default_value("/var/lib/backuppc/pc/"), "Base directory for BackupPC backup hardlinks") + ("pc,p", po::value(&pc_list)->default_value("all"), "Comma-separated list of BackupPC hosts ('all': all hosts)") +// ("start-dir,s", po::value(&start_dir)->default_value("/var/lib/backuppc/pc/nogrod"), "Starting directory") + ("prefix,p", po::value(&prefix)->default_value("/var/lib/backuppc/pc/[0-9a-zA-Z_]*/[0-9]*/(f%2f|f)"), "Prefix to remove for counting (regular expression)") ("limit,l", po::value(&size_limit)->default_value(1), "Minimal directory size to show") ; @@ -102,15 +75,19 @@ int main( int argc, char** argv ) { return 1; } - prefix_re = boost::regex(prefix); - - handle_dir( start_dir ); +// prefix_re = boost::regex(prefix); + vector pc_list_split; + split( pc_list_split, pc_list, boost::is_any_of(","), token_compress_on ); - map result; - for( map::const_iterator i = dirs.begin(); i != dirs.end(); ++i ) { - if ( i->second >= size_limit ) result[i->second] = i->first; + spacecounter counter = spacecounter(boost::regex(prefix)); + for (vector::const_iterator i = pc_list_split.begin(); i != pc_list_split.end(); ++i ) { + cout << "Examining PC " << *i << endl; + counter.handle_dir( path(base_dir) / *i ); } + + map result = counter.get_result( size_limit ); + for( map::const_iterator i = result.begin(); i != result.end(); ++i ) { cout << ToHumanReadable(i->first) << " (" << i->first << ") " << i->second << endl; } diff --git a/spacecounter.cc b/spacecounter.cc new file mode 100644 index 0000000..cec15b9 --- /dev/null +++ b/spacecounter.cc @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "spacecounter.hh" + +using namespace std; +using namespace boost::filesystem; +using namespace boost::algorithm; + +spacecounter::spacecounter( boost::regex a_prefix_re ) { + prefix_re = a_prefix_re; +} + +path spacecounter::strip_path( const path& dir ) { + return erase_regex_copy(dir.string(), prefix_re ); +} + +void spacecounter::handle_file( const path& dir, const path& p ) { + struct stat buf; + if ( lstat(p.string().c_str(), &buf )) { + cout << "error" << endl; + } else { + // cout << "inode: " << buf.st_ino << "; size: " << buf.st_size << endl; + if (inodes.find( buf.st_ino ) == inodes.end() ) { + inodes.insert( buf.st_ino ); + dirs[strip_path(dir)] += buf.st_size; + // cout << "New inode, size of " << strip_path(dir) << " is now " << dirs[strip_path(dir)] << endl; + } else { + // cout << "Old inode" << endl; + } + } +} + +void spacecounter::handle_dir( const path& dir ) { + cout << dir << endl; + if ( dirs.find(strip_path(dir)) == dirs.end() ) { + dirs[strip_path(dir)] = 0; + } + directory_iterator i(dir), dir_end; + for(;i!= dir_end; ++i) { + if ( is_directory( *i ) ) { + handle_dir( *i ); + } else { + handle_file( dir, *i ); + } + } +} + +map spacecounter::get_result( long size_limit ) { + map result; + for( map::const_iterator i = dirs.begin(); i != dirs.end(); ++i ) { + if ( i->second >= size_limit ) result[i->second] = i->first; + } + return result; +} + diff --git a/spacecounter.hh b/spacecounter.hh new file mode 100644 index 0000000..05224e0 --- /dev/null +++ b/spacecounter.hh @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include +#include + +class spacecounter { + private: + std::set inodes; + std::map dirs; + boost::regex prefix_re; + + public: + spacecounter( boost::regex prefix_re ); + protected: + boost::filesystem::path strip_path( const boost::filesystem::path& dir ); + void handle_file( const boost::filesystem::path& dir, const boost::filesystem::path& p ); + public: + void handle_dir( const boost::filesystem::path& dir ); + std::map get_result( long minimum_size ); +}; // class spacecounter