X-Git-Url: http://git.pippins.net/embedvideo/.git/static/git-logo.png?a=blobdiff_plain;f=main.cpp;h=3a86a2bb2df27c03a56a987ff49419129ec683db;hb=0038f2b0e5a51baef501d94ee91ea8e469298fd5;hp=d8a1214192108082e96fe2b78dd11a91e50065de;hpb=de6a3235b4d319401d00db0e7975e2a57ed344b2;p=backups%2F.git diff --git a/main.cpp b/main.cpp index d8a1214..3a86a2b 100644 --- a/main.cpp +++ b/main.cpp @@ -12,38 +12,36 @@ using namespace std; static const unsigned int bytes_in_block = 0x800; static const char * dbname = "/var/lib/backups/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; +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, 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,7 +103,7 @@ void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) { numbytes += filesize; numblocks += blocks( filesize ); - begin++; + ++begin; } } @@ -124,6 +122,7 @@ template void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) { while( begin != end ) { (*begin)->setLastBackupDate( date ); + ++begin; } } @@ -136,6 +135,9 @@ void delete_objects( ITER begin, const ITER &end ) { } 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 ); @@ -152,29 +154,28 @@ int main() { // Now find the list of files to backup. file_set backups; - insert_iterator backups_i( backups, backups.begin() ); // backup all added files - copy( added.begin(), added.end(), backups_i ); + 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_files; + 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 ) ) modified_files.push_back( *i ); + if( needs_backup( *j, *i ) ) modified_v.push_back( *i ); } - copy( modified_files.begin(), modified_files.end(), backups_i ); + 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_files.begin(), modified_files.end(), modified_blocks, modified_bytes ); + sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes ); // Now, sort the backups by filesize and build a list that'll fit on a DVD file_vector backups_s; @@ -184,7 +185,7 @@ int main() { sort( backups_s.begin(), backups_s.end(), sizecmp ); file_set final; - unsigned long long space = 0x100000000ULL; // After looking at how big the ISO can be and how many 1K blocks are left after formatting the filesystem I decided on an even 4GB target + unsigned long long space = 0x100000000ULL; insert_iterator final_i( final, final.begin() ); @@ -215,7 +216,6 @@ int main() { unsigned long long total_blocks, total_bytes; sizes( final.begin(), final.end(), total_blocks, total_bytes ); - unsigned long long now = current_time(); updateLastBackupDate( final.begin(), final.end(), now ); // Write the 'current' list to the dbfile