Reorganize a bit
[backups/.git] / main.cc
1 #include <iostream>
2 #include <fstream>
3 #include <iterator>
4 #include <algorithm>
5 #include <cassert>
6
7 #include "filedata.hpp"
8
9 using namespace std;
10
11 template<class ISTREAM, class SET>
12 void populate_set( ISTREAM &in, SET &files ) {
13   do {
14     FileData *data = new FileData();
15     in >> (*data);
16     files.insert( data );
17   } while( ! in.eof() );
18 }
19
20 template<class SET>
21 void partition_sets( const SET &current, const SET &old,
22                      SET &added, SET &common, SET &deleted  ) {
23   FileDataNameCmp cmp;
24
25   set_difference( current.begin(), current.end(),
26                   old.begin(),     old.end(),
27                   inserter( added, added.begin() ),
28                   cmp );
29
30   set_difference( old.begin(),     old.end(),
31                   current.begin(), current.end(),
32                   inserter( deleted, deleted.begin() ),
33                   cmp );
34
35   set_union(      current.begin(), current.end(),
36                   old.begin(),     old.end(),
37                   inserter( common, common.begin() ),
38                   cmp );
39 }
40
41 int main() {
42   // Parse the list of current files on stdin
43   file_set current;
44   populate_set( cin, current );
45
46   file_set backed_up;
47   ifstream db( "test.db" );
48   populate_set( db, backed_up);
49
50   // Now divide the two sets into three sets (added, deleted and common )
51   file_set added, deleted, common;
52   partition_sets( current, backed_up, added, common, deleted );
53
54   { // This little block will copy the last_backup_date from the second set to the first
55     FileDataNameCmp cmp;
56
57     file_set updated_mirror;
58     set_union( current.begin(),   current.end(),
59                backed_up.begin(), backed_up.end(),
60                inserter( updated_mirror, updated_mirror.begin() ),
61                cmp );
62
63     // TODO Now we need to copy the last_backup_date from 
64     file_set::iterator i = common.begin(), j = updated_mirror.begin();
65     for( ; i != common.end(); ++i, ++j ) {
66       (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
67     }
68   }
69
70   // Now find the list of files to backup.
71   file_set backup_set;
72
73   // backup all added files
74   copy( added.begin(), added.end(), inserter( backup_set, backup_set.begin() ) );
75
76   // backup common files that have changed since the last backup date.
77   for( file_set::iterator i = common.begin(); i != common.end(); ++i ) {
78     if( (*i)->getLastBackupDate() < (*i)->getModifiedDate() ) {
79       backup_set.insert( *i );
80     }
81   }
82
83   // Now, sort the backup_set by filesize and build a list of up to SIZE
84   file_vector backups_bysize;
85   copy( backup_set.begin(), backup_set.end(), back_inserter( backups_bysize ) );
86   FileDataSizeCmp sizecmp;
87   sort( backups_bysize.begin(), backups_bysize.end(), sizecmp );
88
89   file_set final_set;
90   unsigned long long bytes_available = 4700000000ULL;  // 4.3 GBytes
91
92   unsigned long long block_size = 512ULL;
93
94   bool complete = true;
95
96   // Copy files over until full or out of files
97   file_vector::reverse_iterator i = backups_bysize.rbegin();
98   while( 0 != bytes_available && i != backups_bysize.rend() ) {
99     unsigned long long size = (*i)->getFileSize();
100     unsigned long long blocks = size & ( ~(block_size-1) );
101     if( blocks < size ) blocks += block_size;
102     if( blocks <= bytes_available ) {
103       bytes_available -= blocks;
104       final_set.insert( *i );
105     } else {
106       // We missed a file that should be included so the backup is not complete
107       complete = false;
108     }
109     ++i;
110   }
111
112   // Now, sort the non-backed-up list by last_backup_date and back-fill
113   if( 0 != bytes_available ) {
114     file_vector leftovers;
115     FileDataNameCmp cmp;
116     set_difference( current.begin(),   current.end(),
117                     final_set.begin(), final_set.end(),
118                     back_inserter( leftovers ),
119                     cmp );
120
121     FileDataLastBackupCmp lastbackupcmp;
122     sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
123
124     // Copy files over until full or out of files
125     file_vector::const_iterator j = leftovers.begin();
126     while( 0 != bytes_available && j != leftovers.end() ) {
127       unsigned long long size   = (*j)->getFileSize();
128       unsigned long long blocks = size & ( ~(block_size-1) );
129
130       if( blocks < size ) blocks += block_size;
131
132       if( blocks <= bytes_available ) {
133         bytes_available -= blocks;
134         final_set.insert( *j );
135       }
136       ++j;
137     }
138   }
139
140   // TODO Get 'now' from time clock
141   unsigned long long now = 20051019211200ULL;
142   for( file_set::iterator k = final_set.begin(); k != final_set.end(); ++k ) {
143     (*k)->setLastBackupDate( now );
144   }
145
146   // Write the 'current' list to the dbfile
147   ofstream dbout( "test.db" );
148   copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout, "" ) );
149
150   // Write the 'final_set' list to stdout
151   copy( final_set.begin(), final_set.end(), ostream_iterator<FileData*>( cout, "" ) );
152
153   // If ! complete then write a flag to /tmp
154   if( ! complete ) {
155     cerr << "incomplete" << endl;
156   }
157
158   // Clean-up
159   for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }
160   for( file_set::iterator i = current.begin();   i != current.end();   ++i ) { delete *i; }
161 }