d00d8de3aa1f089710890acd71180eb0dff470da
[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
14 unsigned long long current_time() {
15   unsigned long long rc = 0;
16   time_t now_tt = time( 0 );
17   tm *now = localtime( &now_tt );
18   rc += ( now->tm_year + 1900ULL ) * 10000000000ULL;
19   rc += ( now->tm_mon  + 1ULL )    * 100000000ULL;
20   rc +=   now->tm_mday             * 1000000ULL;
21   rc +=   now->tm_hour             * 10000ULL;
22   rc +=   now->tm_min              * 100ULL;
23   rc +=   now->tm_sec;
24
25   return rc;
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 int main() {
112   // Parse the list of current files on stdin
113   file_set current;
114   populate_set( cin, current );
115
116   file_set backed_up;
117   ifstream db( "test.db" );
118   if( db && db.good() ) {
119     populate_set( db, backed_up );
120   }
121
122   // Now divide the two sets into three sets (added, deleted and common )
123   file_set added, deleted, common, old_common;
124   partition_sets( current, backed_up, added, common, old_common, deleted );
125
126   // Now find the list of files to backup.
127   file_set backups;
128   insert_iterator<file_set> backups_i( backups, backups.begin() );
129
130   // backup all added files
131   copy( added.begin(), added.end(), backups_i );
132
133   // Track the total size of added files
134   unsigned long long added_blocks, added_bytes;
135   sizes( backups.begin(), backups.end(), added_blocks, added_bytes );
136
137   file_vector modified_files;
138   // Backup files that have been modified
139   file_set::iterator i = common.begin(), j = old_common.begin();
140   for( ; i != common.end(); ++i, ++j ) {
141     (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
142
143     if( needs_backup( *j, *i ) ) modified_files.push_back( *i );
144   }
145
146   copy( modified_files.begin(), modified_files.end(), backups_i );
147
148   // Track the total size of modified files
149   unsigned long long modified_blocks, modified_bytes;
150   sizes( modified_files.begin(), modified_files.end(), modified_blocks, modified_bytes );
151
152   // Now, sort the backups by filesize and build a list that'll fit on a DVD
153   file_vector backups_s;
154   copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
155
156   FileDataSizeCmp sizecmp;
157   sort( backups_s.begin(), backups_s.end(), sizecmp );
158
159   file_set final;
160   unsigned long long space = 0x102800000ULL;  // about 98% of 4220 MBytes
161
162   insert_iterator<file_set> final_i( final, final.begin() );
163
164   // Copy files over until full or out of files
165   bool complete = copy_until_full( backups_s.rbegin(),
166                                    backups_s.rend(),
167                                    final_i,
168                                    space );
169
170   // Track the size filled up by essential backups
171   unsigned long long essential_blocks, essential_bytes;
172   sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
173
174   // Now, sort the non-backed-up list by last_backup_date and back-fill
175   if( 0 != space ) {
176     file_vector leftovers;
177     FileDataNameCmp cmp;
178     set_difference( current.begin(), current.end(),
179                     final.begin(),   final.end(),
180                     back_inserter( leftovers ),
181                     cmp );
182
183     FileDataLastBackupCmp lastbackupcmp;
184     sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
185
186     copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
187   }
188
189   // Track the total size to be copied to the dvd
190   unsigned long long total_blocks, total_bytes;
191   sizes( final.begin(), final.end(), total_blocks, total_bytes );
192
193   unsigned long long now = current_time();
194   for( file_set::iterator k = final.begin(); k != final.end(); ++k ) {
195     (*k)->setLastBackupDate( now );
196   }
197
198   // Write the 'current' list to the dbfile
199   ofstream dbout( "test.db" );
200   copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout, "" ) );
201
202   // Write the 'final' list to stdout
203   copy( final.begin(), final.end(), ostream_iterator<FileData*>( cout, "" ) );
204
205   cerr << "Need backing up..." << endl;
206   cerr << "     Added Bytes:            " << added_bytes << endl;
207   cerr << "     Added Blocks:           " << added_blocks << endl;
208   cerr << "     Modified Bytes:         " << modified_bytes << endl;
209   cerr << "     Modified Blocks:        " << modified_blocks << endl << endl;
210
211   cerr << "Will be backed up..." << endl;
212   cerr << "     Essential Bytes:        " << essential_bytes << endl;
213   cerr << "     Essential Blocks:       " << essential_blocks << endl;
214   cerr << "     Total Bytes:            " << total_bytes << endl;
215   cerr << "     Total Blocks:           " << total_blocks << endl << endl;
216
217   if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
218
219   // Clean-up
220   for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }
221   for( file_set::iterator i = current.begin();   i != current.end();   ++i ) { delete *i; }
222 }