X-Git-Url: http://git.pippins.net/embedvideo/.git/?a=blobdiff_plain;f=main.cpp;h=4e7116368fea465c46528b374ba6b63063acd541;hb=a0d9675e7514609651f14794a08ec2b61b0fe12e;hp=07868bcd65199d14e2a43da88dbf3740018bb672;hpb=3a93fc06d34d4a2a577ff53fa9e6321598d8f9fd;p=backups%2F.git diff --git a/main.cpp b/main.cpp index 07868bc..4e71163 100644 --- a/main.cpp +++ b/main.cpp @@ -9,40 +9,39 @@ using namespace std; -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 unsigned int bytes_in_block = 0x800; +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 directly here + for( c = i.get(); 0 != c && char_traits::eof() != c; c = i.get() ) { + date_string.push_back( c ); + } + if( char_traits::eof() == c ) { i.setstate( ios_base::eofbit ); } + + return atoll( date_string.c_str() ); } -template -bool copy_until_full( I begin, I end, O out, unsigned long long &space ) { - const unsigned long long block_size = 0x200ULL; +template +bool copy_until_full( I begin, I end, O out, INT &space ) { bool complete = true; - I i = begin; - while( 0 != space && i != end ) { - unsigned long long size = (*i)->getFileSize(); - unsigned long long blocks = size & ( ~(block_size-1) ); - if( blocks < size ) blocks += block_size; + while( 0 != space && begin != end ) { + INT size = (*begin)->getFileSize(); + INT blocksize = blocks( size ) * bytes_in_block; - if( blocks <= space ) { - space -= blocks; - out = *i; + if( blocksize <= space ) { + space -= blocksize; + *out = *begin; ++out; } else { // We missed a file that should be included so the backup is not complete complete = false; } - ++i; + ++begin; } return complete; } @@ -63,36 +62,86 @@ void populate_set( istream &in, SET &files ) { template void partition_sets( const SET ¤t, const SET &old, SET &added, SET &common, SET &old_common, SET &deleted ) { - FileDataNameCmp cmp; - - set_difference( current.begin(), current.end(), - old.begin(), old.end(), - inserter( added, added.begin() ), - cmp ); - - set_difference( old.begin(), old.end(), - current.begin(), current.end(), - inserter( deleted, deleted.begin() ), - cmp ); - - set_union( current.begin(), current.end(), - old.begin(), old.end(), - inserter( common, common.begin() ), - cmp ); - - set_union( old.begin(), old.end(), - common.begin(), common.end(), - inserter( old_common, old_common.begin() ), - cmp ); + set_difference( current.begin(), current.end(), + old.begin(), old.end(), + inserter( added, added.begin() ), + FileData::namecmp ); + + set_difference( old.begin(), old.end(), + current.begin(), current.end(), + inserter( deleted, deleted.begin() ), + FileData::namecmp ); + + set_intersection( current.begin(), current.end(), + old.begin(), old.end(), + inserter( common, common.begin() ), + FileData::namecmp ); + + set_intersection( old.begin(), old.end(), + common.begin(), common.end(), + inserter( old_common, old_common.begin() ), + FileData::namecmp ); +} + +template +INT blocks( const INT &bytes ) { + INT numblocks = bytes / bytes_in_block; + if( 0 != bytes % bytes_in_block ) numblocks++; + + return numblocks; +} + +template +void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) { + numblocks = 0; + numbytes = 0; + + while( begin != end ) { + INT filesize = (*begin)->getFileSize(); + + numbytes += filesize; + numblocks += blocks( filesize ); + ++begin; + } +} + +template +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 +void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) { + while( begin != end ) { + (*begin)->setLastBackupDate( date ); + ++begin; + } +} + +template +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 ); file_set backed_up; - ifstream db( "test.db" ); + ifstream db( dbname ); if( db && db.good() ) { populate_set( db, backed_up ); } @@ -107,62 +156,93 @@ int main() { // backup all added files copy( added.begin(), added.end(), inserter( backups, backups.begin() ) ); + // Track the total size of added files + unsigned long long added_blocks, added_bytes; + sizes( added.begin(), added.end(), added_blocks, added_bytes ); + + file_vector modified_v; // 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 ); + if( needs_backup( *j, *i ) ) modified_v.push_back( *i ); } - // Now, sort the backups by filesize and build a list that'll fit on a DVD + copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) ); + + // Track the total size of modified files + unsigned long long modified_blocks, modified_bytes; + sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes ); + + // Now, sort the backups by filesize (decreasing) 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 ); + sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp ); file_set final; - unsigned long long space = 0x107c00000ULL; // 4220 MBytes + unsigned long long space = 0x100000000ULL; insert_iterator 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.begin(), backups_s.end(), final_i, space ); - // Now, sort the non-backed-up list by last_backup_date and back-fill + // Track the size filled up by essential backups + unsigned long long essential_blocks, essential_bytes; + sizes( final.begin(), final.end(), essential_blocks, essential_bytes ); + + // Now, sort the non-backed-up list by last_backup_date, then by filesize + // (decreasing) and back-fill. This should minimize the number of DVDs in the + // collection left with actual content. if( 0 != space ) { file_vector leftovers; - FileDataNameCmp cmp; set_difference( current.begin(), current.end(), final.begin(), final.end(), back_inserter( leftovers ), - cmp ); + FileData::namecmp ); - FileDataLastBackupCmp lastbackupcmp; - sort( leftovers.begin(), leftovers.end(), lastbackupcmp ); + // Achieve 'last back date then by filesize' by first sorting by filesizing + // and then running stable sort by last backup date. + sort( leftovers.rbegin(), leftovers.rend(), FileData::sizecmp ); + stable_sort( leftovers.begin(), leftovers.end(), FileData::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 ); - } + // Track the total size to be copied to the dvd + unsigned long long total_blocks, total_bytes; + sizes( final.begin(), final.end(), total_blocks, total_bytes ); + + updateLastBackupDate( final.begin(), final.end(), now ); // Write the 'current' list to the dbfile - ofstream dbout( "test.db" ); - copy( current.begin(), current.end(), ostream_iterator( dbout, "" ) ); + ofstream dbout( dbname ); + copy( current.begin(), current.end(), ostream_iterator( dbout ) ); // Write the 'final' list to stdout - copy( final.begin(), final.end(), ostream_iterator( cout, "" ) ); + copy_filenames( final.begin(), final.end(), ostream_iterator( cout ) ); + + cerr << now << endl << endl; + + cerr << "Need backing up..." << endl; + cerr << " Added Bytes: " << added_bytes << endl; + cerr << " Added Blocks: " << added_blocks << endl; + cerr << " Modified Bytes: " << modified_bytes << endl; + cerr << " Modified Blocks: " << modified_blocks << endl << endl; + + cerr << "Will be backed up..." << endl; + cerr << " Essential Bytes: " << essential_bytes << endl; + cerr << " Essential Blocks: " << essential_blocks << endl; + cerr << " Total Bytes: " << total_bytes << endl; + cerr << " Total Blocks: " << total_blocks << endl << endl; - if( ! complete ) { cerr << "incomplete" << endl; } + 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() ); }