8 #include "filedata.hpp"
12 static const unsigned int bytes_in_block = 0x800;
13 static const char * dbname = "/var/lib/backups/backups.db";
15 unsigned long long read_time( istream &i ) {
19 // Todo, don't use char_traits<char> directly here
20 for( c = i.get(); 0 != c && char_traits<char>::eof() != c; c = i.get() ) {
21 date_string.push_back( c );
23 if( char_traits<char>::eof() == c ) { i.setstate( ios_base::eofbit ); }
25 return atoll( date_string.c_str() );
28 template<class I, class O, class INT>
29 bool copy_until_full( I begin, I end, O out, INT &space ) {
32 while( 0 != space && begin != end ) {
33 INT size = (*begin)->getFileSize();
34 INT blocksize = blocks( size ) * bytes_in_block;
36 if( blocksize <= space ) {
41 // We missed a file that should be included so the backup is not complete
50 void populate_set( istream &in, SET &files ) {
52 FileData *data = new FileData();
54 if( data->getFileName().size() ) {
59 } while( ! in.eof() );
63 void partition_sets( const SET ¤t, const SET &old,
64 SET &added, SET &common, SET &old_common, SET &deleted ) {
67 set_difference( current.begin(), current.end(),
68 old.begin(), old.end(),
69 inserter( added, added.begin() ),
72 set_difference( old.begin(), old.end(),
73 current.begin(), current.end(),
74 inserter( deleted, deleted.begin() ),
77 set_intersection( current.begin(), current.end(),
78 old.begin(), old.end(),
79 inserter( common, common.begin() ),
82 set_intersection( old.begin(), old.end(),
83 common.begin(), common.end(),
84 inserter( old_common, old_common.begin() ),
89 INT blocks( const INT &bytes ) {
90 INT numblocks = bytes / bytes_in_block;
91 if( 0 != bytes % bytes_in_block ) numblocks++;
96 template<class ITER, class INT>
97 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
101 while( begin != end ) {
102 INT filesize = (*begin)->getFileSize();
104 numbytes += filesize;
105 numblocks += blocks( filesize );
110 template<class I, class O>
111 void copy_filenames( I begin, const I &end, O out ) {
112 while( begin != end ) {
113 string output = (*begin)->getFileName();
114 output.push_back( 0 );
122 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
123 while( begin != end ) {
124 (*begin)->setLastBackupDate( date );
130 void delete_objects( ITER begin, const ITER &end ) {
131 while( begin != end ) {
138 // Get the date on stdin
139 unsigned long long now = read_time( cin );
141 // Parse the list of current files on stdin
143 populate_set( cin, current );
146 ifstream db( dbname );
147 if( db && db.good() ) {
148 populate_set( db, backed_up );
151 // Now divide the two sets into three sets (added, deleted and common )
152 file_set added, deleted, common, old_common;
153 partition_sets( current, backed_up, added, common, old_common, deleted );
155 // Now find the list of files to backup.
158 // backup all added files
159 copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
161 // Track the total size of added files
162 unsigned long long added_blocks, added_bytes;
163 sizes( added.begin(), added.end(), added_blocks, added_bytes );
165 file_vector modified_v;
166 // Backup files that have been modified
167 file_set::iterator i = common.begin(), j = old_common.begin();
168 for( ; i != common.end(); ++i, ++j ) {
169 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
171 if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
174 copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
176 // Track the total size of modified files
177 unsigned long long modified_blocks, modified_bytes;
178 sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes );
180 // Now, sort the backups by filesize (decreasing) and build a list that'll fit
182 file_vector backups_s;
183 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
185 FileDataSizeCmp sizecmp;
186 sort( backups_s.rbegin(), backups_s.rend(), sizecmp );
189 unsigned long long space = 0x100000000ULL;
191 insert_iterator<file_set> final_i( final, final.begin() );
193 // Copy files over until full or out of files
195 = copy_until_full( backups_s.begin(), backups_s.end(), final_i, space );
197 // Track the size filled up by essential backups
198 unsigned long long essential_blocks, essential_bytes;
199 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
201 // Now, sort the non-backed-up list by last_backup_date, then by filesize
202 // (decreasing) and back-fill. This should minimize the number of DVDs in the
203 // collection left with actual content.
205 file_vector leftovers;
207 set_difference( current.begin(), current.end(),
208 final.begin(), final.end(),
209 back_inserter( leftovers ),
212 FileDataLastBackupCmp lastbackupcmp;
213 // Achieve 'last back date then by filesize' by first sorting by filesizing
214 // and then running stable sort by last backup date.
215 sort( leftovers.rbegin(), leftovers.rend(), sizecmp );
216 stable_sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
218 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
221 // Track the total size to be copied to the dvd
222 unsigned long long total_blocks, total_bytes;
223 sizes( final.begin(), final.end(), total_blocks, total_bytes );
225 updateLastBackupDate( final.begin(), final.end(), now );
227 // Write the 'current' list to the dbfile
228 ofstream dbout( dbname );
229 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
231 // Write the 'final' list to stdout
232 copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
234 cerr << now << endl << endl;
236 cerr << "Need backing up..." << endl;
237 cerr << " Added Bytes: " << added_bytes << endl;
238 cerr << " Added Blocks: " << added_blocks << endl;
239 cerr << " Modified Bytes: " << modified_bytes << endl;
240 cerr << " Modified Blocks: " << modified_blocks << endl << endl;
242 cerr << "Will be backed up..." << endl;
243 cerr << " Essential Bytes: " << essential_bytes << endl;
244 cerr << " Essential Blocks: " << essential_blocks << endl;
245 cerr << " Total Bytes: " << total_bytes << endl;
246 cerr << " Total Blocks: " << total_blocks << endl << endl;
248 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
251 delete_objects( backed_up.begin(), backed_up.end() );
252 delete_objects( current.begin(), current.end() );