8 #include "filedata.hpp"
12 static unsigned int bytes_in_block;
13 static const char * dbname_in = getenv("backupdbin");
14 static const char * dbname_out = getenv("backupdbout");
16 unsigned long long read_time( istream &i ) {
20 // Todo, don't use char_traits<char> directly here
21 for( c = i.get(); 0 != c && char_traits<char>::eof() != c; c = i.get() ) {
22 date_string.push_back( c );
24 if( char_traits<char>::eof() == c ) { i.setstate( ios_base::eofbit ); }
26 return atoll( date_string.c_str() );
29 template<class I, class O, class INT>
30 bool copy_until_full( I begin, I end, O out, INT &space ) {
33 while( 0 != space && begin != end ) {
34 INT size = (*begin)->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 ) {
66 set_difference( current.begin(), current.end(),
67 old.begin(), old.end(),
68 inserter( added, added.begin() ),
71 set_difference( old.begin(), old.end(),
72 current.begin(), current.end(),
73 inserter( deleted, deleted.begin() ),
76 set_intersection( current.begin(), current.end(),
77 old.begin(), old.end(),
78 inserter( common, common.begin() ),
81 set_intersection( old.begin(), old.end(),
82 common.begin(), common.end(),
83 inserter( old_common, old_common.begin() ),
88 INT blocks( const INT &bytes ) {
89 INT numblocks = bytes / bytes_in_block;
90 if( 0 != bytes % bytes_in_block ) numblocks++;
95 template<class ITER, class INT>
96 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
100 while( begin != end ) {
101 INT filesize = (*begin)->getFileSize();
103 numbytes += filesize;
104 numblocks += blocks( filesize );
109 template<class I, class O>
110 void copy_filenames( I begin, const I &end, O out ) {
111 while( begin != end ) {
112 string output = (*begin)->getFileName();
113 output.push_back( 0 );
121 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
122 while( begin != end ) {
123 (*begin)->setLastBackupDate( date );
129 void delete_objects( ITER begin, const ITER &end ) {
130 while( begin != end ) {
138 // Check to make sure required env variables are set
139 if( getenv( "backupdbin" ) == NULL ||
140 getenv( "backupdbout" ) == NULL ||
141 getenv( "blocksize" ) == NULL ||
142 getenv( "availsizemb" ) == NULL )
144 cerr << "Required environment variables are not set. Exiting." << endl;
148 // Setup our bytes_in_block value
149 bytes_in_block = atoll(getenv("blocksize"));
151 // Get the date on stdin
152 unsigned long long now = read_time( cin );
154 // Parse the list of current files on stdin
156 populate_set( cin, current );
159 ifstream db( dbname_in );
160 if( db && db.good() ) {
161 populate_set( db, backed_up );
164 // Now divide the two sets into three sets (added, deleted and common )
165 file_set added, deleted, common, old_common;
166 partition_sets( current, backed_up, added, common, old_common, deleted );
168 // Now find the list of files to backup.
171 // backup all added files
172 copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
174 // Track the total size of added files
175 unsigned long long added_blocks, added_bytes;
176 sizes( added.begin(), added.end(), added_blocks, added_bytes );
178 file_vector modified_v;
179 // Backup files that have been modified
180 file_set::iterator i = common.begin(), j = old_common.begin();
181 for( ; i != common.end(); ++i, ++j ) {
182 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
184 if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
187 copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
189 // Track the total size of modified files
190 unsigned long long modified_blocks, modified_bytes;
191 sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes );
193 // Now, sort the backups by filesize (decreasing) and build a list that'll fit
195 file_vector backups_s;
196 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
198 sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
201 const unsigned long long availsizemb = atoll( getenv("availsizemb") ) * 0x100000ull;
202 unsigned long long space = availsizemb;
204 insert_iterator<file_set> final_i( final, final.begin() );
206 // Copy files over until full or out of files
208 = copy_until_full( backups_s.begin(), backups_s.end(), final_i, space );
210 // Track the size filled up by essential backups
211 unsigned long long essential_blocks, essential_bytes;
212 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
214 // Now, sort the non-backed-up list by last_backup_date, then by filesize
215 // (decreasing) and back-fill. This should minimize the number of DVDs in the
216 // collection left with actual content.
218 file_vector leftovers;
219 set_difference( current.begin(), current.end(),
220 final.begin(), final.end(),
221 back_inserter( leftovers ),
224 // Achieve 'last backup date then by filesize' by first sorting by filesize
225 // and then running stable sort by last backup date.
226 sort( leftovers.rbegin(), leftovers.rend(), FileData::sizecmp );
227 stable_sort( leftovers.begin(), leftovers.end(), FileData::lastbackupcmp );
229 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
232 // Track the total size to be copied to the dvd
233 unsigned long long total_blocks, total_bytes;
234 sizes( final.begin(), final.end(), total_blocks, total_bytes );
236 // Track how many disks there are remaining to be burned
237 unsigned long long disks_remaining = 0;
238 if(modified_bytes || added_bytes)
239 disks_remaining = static_cast<unsigned long long>(
240 ceil( static_cast<double>( modified_bytes + added_bytes ) / availsizemb ) - 1
243 updateLastBackupDate( final.begin(), final.end(), now );
245 // Write the 'current' list to the dbfile
246 ofstream dbout( dbname_out );
247 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
249 // Write the 'final' list to stdout
250 copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
252 cerr << now << endl << endl;
254 cerr << "Need backing up..." << endl;
255 cerr << " Added Bytes: " << added_bytes << endl;
256 cerr << " Added Blocks: " << added_blocks << endl;
257 cerr << " Modified Bytes: " << modified_bytes << endl;
258 cerr << " Modified Blocks: " << modified_blocks << endl;
259 cerr << " Disks Remaining: " << disks_remaining << endl << endl;
261 cerr << "Will be backed up..." << endl;
262 cerr << " Essential Bytes: " << essential_bytes << endl;
263 cerr << " Essential Blocks: " << essential_blocks << endl;
264 cerr << " Total Bytes: " << total_bytes << endl;
265 cerr << " Total Blocks: " << total_blocks << endl << endl;
267 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
270 delete_objects( backed_up.begin(), backed_up.end() );
271 delete_objects( current.begin(), current.end() );