++begin
[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   I i = begin;
33   while( 0 != space && i != end ) {
34     INT size = (*i)->getFileSize();
35     INT blocksize = blocks( size ) * bytes_in_block;
36
37     if( blocksize <= space ) {
38       space -= blocksize;
39       out = *i;
40       ++out;
41     } else {
42       // We missed a file that should be included so the backup is not complete
43       complete = false;
44     }
45     ++i;
46   }
47   return complete;
48 }
49
50 template<class SET>
51 void populate_set( istream &in, SET &files ) {
52   do {
53     FileData *data = new FileData();
54     in >> data;
55     if( data->getFileName().size() ) {
56       files.insert( data );
57     } else {
58       delete data;
59     }
60   } while( ! in.eof() );
61 }
62
63 template<class SET>
64 void partition_sets( const SET &current, const SET &old,
65                      SET &added, SET &common, SET &old_common, SET &deleted  ) {
66   FileDataNameCmp cmp;
67
68   set_difference( current.begin(), current.end(),
69                   old.begin(),     old.end(),
70                   inserter( added, added.begin() ),
71                   cmp );
72
73   set_difference( old.begin(),     old.end(),
74                   current.begin(), current.end(),
75                   inserter( deleted, deleted.begin() ),
76                   cmp );
77
78   set_union(      current.begin(), current.end(),
79                   old.begin(),     old.end(),
80                   inserter( common, common.begin() ),
81                   cmp );
82
83   set_union(      old.begin(),    old.end(),
84                   common.begin(), common.end(),
85                   inserter( old_common, old_common.begin() ),
86                   cmp );
87 }
88
89 template<class INT>
90 INT blocks( const INT &bytes ) {
91   INT numblocks = bytes / bytes_in_block;
92   if( 0 != bytes % bytes_in_block ) numblocks++;
93
94   return numblocks;
95 }
96
97 template<class ITER, class INT>
98 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
99   numblocks = 0;
100   numbytes  = 0;
101
102   while( begin != end ) {
103     INT filesize = (*begin)->getFileSize();
104
105     numbytes  += filesize;
106     numblocks += blocks( filesize );
107     ++begin;
108   }
109 }
110
111 template<class I, class O>
112 void copy_filenames( I begin, const I &end, O out ) {
113   while( begin != end ) {
114     string output = (*begin)->getFileName();
115     output.push_back( 0 );
116     *out = output;
117     ++out;
118     ++begin;
119   }
120 }
121
122 template<class ITER>
123 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
124   while( begin != end ) {
125     (*begin)->setLastBackupDate( date );
126     ++begin;
127   }
128 }
129
130 template<class ITER>
131 void delete_objects( ITER begin, const ITER &end ) {
132   while( begin != end ) {
133     delete *begin;
134     ++begin;
135   }
136 }
137
138 int main() {
139   // Get the date on stdin
140   unsigned long long now = read_time( cin );
141
142   // Parse the list of current files on stdin
143   file_set current;
144   populate_set( cin, current );
145
146   file_set backed_up;
147   ifstream db( dbname );
148   if( db && db.good() ) {
149     populate_set( db, backed_up );
150   }
151
152   // Now divide the two sets into three sets (added, deleted and common )
153   file_set added, deleted, common, old_common;
154   partition_sets( current, backed_up, added, common, old_common, deleted );
155
156   // Now find the list of files to backup.
157   file_set backups;
158   insert_iterator<file_set> backups_i( backups, backups.begin() );
159
160   // backup all added files
161   copy( added.begin(), added.end(), backups_i );
162
163   // Track the total size of added files
164   unsigned long long added_blocks, added_bytes;
165   sizes( added.begin(), added.end(), added_blocks, added_bytes );
166
167   file_vector modified_files;
168   // Backup files that have been modified
169   file_set::iterator i = common.begin(), j = old_common.begin();
170   for( ; i != common.end(); ++i, ++j ) {
171     (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
172
173     if( needs_backup( *j, *i ) ) modified_files.push_back( *i );
174   }
175
176   copy( modified_files.begin(), modified_files.end(), backups_i );
177
178   // Track the total size of modified files
179   unsigned long long modified_blocks, modified_bytes;
180   sizes( modified_files.begin(), modified_files.end(), modified_blocks, modified_bytes );
181
182   // Now, sort the backups by filesize and build a list that'll fit on a DVD
183   file_vector backups_s;
184   copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
185
186   FileDataSizeCmp sizecmp;
187   sort( backups_s.begin(), backups_s.end(), sizecmp );
188
189   file_set final;
190   unsigned long long space = 0x100000000ULL; // After looking at how big the ISO can be and how many 1K blocks are left after formatting the filesystem I decided on an even 4GB target
191
192   insert_iterator<file_set> final_i( final, final.begin() );
193
194   // Copy files over until full or out of files
195   bool complete
196     = copy_until_full( backups_s.rbegin(), backups_s.rend(), final_i, space );
197
198   // Track the size filled up by essential backups
199   unsigned long long essential_blocks, essential_bytes;
200   sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
201
202   // Now, sort the non-backed-up list by last_backup_date and back-fill
203   if( 0 != space ) {
204     file_vector leftovers;
205     FileDataNameCmp cmp;
206     set_difference( current.begin(), current.end(),
207                     final.begin(),   final.end(),
208                     back_inserter( leftovers ),
209                     cmp );
210
211     FileDataLastBackupCmp lastbackupcmp;
212     sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
213
214     copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
215   }
216
217   // Track the total size to be copied to the dvd
218   unsigned long long total_blocks, total_bytes;
219   sizes( final.begin(), final.end(), total_blocks, total_bytes );
220
221   updateLastBackupDate( final.begin(), final.end(), now );
222
223   // Write the 'current' list to the dbfile
224   ofstream dbout( dbname );
225   copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
226
227   // Write the 'final' list to stdout
228   copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
229
230   cerr << now << endl << endl;
231
232   cerr << "Need backing up..." << endl;
233   cerr << "     Added Bytes:            " << added_bytes << endl;
234   cerr << "     Added Blocks:           " << added_blocks << endl;
235   cerr << "     Modified Bytes:         " << modified_bytes << endl;
236   cerr << "     Modified Blocks:        " << modified_blocks << endl << endl;
237
238   cerr << "Will be backed up..." << endl;
239   cerr << "     Essential Bytes:        " << essential_bytes << endl;
240   cerr << "     Essential Blocks:       " << essential_blocks << endl;
241   cerr << "     Total Bytes:            " << total_bytes << endl;
242   cerr << "     Total Blocks:           " << total_blocks << endl << endl;
243
244   if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
245
246   // Clean-up
247   delete_objects( backed_up.begin(), backed_up.end() );
248   delete_objects( current.begin(),   current.end() );
249 }