Reorganize a bit
[backups/.git] / main.cc
diff --git a/main.cc b/main.cc
index 9224f00747b7318796c13829ad0504751d79e33e..079252e1ce51d62d24382a8859e33f516f323bfd 100644 (file)
--- a/main.cc
+++ b/main.cc
 #include <iostream>
+#include <fstream>
 #include <iterator>
-#include <vector>
 #include <algorithm>
-
-#include <sqlite3.h>
+#include <cassert>
 
 #include "filedata.hpp"
 
 using namespace std;
 
-vector<string> split( const string &line, char c, int limit = -1 ) {
-  string::size_type start = 0, end = 0;
-
-  vector<string> out;
-  while( 0 != limit-- && end != line.size() ) {
-    if( 0 == limit ) {
-      end = line.size();
-    } else {
-      end = line.find( c, start );
-      if( end == string::npos ) {
-        end = line.size();
-      }
-    }
-    out.push_back( line.substr( start, end-start ) );
-    start = end + 1;
-  }
-  return out;
-}
-
-// Callback function for getting files from the database
-int populate_set( void *files_v, int, char **vals, char ** ) {
-  file_set *files = reinterpret_cast<file_set*>( files_v );
-  files->insert( new FileData( vals[0][0],
-        vals[1],
-        vals[2],
-        vals[3],
-        atoi( vals[4] ),
-        atoi( vals[5] ),
-        vals[6]) );
-  return 0;
-}
-
-int main() {
-  string file_string;
-
-  file_set current;
-
-  // Parse the list of files on stdin
+template<class ISTREAM, class SET>
+void populate_set( ISTREAM &in, SET &files ) {
   do {
-    file_string.clear();
-    for( int c = cin.get(); 0 != c && ! cin.eof(); c = cin.get() ) {
-      file_string.push_back( c );
-    }
-    if( 0 != file_string.size() ) {
-      // Example entry
-      // type perms user group size datemodified name (7 total)
-      // f 0600 cnb cnb 424 20051015205340 ./.git/index
-      vector<string> vals = split( file_string, ' ', 7 );
-      current.insert( new FileData( vals[0][0],
-            vals[1],
-            vals[2],
-            vals[3],
-            atoi( vals[4].c_str() ),
-            atoi( vals[5].c_str() ),
-            vals[6]) );
-    }
-  } while( ! cin.eof() );
-
-  // Get the list of previously backed up files from the database.
-  sqlite3 *db;
-
-  const char *dbname = "test.db";
-  int rc = sqlite3_open( dbname, &db );
-  assert( SQLITE_OK == rc );
-
-  char *sqliteErrMsg = 0;
-  file_set backed_up;
-  rc = sqlite3_exec( db, "select * from filedata;", populate_set, &backed_up, &sqliteErrMsg );
-  assert( SQLITE_OK == rc );
-
-  rc = sqlite3_close( db );
-  assert( SQLITE_OK == rc );
+    FileData *data = new FileData();
+    in >> (*data);
+    files.insert( data );
+  } while( ! in.eof() );
+}
 
-  // Now divide the two sets into three sets (new, deleted and updated )
-  FileDataPtrCmp cmp;
+template<class SET>
+void partition_sets( const SET &current, const SET &old,
+                     SET &added, SET &common, SET &deleted  ) {
+  FileDataNameCmp cmp;
 
-  file_set new_set;
-  set_difference( current.begin(),  current.end(),
-                  backed_up.begin(), backed_up.end(),
-                  inserter( new_set, new_set.begin() ),
+  set_difference( current.begin(), current.end(),
+                  old.begin(),     old.end(),
+                  inserter( added, added.begin() ),
                   cmp );
 
-  file_set deleted;
-  set_difference( backed_up.begin(), backed_up.end(),
-                  current.begin(),  current.end(),
+  set_difference( old.begin(),     old.end(),
+                  current.begin(), current.end(),
                   inserter( deleted, deleted.begin() ),
                   cmp );
 
-  // backed_up should *definitely* be the first set here
-  file_set updated;
-  set_difference( backed_up.begin(), backed_up.end(),
-                  current.begin(),  current.end(),
-                  inserter( updated, updated.begin() ),
+  set_union(      current.begin(), current.end(),
+                  old.begin(),     old.end(),
+                  inserter( common, common.begin() ),
                   cmp );
+}
+
+int main() {
+  // Parse the list of current files on stdin
+  file_set current;
+  populate_set( cin, current );
+
+  file_set backed_up;
+  ifstream db( "test.db" );
+  populate_set( db, backed_up);
+
+  // Now divide the two sets into three sets (added, deleted and common )
+  file_set added, deleted, common;
+  partition_sets( current, backed_up, added, common, deleted );
+
+  { // This little block will copy the last_backup_date from the second set to the first
+    FileDataNameCmp cmp;
+
+    file_set updated_mirror;
+    set_union( current.begin(),   current.end(),
+               backed_up.begin(), backed_up.end(),
+               inserter( updated_mirror, updated_mirror.begin() ),
+               cmp );
+
+    // TODO Now we need to copy the last_backup_date from 
+    file_set::iterator i = common.begin(), j = updated_mirror.begin();
+    for( ; i != common.end(); ++i, ++j ) {
+      (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
+    }
+  }
 
   // Now find the list of files to backup.
-  file_set backup_list;
+  file_set backup_set;
 
-  // backup all new files
-  copy( new_set.begin(), new_set.end(), inserter( backup_list, backup_list.begin() ) );
+  // backup all added files
+  copy( added.begin(), added.end(), inserter( backup_set, backup_set.begin() ) );
 
-  // backup already backed-up files that have changed since the last backup date.
-  for( file_set::iterator i = updated.begin(); i != updated.end(); ++i ) {
+  // backup common files that have changed since the last backup date.
+  for( file_set::iterator i = common.begin(); i != common.end(); ++i ) {
     if( (*i)->getLastBackupDate() < (*i)->getModifiedDate() ) {
-      backup_list.insert( *i );
+      backup_set.insert( *i );
     }
   }
 
-  // Now, sort the backup_list by filesize and build a list of up to SIZE
+  // Now, sort the backup_set by filesize and build a list of up to SIZE
+  file_vector backups_bysize;
+  copy( backup_set.begin(), backup_set.end(), back_inserter( backups_bysize ) );
+  FileDataSizeCmp sizecmp;
+  sort( backups_bysize.begin(), backups_bysize.end(), sizecmp );
 
-  // Now, sort the non-backed-up list my last_backup_date and back-fill
+  file_set final_set;
+  unsigned long long bytes_available = 4700000000ULL;  // 4.3 GBytes
 
-  // Remove deleted files from the database.
-  const char *delete_sql = "delete from filedata where filename = :filename";
-  sqlite3_stmt *ppStmt;
+  unsigned long long block_size = 512ULL;
 
-  sqlite3_prepare( db, delete_sql, -1, &ppStmt, NULL );
-  assert( NULL != ppStmt );
+  bool complete = true;
 
-  for( file_set::iterator i = deleted.begin(); i != deleted.end(); ++i ) {
-    const string &name = (*i)->getFileName();
+  // Copy files over until full or out of files
+  file_vector::reverse_iterator i = backups_bysize.rbegin();
+  while( 0 != bytes_available && i != backups_bysize.rend() ) {
+    unsigned long long size = (*i)->getFileSize();
+    unsigned long long blocks = size & ( ~(block_size-1) );
+    if( blocks < size ) blocks += block_size;
+    if( blocks <= bytes_available ) {
+      bytes_available -= blocks;
+      final_set.insert( *i );
+    } else {
+      // We missed a file that should be included so the backup is not complete
+      complete = false;
+    }
+    ++i;
+  }
 
-    rc = sqlite3_bind_text( ppStmt, 1, name.data(), name.size(), SQLITE_TRANSIENT );
-    assert( SQLITE_OK == rc );
+  // Now, sort the non-backed-up list by last_backup_date and back-fill
+  if( 0 != bytes_available ) {
+    file_vector leftovers;
+    FileDataNameCmp cmp;
+    set_difference( current.begin(),   current.end(),
+                    final_set.begin(), final_set.end(),
+                    back_inserter( leftovers ),
+                    cmp );
+
+    FileDataLastBackupCmp lastbackupcmp;
+    sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
+
+    // Copy files over until full or out of files
+    file_vector::const_iterator j = leftovers.begin();
+    while( 0 != bytes_available && j != leftovers.end() ) {
+      unsigned long long size   = (*j)->getFileSize();
+      unsigned long long blocks = size & ( ~(block_size-1) );
+
+      if( blocks < size ) blocks += block_size;
+
+      if( blocks <= bytes_available ) {
+        bytes_available -= blocks;
+        final_set.insert( *j );
+      }
+      ++j;
+    }
+  }
 
-    rc = sqlite3_step( ppStmt );
-    assert( SQLITE_OK == rc );
+  // TODO Get 'now' from time clock
+  unsigned long long now = 20051019211200ULL;
+  for( file_set::iterator k = final_set.begin(); k != final_set.end(); ++k ) {
+    (*k)->setLastBackupDate( now );
   }
-  rc = sqlite3_finalize( ppStmt );
-  assert( SQLITE_OK == rc );
 
-  // Now, use the current set to update values in the database.  You should copy
-  // last_backup_date dates from the backed_up set first.  This will get all of
-  // the latest permissions but include the last_backup_date.
+  // Write the 'current' list to the dbfile
+  ofstream dbout( "test.db" );
+  copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout, "" ) );
 
-  // Now, update the last_backup_date for all of the files that are in the list
+  // Write the 'final_set' list to stdout
+  copy( final_set.begin(), final_set.end(), ostream_iterator<FileData*>( cout, "" ) );
+
+  // If ! complete then write a flag to /tmp
+  if( ! complete ) {
+    cerr << "incomplete" << endl;
+  }
 
   // Clean-up
   for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }