Mispelling in comment
[backups/.git] / main.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <iterator>
4 #include <algorithm>
5 #include <cassert>
6 #include <ctime>
7
8 #include "filedata.hpp"
9
10 using namespace std;
11
12 static const unsigned int bytes_in_block = 0x800;
13 static const char * dbname = "/var/lib/backups/backups.db";
14
15 unsigned long long read_time( istream &i ) {
16   string date_string;
17
18   int c;
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 );
22   }
23   if( char_traits<char>::eof() == c ) { i.setstate( ios_base::eofbit ); }
24
25   return atoll( date_string.c_str() );
26 }
27
28 template<class I, class O, class INT>
29 bool copy_until_full( I begin, I end, O out, INT &space ) {
30   bool complete = true;
31
32   while( 0 != space && begin != end ) {
33     INT size = (*begin)->getFileSize();
34     INT blocksize = blocks( size ) * bytes_in_block;
35
36     if( blocksize <= space ) {
37       space -= blocksize;
38       *out = *begin;
39       ++out;
40     } else {
41       // We missed a file that should be included so the backup is not complete
42       complete = false;
43     }
44     ++begin;
45   }
46   return complete;
47 }
48
49 template<class SET>
50 void populate_set( istream &in, SET &files ) {
51   do {
52     FileData *data = new FileData();
53     in >> data;
54     if( data->getFileName().size() ) {
55       files.insert( data );
56     } else {
57       delete data;
58     }
59   } while( ! in.eof() );
60 }
61
62 template<class SET>
63 void partition_sets( const SET &current, 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() ),
68                     FileData::namecmp );
69
70   set_difference(   old.begin(),     old.end(),
71                     current.begin(), current.end(),
72                     inserter( deleted, deleted.begin() ),
73                     FileData::namecmp );
74
75   set_intersection( current.begin(), current.end(),
76                     old.begin(),     old.end(),
77                     inserter( common, common.begin() ),
78                     FileData::namecmp );
79
80   set_intersection( old.begin(),    old.end(),
81                     common.begin(), common.end(),
82                     inserter( old_common, old_common.begin() ),
83                     FileData::namecmp );
84 }
85
86 template<class INT>
87 INT blocks( const INT &bytes ) {
88   INT numblocks = bytes / bytes_in_block;
89   if( 0 != bytes % bytes_in_block ) numblocks++;
90
91   return numblocks;
92 }
93
94 template<class ITER, class INT>
95 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
96   numblocks = 0;
97   numbytes  = 0;
98
99   while( begin != end ) {
100     INT filesize = (*begin)->getFileSize();
101
102     numbytes  += filesize;
103     numblocks += blocks( filesize );
104     ++begin;
105   }
106 }
107
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 );
113     *out = output;
114     ++out;
115     ++begin;
116   }
117 }
118
119 template<class ITER>
120 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
121   while( begin != end ) {
122     (*begin)->setLastBackupDate( date );
123     ++begin;
124   }
125 }
126
127 template<class ITER>
128 void delete_objects( ITER begin, const ITER &end ) {
129   while( begin != end ) {
130     delete *begin;
131     ++begin;
132   }
133 }
134
135 int main() {
136   // Get the date on stdin
137   unsigned long long now = read_time( cin );
138
139   // Parse the list of current files on stdin
140   file_set current;
141   populate_set( cin, current );
142
143   file_set backed_up;
144   ifstream db( dbname );
145   if( db && db.good() ) {
146     populate_set( db, backed_up );
147   }
148
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 );
152
153   // Now find the list of files to backup.
154   file_set backups;
155
156   // backup all added files
157   copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
158
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 );
162
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() );
168
169     if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
170   }
171
172   copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
173
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 );
177
178   // Now, sort the backups by filesize (decreasing) and build a list that'll fit
179   // on a DVD
180   file_vector backups_s;
181   copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
182
183   sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
184
185   file_set final;
186   unsigned long long space = 0x100000000ULL;
187
188   insert_iterator<file_set> final_i( final, final.begin() );
189
190   // Copy files over until full or out of files
191   bool complete
192     = copy_until_full( backups_s.begin(), backups_s.end(), final_i, space );
193
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 );
197
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.
201   if( 0 != space ) {
202     file_vector leftovers;
203     set_difference( current.begin(), current.end(),
204                     final.begin(),   final.end(),
205                     back_inserter( leftovers ),
206                     FileData::namecmp );
207
208     // Achieve 'last backup 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 );
212
213     copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
214   }
215
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 );
219
220   updateLastBackupDate( final.begin(), final.end(), now );
221
222   // Write the 'current' list to the dbfile
223   ofstream dbout( dbname );
224   copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
225
226   // Write the 'final' list to stdout
227   copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
228
229   cerr << now << endl << endl;
230
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;
236
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;
242
243   if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
244
245   // Clean-up
246   delete_objects( backed_up.begin(), backed_up.end() );
247   delete_objects( current.begin(),   current.end() );
248 }