- // Parse the list of files on stdin
- 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_files.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() );
+ file_set backed_up;
+ ifstream db( "test.db" );
+ if( db && db.good() ) {
+ populate_set( db, backed_up );
+ }
+
+ // Now divide the two sets into three sets (added, deleted and common )
+ file_set added, deleted, common, old_common;
+ partition_sets( current, backed_up, added, common, old_common, deleted );
+
+ // Now find the list of files to backup.
+ file_set backups;
+
+ // backup all added files
+ copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
+
+ // Backup files that have been modified
+ file_set::iterator i = common.begin(), j = old_common.begin();
+ for( ; i != common.end(); ++i, ++j ) {
+ (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
+
+ if( needs_backup( *j, *i ) ) backups.insert( *i );
+ }
+
+ // Now, sort the backups by filesize and build a list that'll fit on a DVD
+ file_vector backups_s;
+ copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
+
+ FileDataSizeCmp sizecmp;
+ sort( backups_s.begin(), backups_s.end(), sizecmp );
+
+ file_set final;
+ unsigned long long space = 0x107c00000ULL; // 4220 MBytes
+
+ 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 );
+
+ // Now, sort the non-backed-up list by last_backup_date and back-fill
+ if( 0 != space ) {
+ file_vector leftovers;
+ FileDataNameCmp cmp;
+ set_difference( current.begin(), current.end(),
+ final.begin(), final.end(),
+ back_inserter( leftovers ),
+ cmp );
+
+ FileDataLastBackupCmp lastbackupcmp;
+ sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
+
+ copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
+ }
+
+ unsigned long long now = current_time();
+ for( file_set::iterator k = final.begin(); k != final.end(); ++k ) {
+ (*k)->setLastBackupDate( now );
+ }
+
+ // Write the 'current' list to the dbfile
+ ofstream dbout( "test.db" );
+ copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout, "" ) );
+
+ // Write the 'final' list to stdout
+ copy( final.begin(), final.end(), ostream_iterator<FileData*>( cout, "" ) );
+
+ if( ! complete ) { cerr << "incomplete" << endl; }