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 ) {
65 set_difference( current.begin(), current.end(),
66 old.begin(), old.end(),
67 inserter( added, added.begin() ),
70 set_difference( old.begin(), old.end(),
71 current.begin(), current.end(),
72 inserter( deleted, deleted.begin() ),
75 set_intersection( current.begin(), current.end(),
76 old.begin(), old.end(),
77 inserter( common, common.begin() ),
80 set_intersection( old.begin(), old.end(),
81 common.begin(), common.end(),
82 inserter( old_common, old_common.begin() ),
87 INT blocks( const INT &bytes ) {
88 INT numblocks = bytes / bytes_in_block;
89 if( 0 != bytes % bytes_in_block ) numblocks++;
94 template<class ITER, class INT>
95 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
99 while( begin != end ) {
100 INT filesize = (*begin)->getFileSize();
102 numbytes += filesize;
103 numblocks += blocks( filesize );
108 template<class I, class O>
109 void copy_filenames( I begin, const I &end, O out ) {
110 while( begin != end ) {
111 string output = (*begin)->getFileName();
112 output.push_back( 0 );
120 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
121 while( begin != end ) {
122 (*begin)->setLastBackupDate( date );
128 void delete_objects( ITER begin, const ITER &end ) {
129 while( begin != end ) {
136 // Get the date on stdin
137 unsigned long long now = read_time( cin );
139 // Parse the list of current files on stdin
141 populate_set( cin, current );
144 ifstream db( dbname );
145 if( db && db.good() ) {
146 populate_set( db, backed_up );
149 // Now divide the two sets into three sets (added, deleted and common )
150 file_set added, deleted, common, old_common;
151 partition_sets( current, backed_up, added, common, old_common, deleted );
153 // Now find the list of files to backup.
156 // backup all added files
157 copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
159 // Track the total size of added files
160 unsigned long long added_blocks, added_bytes;
161 sizes( added.begin(), added.end(), added_blocks, added_bytes );
163 file_vector modified_v;
164 // Backup files that have been modified
165 file_set::iterator i = common.begin(), j = old_common.begin();
166 for( ; i != common.end(); ++i, ++j ) {
167 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
169 if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
172 copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
174 // Track the total size of modified files
175 unsigned long long modified_blocks, modified_bytes;
176 sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes );
178 // Now, sort the backups by filesize (decreasing) and build a list that'll fit
180 file_vector backups_s;
181 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
183 sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
186 unsigned long long space = 0x100000000ULL;
188 insert_iterator<file_set> final_i( final, final.begin() );
190 // Copy files over until full or out of files
192 = copy_until_full( backups_s.begin(), backups_s.end(), final_i, space );
194 // Track the size filled up by essential backups
195 unsigned long long essential_blocks, essential_bytes;
196 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
198 // Now, sort the non-backed-up list by last_backup_date, then by filesize
199 // (decreasing) and back-fill. This should minimize the number of DVDs in the
200 // collection left with actual content.
202 file_vector leftovers;
203 set_difference( current.begin(), current.end(),
204 final.begin(), final.end(),
205 back_inserter( leftovers ),
208 // Achieve 'last back date then by filesize' by first sorting by filesize
209 // and then running stable sort by last backup date.
210 sort( leftovers.rbegin(), leftovers.rend(), FileData::sizecmp );
211 stable_sort( leftovers.begin(), leftovers.end(), FileData::lastbackupcmp );
213 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
216 // Track the total size to be copied to the dvd
217 unsigned long long total_blocks, total_bytes;
218 sizes( final.begin(), final.end(), total_blocks, total_bytes );
220 updateLastBackupDate( final.begin(), final.end(), now );
222 // Write the 'current' list to the dbfile
223 ofstream dbout( dbname );
224 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
226 // Write the 'final' list to stdout
227 copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
229 cerr << now << endl << endl;
231 cerr << "Need backing up..." << endl;
232 cerr << " Added Bytes: " << added_bytes << endl;
233 cerr << " Added Blocks: " << added_blocks << endl;
234 cerr << " Modified Bytes: " << modified_bytes << endl;
235 cerr << " Modified Blocks: " << modified_blocks << endl << endl;
237 cerr << "Will be backed up..." << endl;
238 cerr << " Essential Bytes: " << essential_bytes << endl;
239 cerr << " Essential Blocks: " << essential_blocks << endl;
240 cerr << " Total Bytes: " << total_bytes << endl;
241 cerr << " Total Blocks: " << total_blocks << endl << endl;
243 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
246 delete_objects( backed_up.begin(), backed_up.end() );
247 delete_objects( current.begin(), current.end() );