9 #include "filedata.hpp"
13 static unsigned int bytes_in_block;
14 static const char * dbname_in = getenv("backupdbin");
15 static const char * dbname_out = getenv("backupdbout");
17 unsigned long long read_time( istream &i ) {
21 // Todo, don't use char_traits<char> directly here
22 for( c = i.get(); 0 != c && char_traits<char>::eof() != c; c = i.get() ) {
23 date_string.push_back( c );
25 if( char_traits<char>::eof() == c ) { i.setstate( ios_base::eofbit ); }
27 return atoll( date_string.c_str() );
30 template<class I, class O, class INT>
31 bool copy_until_full( I begin, I end, O out, INT &space ) {
34 while( begin != end ) {
35 INT size = (*begin)->getFileSize();
36 INT blocksize = blocks( size ) * bytes_in_block;
38 if( blocksize <= space ) {
43 // We missed a file that should be included so the backup is not complete
52 void populate_set( istream &in, SET &files ) {
54 FileData *data = new FileData();
56 if( data->getFileName().size() ) {
61 } while( ! in.eof() );
65 void partition_sets( const SET ¤t, const SET &old,
66 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 ) {
139 // Check to make sure required env variables are set
140 if( getenv( "backupdbin" ) == NULL ||
141 getenv( "backupdbout" ) == NULL ||
142 getenv( "blocksize" ) == NULL ||
143 getenv( "availsizemb" ) == NULL )
145 cerr << "Required environment variables are not set. Exiting." << endl;
149 // Setup our bytes_in_block value
150 bytes_in_block = atoll(getenv("blocksize"));
152 // Get the date on stdin
153 unsigned long long now = read_time( cin );
155 // Parse the list of current files on stdin
157 populate_set( cin, current );
160 ifstream db( dbname_in );
161 if( db && db.good() ) {
162 populate_set( db, backed_up );
165 // Now divide the two sets into three sets (added, deleted and common )
166 file_set added, deleted, common, old_common;
167 partition_sets( current, backed_up, added, common, old_common, deleted );
169 // Now find the list of files to backup.
172 // backup all added files
173 copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
175 // Track the total size of added files
176 unsigned long long added_blocks, added_bytes;
177 sizes( added.begin(), added.end(), added_blocks, added_bytes );
179 file_vector modified_v;
180 // Backup files that have been modified
181 file_set::iterator i = common.begin(), j = old_common.begin();
182 for( ; i != common.end(); ++i, ++j ) {
183 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
185 if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
188 copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
190 // Track the total size of modified files
191 unsigned long long modified_blocks, modified_bytes;
192 sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes );
194 // Now, sort the backups by filesize (decreasing) and build a list that'll fit
196 file_vector backups_s;
197 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
199 sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
202 const unsigned long long availsizemb = atoll( getenv("availsizemb") ) * 0x100000ull;
203 unsigned long long space = availsizemb;
205 insert_iterator<file_set> final_i( final, final.begin() );
207 // Copy files over until full or out of files
209 = copy_until_full( backups_s.begin(), backups_s.end(), final_i, space );
211 // Track the size filled up by essential backups
212 unsigned long long essential_blocks, essential_bytes;
213 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
215 // Now, sort the non-backed-up list by last_backup_date, then by filesize
216 // (decreasing) and back-fill. This should minimize the number of DVDs in the
217 // collection left with actual content.
219 file_vector leftovers;
220 set_difference( current.begin(), current.end(),
221 final.begin(), final.end(),
222 back_inserter( leftovers ),
225 // Achieve 'last backup date then by filesize' by first sorting by filesize
226 // and then running stable sort by last backup date.
227 sort( leftovers.rbegin(), leftovers.rend(), FileData::sizecmp );
228 stable_sort( leftovers.begin(), leftovers.end(), FileData::lastbackupcmp );
230 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
233 // Track the total size to be copied to the dvd
234 unsigned long long total_blocks, total_bytes;
235 sizes( final.begin(), final.end(), total_blocks, total_bytes );
237 // Track how many disks there are remaining to be burned
238 unsigned long long disks_remaining = 0;
239 if(modified_bytes || added_bytes)
240 disks_remaining = static_cast<unsigned long long>(
241 ceil( static_cast<double>( modified_bytes + added_bytes ) / availsizemb ) - 1
244 updateLastBackupDate( final.begin(), final.end(), now );
246 // Write the 'current' list to the dbfile
247 ofstream dbout( dbname_out );
248 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
250 // Write the 'final' list to stdout
251 copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
253 cerr << now << endl << endl;
255 cerr << "Need backing up..." << endl;
256 cerr << " Added Bytes: " << added_bytes << endl;
257 cerr << " Added Blocks: " << added_blocks << endl;
258 cerr << " Modified Bytes: " << modified_bytes << endl;
259 cerr << " Modified Blocks: " << modified_blocks << endl;
260 cerr << " Disks Remaining: " << disks_remaining << endl << endl;
262 cerr << "Will be backed up..." << endl;
263 cerr << " Essential Bytes: " << essential_bytes << endl;
264 cerr << " Essential Blocks: " << essential_blocks << endl;
265 cerr << " Total Bytes: " << total_bytes << endl;
266 cerr << " Total Blocks: " << total_blocks << endl << endl;
268 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
271 delete_objects( backed_up.begin(), backed_up.end() );
272 delete_objects( current.begin(), current.end() );