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_union( current.begin(), current.end(),
78 old.begin(), old.end(),
79 inserter( common, common.begin() ),
82 set_union( 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 and build a list that'll fit on a DVD
181 file_vector backups_s;
182 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
184 FileDataSizeCmp sizecmp;
185 sort( backups_s.begin(), backups_s.end(), sizecmp );
188 unsigned long long space = 0x100000000ULL;
190 insert_iterator<file_set> final_i( final, final.begin() );
192 // Copy files over until full or out of files
194 = copy_until_full( backups_s.rbegin(), backups_s.rend(), final_i, space );
196 // Track the size filled up by essential backups
197 unsigned long long essential_blocks, essential_bytes;
198 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
200 // Now, sort the non-backed-up list by last_backup_date and back-fill
202 file_vector leftovers;
204 set_difference( current.begin(), current.end(),
205 final.begin(), final.end(),
206 back_inserter( leftovers ),
209 FileDataLastBackupCmp lastbackupcmp;
210 sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
212 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
215 // Track the total size to be copied to the dvd
216 unsigned long long total_blocks, total_bytes;
217 sizes( final.begin(), final.end(), total_blocks, total_bytes );
219 updateLastBackupDate( final.begin(), final.end(), now );
221 // Write the 'current' list to the dbfile
222 ofstream dbout( dbname );
223 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
225 // Write the 'final' list to stdout
226 copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
228 cerr << now << endl << endl;
230 cerr << "Need backing up..." << endl;
231 cerr << " Added Bytes: " << added_bytes << endl;
232 cerr << " Added Blocks: " << added_blocks << endl;
233 cerr << " Modified Bytes: " << modified_bytes << endl;
234 cerr << " Modified Blocks: " << modified_blocks << endl << endl;
236 cerr << "Will be backed up..." << endl;
237 cerr << " Essential Bytes: " << essential_bytes << endl;
238 cerr << " Essential Blocks: " << essential_blocks << endl;
239 cerr << " Total Bytes: " << total_bytes << endl;
240 cerr << " Total Blocks: " << total_blocks << endl << endl;
242 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
245 delete_objects( backed_up.begin(), backed_up.end() );
246 delete_objects( current.begin(), current.end() );