Cleanup
[backups/.git] / main.cpp
index 945cc0d0b36c91de96147dddb469355a87e89d75..2323efe3295da8031ab88a111b590f38a8a6787d 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 using namespace std;
 
 static const unsigned int bytes_in_block = 0x800;
-static const char * dbname = "/var/tmp/backups.db";
-
-unsigned long long current_time() {
-  unsigned long long rc = 0;
-  time_t now_tt = time( 0 );
-  tm *now = localtime( &now_tt );
-  rc += ( now->tm_year + 1900ULL ) * 10000000000ULL;
-  rc += ( now->tm_mon  + 1ULL )    * 100000000ULL;
-  rc +=   now->tm_mday             * 1000000ULL;
-  rc +=   now->tm_hour             * 10000ULL;
-  rc +=   now->tm_min              * 100ULL;
-  rc +=   now->tm_sec;
-
-  return rc;
+static const char * dbname = "/var/lib/backups/backups.db";
+
+unsigned long long read_time( istream &i ) {
+  string date_string;
+
+  int c;
+  // Todo, don't use char_traits<char> directly here
+  for( c = i.get(); 0 != c && char_traits<char>::eof() != c; c = i.get() ) {
+    date_string.push_back( c );
+  }
+  if( char_traits<char>::eof() == c ) { i.setstate( ios_base::eofbit ); }
+
+  return atoll( date_string.c_str() );
 }
 
 template<class I, class O, class INT>
 bool copy_until_full( I begin, I end, O out, INT &space ) {
   bool complete = true;
 
-  I i = begin;
-  while( 0 != space && i != end ) {
-    INT size = (*i)->getFileSize();
+  while( 0 != space && begin != end ) {
+    INT size = (*begin)->getFileSize();
     INT blocksize = blocks( size ) * bytes_in_block;
 
     if( blocksize <= space ) {
       space -= blocksize;
-      out = *i;
+      out = *begin;
       ++out;
     } else {
       // We missed a file that should be included so the backup is not complete
       complete = false;
     }
-    ++i;
+    ++begin;
   }
   return complete;
 }
@@ -105,11 +103,41 @@ void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
 
     numbytes  += filesize;
     numblocks += blocks( filesize );
-    begin++;
+    ++begin;
+  }
+}
+
+template<class I, class O>
+void copy_filenames( I begin, const I &end, O out ) {
+  while( begin != end ) {
+    string output = (*begin)->getFileName();
+    output.push_back( 0 );
+    *out = output;
+    ++out;
+    ++begin;
+  }
+}
+
+template<class ITER>
+void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
+  while( begin != end ) {
+    (*begin)->setLastBackupDate( date );
+    ++begin;
+  }
+}
+
+template<class ITER>
+void delete_objects( ITER begin, const ITER &end ) {
+  while( begin != end ) {
+    delete *begin;
+    ++begin;
   }
 }
 
 int main() {
+  // Get the date on stdin
+  unsigned long long now = read_time( cin );
+
   // Parse the list of current files on stdin
   file_set current;
   populate_set( cin, current );
@@ -133,7 +161,7 @@ int main() {
 
   // Track the total size of added files
   unsigned long long added_blocks, added_bytes;
-  sizes( backups.begin(), backups.end(), added_blocks, added_bytes );
+  sizes( added.begin(), added.end(), added_blocks, added_bytes );
 
   file_vector modified_files;
   // Backup files that have been modified
@@ -163,10 +191,8 @@ int main() {
   insert_iterator<file_set> final_i( final, final.begin() );
 
   // Copy files over until full or out of files
-  bool complete = copy_until_full( backups_s.rbegin(),
-                                   backups_s.rend(),
-                                   final_i,
-                                   space );
+  bool complete
+    = copy_until_full( backups_s.rbegin(), backups_s.rend(), final_i, space );
 
   // Track the size filled up by essential backups
   unsigned long long essential_blocks, essential_bytes;
@@ -191,17 +217,16 @@ int main() {
   unsigned long long total_blocks, total_bytes;
   sizes( final.begin(), final.end(), total_blocks, total_bytes );
 
-  unsigned long long now = current_time();
-  for( file_set::iterator k = final.begin(); k != final.end(); ++k ) {
-    (*k)->setLastBackupDate( now );
-  }
+  updateLastBackupDate( final.begin(), final.end(), now );
 
   // Write the 'current' list to the dbfile
   ofstream dbout( dbname );
-  copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout, "" ) );
+  copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
 
   // Write the 'final' list to stdout
-  copy( final.begin(), final.end(), ostream_iterator<FileData*>( cout, "" ) );
+  copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
+
+  cerr << now << endl << endl;
 
   cerr << "Need backing up..." << endl;
   cerr << "    Added Bytes:            " << added_bytes << endl;
@@ -218,6 +243,6 @@ int main() {
   if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
 
   // Clean-up
-  for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }
-  for( file_set::iterator i = current.begin();   i != current.end();   ++i ) { delete *i; }
+  delete_objects( backed_up.begin(), backed_up.end() );
+  delete_objects( current.begin(),   current.end() );
 }