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 || getenv("backupdbout") == NULL || getenv("blocksize") == NULL || getenv("availsizemb") == NULL)
141 cerr << "Required environment variables are not set. Exiting." << endl;
145 // Setup our bytes_in_block value
146 bytes_in_block = atoll(getenv("blocksize"));
148 // Get the date on stdin
149 unsigned long long now = read_time( cin );
151 // Parse the list of current files on stdin
153 populate_set( cin, current );
156 ifstream db( dbname_in );
157 if( db && db.good() ) {
158 populate_set( db, backed_up );
161 // Now divide the two sets into three sets (added, deleted and common )
162 file_set added, deleted, common, old_common;
163 partition_sets( current, backed_up, added, common, old_common, deleted );
165 // Now find the list of files to backup.
168 // backup all added files
169 copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
171 // Track the total size of added files
172 unsigned long long added_blocks, added_bytes;
173 sizes( added.begin(), added.end(), added_blocks, added_bytes );
175 file_vector modified_v;
176 // Backup files that have been modified
177 file_set::iterator i = common.begin(), j = old_common.begin();
178 for( ; i != common.end(); ++i, ++j ) {
179 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
181 if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
184 copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
186 // Track the total size of modified files
187 unsigned long long modified_blocks, modified_bytes;
188 sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes );
190 // Now, sort the backups by filesize (decreasing) and build a list that'll fit
192 file_vector backups_s;
193 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
195 sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
198 unsigned long long space = atoll(getenv("availsizemb")) * 1048576ull;
200 insert_iterator<file_set> final_i( final, final.begin() );
202 // Copy files over until full or out of files
204 = copy_until_full( backups_s.begin(), backups_s.end(), final_i, space );
206 // Track the size filled up by essential backups
207 unsigned long long essential_blocks, essential_bytes;
208 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
210 // Now, sort the non-backed-up list by last_backup_date, then by filesize
211 // (decreasing) and back-fill. This should minimize the number of DVDs in the
212 // collection left with actual content.
214 file_vector leftovers;
215 set_difference( current.begin(), current.end(),
216 final.begin(), final.end(),
217 back_inserter( leftovers ),
220 // Achieve 'last backup date then by filesize' by first sorting by filesize
221 // and then running stable sort by last backup date.
222 sort( leftovers.rbegin(), leftovers.rend(), FileData::sizecmp );
223 stable_sort( leftovers.begin(), leftovers.end(), FileData::lastbackupcmp );
225 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
228 // Track the total size to be copied to the dvd
229 unsigned long long total_blocks, total_bytes;
230 sizes( final.begin(), final.end(), total_blocks, total_bytes );
232 // Track how many disks there are remaining to be burned
233 unsigned long long disks_remaining = 0;
234 if(modified_bytes || added_bytes)
235 disks_remaining = (unsigned long long)ceil(static_cast<double>(modified_bytes+added_bytes)/(atoll(getenv("availsizemb"))*1048576ull))-1;
237 updateLastBackupDate( final.begin(), final.end(), now );
239 // Write the 'current' list to the dbfile
240 ofstream dbout( dbname_out );
241 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
243 // Write the 'final' list to stdout
244 copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
246 cerr << now << endl << endl;
248 cerr << "Need backing up..." << endl;
249 cerr << " Added Bytes: " << added_bytes << endl;
250 cerr << " Added Blocks: " << added_blocks << endl;
251 cerr << " Modified Bytes: " << modified_bytes << endl;
252 cerr << " Modified Blocks: " << modified_blocks << endl;
253 cerr << " Disks Remaining: " << disks_remaining << endl << endl;
255 cerr << "Will be backed up..." << endl;
256 cerr << " Essential Bytes: " << essential_bytes << endl;
257 cerr << " Essential Blocks: " << essential_blocks << endl;
258 cerr << " Total Bytes: " << total_bytes << endl;
259 cerr << " Total Blocks: " << total_blocks << endl << endl;
261 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
264 delete_objects( backed_up.begin(), backed_up.end() );
265 delete_objects( current.begin(), current.end() );