]> www.vanbest.org Git - backupusage.git/commitdiff
Refactoring: move work to spacecounter class
authorJan-Pascal van Best <janpascal@vanbest.org>
Mon, 29 Aug 2011 08:07:09 +0000 (10:07 +0200)
committerJan-Pascal van Best <janpascal@vanbest.org>
Mon, 29 Aug 2011 08:07:09 +0000 (10:07 +0200)
.gitignore
Makefile
backupusage.cc
spacecounter.cc [new file with mode: 0644]
spacecounter.hh [new file with mode: 0644]

index 6dd10227f3b65476ad403d8959937f230a55e7d3..34b410188e4dbc112f5ed1cae6957745d0051db4 100644 (file)
@@ -1 +1,2 @@
 backupusage
+*.o
index b8c673dedcaa34aad2a0da0210ccd9443f8e65e6..44df599646f7b77f0a3a830ec6db48299485c7d0 100644 (file)
--- 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
index 2143ce07af559eaa10d0a602374a822d839c09e0..fd6844a22df474d3e72ae3eb1b3c69a63aa18b2b 100644 (file)
@@ -1,13 +1,18 @@
+#include <string>
+#include <vector>
 #include <set>
 #include <map>
-#include <string>
 #include <iostream>
 #include <sys/stat.h>
 #include <boost/filesystem.hpp>
-#include <boost/algorithm/string/regex.hpp>
 #include <boost/regex.hpp>
 #include <boost/format.hpp>
 #include <boost/program_options.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/algorithm/string.hpp>
+
+#include "spacecounter.hh"
 
 using namespace std;
 using namespace boost::filesystem;
@@ -15,14 +20,10 @@ using namespace boost::algorithm;
 
 namespace po = boost::program_options;
 
-set<ino_t> inodes;
-map<path,long> dirs;
+//set<ino_t> inodes;
+//map<path,long> 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<string>(&start_dir)->default_value("/var/lib/backuppc/pc/nogrod"), "Starting directory")
-      ("prefix,p", po::value<string>(&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<string>(&base_dir)->default_value("/var/lib/backuppc/pc/"), "Base directory for BackupPC backup hardlinks")
+      ("pc,p", po::value<string>(&pc_list)->default_value("all"), "Comma-separated list of BackupPC hosts ('all': all hosts)")
+//      ("start-dir,s", po::value<string>(&start_dir)->default_value("/var/lib/backuppc/pc/nogrod"), "Starting directory")
+      ("prefix,p", po::value<string>(&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<long>(&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<string> pc_list_split;
+  split( pc_list_split, pc_list, boost::is_any_of(","), token_compress_on );
 
-  map<long,path> result;
-  for( map<path,long>::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<string>::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<long,path> result = counter.get_result( size_limit );
+
   for( map<long,path>::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 (file)
index 0000000..cec15b9
--- /dev/null
@@ -0,0 +1,67 @@
+#include <string>
+#include <vector>
+#include <set>
+#include <map>
+#include <iostream>
+#include <sys/stat.h>
+#include <boost/filesystem.hpp>
+#include <boost/algorithm/string/regex.hpp>
+#include <boost/regex.hpp>
+#include <boost/format.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/algorithm/string.hpp>
+
+#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<long,path> spacecounter::get_result( long size_limit ) {
+  map<long,path> result;
+  for( map<path,long>::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 (file)
index 0000000..05224e0
--- /dev/null
@@ -0,0 +1,22 @@
+#include <string>
+#include <set>
+#include <map>
+#include <boost/filesystem.hpp>
+#include <boost/regex.hpp>
+#include <sys/stat.h>
+
+class spacecounter {
+  private:
+    std::set<ino_t> inodes;
+    std::map<boost::filesystem::path,long> 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<long,boost::filesystem::path> get_result( long minimum_size );
+}; // class spacecounter