8 #include "filedata.hpp"
12 static const unsigned int bytes_in_block = 0x800;
14 unsigned long long current_time() {
15 unsigned long long rc = 0;
16 time_t now_tt = time( 0 );
17 tm *now = localtime( &now_tt );
18 rc += ( now->tm_year + 1900ULL ) * 10000000000ULL;
19 rc += ( now->tm_mon + 1ULL ) * 100000000ULL;
20 rc += now->tm_mday * 1000000ULL;
21 rc += now->tm_hour * 10000ULL;
22 rc += now->tm_min * 100ULL;
28 template<class I, class O, class INT>
29 bool copy_until_full( I begin, I end, O out, INT &space ) {
33 while( 0 != space && i != end ) {
34 INT size = (*i)->getFileSize();
35 INT blocksize = blocks( size ) * bytes_in_block;
37 if( blocksize <= space ) {
42 // We missed a file that should be included so the backup is not complete
51 void populate_set( istream &in, SET &files ) {
53 FileData *data = new FileData();
55 if( data->getFileName().size() ) {
60 } while( ! in.eof() );
64 void partition_sets( const SET ¤t, const SET &old,
65 SET &added, SET &common, SET &old_common, SET &deleted ) {
68 set_difference( current.begin(), current.end(),
69 old.begin(), old.end(),
70 inserter( added, added.begin() ),
73 set_difference( old.begin(), old.end(),
74 current.begin(), current.end(),
75 inserter( deleted, deleted.begin() ),
78 set_union( current.begin(), current.end(),
79 old.begin(), old.end(),
80 inserter( common, common.begin() ),
83 set_union( old.begin(), old.end(),
84 common.begin(), common.end(),
85 inserter( old_common, old_common.begin() ),
90 INT blocks( const INT &bytes ) {
91 INT numblocks = bytes / bytes_in_block;
92 if( 0 != bytes % bytes_in_block ) numblocks++;
97 template<class ITER, class INT>
98 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
102 while( begin != end ) {
103 INT filesize = (*begin)->getFileSize();
105 numbytes += filesize;
106 numblocks += blocks( filesize );
112 // Parse the list of current files on stdin
114 populate_set( cin, current );
117 ifstream db( "test.db" );
118 if( db && db.good() ) {
119 populate_set( db, backed_up );
122 // Now divide the two sets into three sets (added, deleted and common )
123 file_set added, deleted, common, old_common;
124 partition_sets( current, backed_up, added, common, old_common, deleted );
126 // Now find the list of files to backup.
128 insert_iterator<file_set> backups_i( backups, backups.begin() );
130 // backup all added files
131 copy( added.begin(), added.end(), backups_i );
133 // Track the total size of added files
134 unsigned long long added_blocks, added_bytes;
135 sizes( backups.begin(), backups.end(), added_blocks, added_bytes );
137 file_vector modified_files;
138 // Backup files that have been modified
139 file_set::iterator i = common.begin(), j = old_common.begin();
140 for( ; i != common.end(); ++i, ++j ) {
141 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
143 if( needs_backup( *j, *i ) ) modified_files.push_back( *i );
146 copy( modified_files.begin(), modified_files.end(), backups_i );
148 // Track the total size of modified files
149 unsigned long long modified_blocks, modified_bytes;
150 sizes( modified_files.begin(), modified_files.end(), modified_blocks, modified_bytes );
152 // Now, sort the backups by filesize and build a list that'll fit on a DVD
153 file_vector backups_s;
154 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
156 FileDataSizeCmp sizecmp;
157 sort( backups_s.begin(), backups_s.end(), sizecmp );
160 unsigned long long space = 0x102800000ULL; // about 98% of 4220 MBytes
162 insert_iterator<file_set> final_i( final, final.begin() );
164 // Copy files over until full or out of files
165 bool complete = copy_until_full( backups_s.rbegin(),
170 // Track the size filled up by essential backups
171 unsigned long long essential_blocks, essential_bytes;
172 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
174 // Now, sort the non-backed-up list by last_backup_date and back-fill
176 file_vector leftovers;
178 set_difference( current.begin(), current.end(),
179 final.begin(), final.end(),
180 back_inserter( leftovers ),
183 FileDataLastBackupCmp lastbackupcmp;
184 sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
186 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
189 // Track the total size to be copied to the dvd
190 unsigned long long total_blocks, total_bytes;
191 sizes( final.begin(), final.end(), total_blocks, total_bytes );
193 unsigned long long now = current_time();
194 for( file_set::iterator k = final.begin(); k != final.end(); ++k ) {
195 (*k)->setLastBackupDate( now );
198 // Write the 'current' list to the dbfile
199 ofstream dbout( "test.db" );
200 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout, "" ) );
202 // Write the 'final' list to stdout
203 copy( final.begin(), final.end(), ostream_iterator<FileData*>( cout, "" ) );
205 cerr << "Need backing up..." << endl;
206 cerr << " Added Bytes: " << added_bytes << endl;
207 cerr << " Added Blocks: " << added_blocks << endl;
208 cerr << " Modified Bytes: " << modified_bytes << endl;
209 cerr << " Modified Blocks: " << modified_blocks << endl << endl;
211 cerr << "Will be backed up..." << endl;
212 cerr << " Essential Bytes: " << essential_bytes << endl;
213 cerr << " Essential Blocks: " << essential_blocks << endl;
214 cerr << " Total Bytes: " << total_bytes << endl;
215 cerr << " Total Blocks: " << total_blocks << endl << endl;
217 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
220 for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }
221 for( file_set::iterator i = current.begin(); i != current.end(); ++i ) { delete *i; }