Routine commit
[backups/.git] / main.cc
1 #include <iostream>
2 #include <iterator>
3 #include <vector>
4 #include <algorithm>
5
6 #include <sqlite3.h>
7
8 #include "filedata.hpp"
9
10 using namespace std;
11
12 vector<string> split( const string &line, char c, int limit = -1 ) {
13   string::size_type start = 0, end = 0;
14
15   vector<string> out;
16   while( 0 != limit-- && end != line.size() ) {
17     if( 0 == limit ) {
18       end = line.size();
19     } else {
20       end = line.find( c, start );
21       if( end == string::npos ) {
22         end = line.size();
23       }
24     }
25     out.push_back( line.substr( start, end-start ) );
26     start = end + 1;
27   }
28   return out;
29 }
30
31 // Callback function for getting files from the database
32 int populate_set( void *files_v, int, char **vals, char ** ) {
33   file_set *files = reinterpret_cast<file_set*>( files_v );
34   files->insert( new FileData( vals[0][0],
35         vals[1],
36         vals[2],
37         vals[3],
38         atoi( vals[4] ),
39         atoi( vals[5] ),
40         vals[6]) );
41   return 0;
42 }
43
44 int main() {
45   string file_string;
46
47   file_set current;
48
49   // Parse the list of files on stdin
50   do {
51     file_string.clear();
52     for( int c = cin.get(); 0 != c && ! cin.eof(); c = cin.get() ) {
53       file_string.push_back( c );
54     }
55     if( 0 != file_string.size() ) {
56       // Example entry
57       // type perms user group size datemodified name (7 total)
58       // f 0600 cnb cnb 424 20051015205340 ./.git/index
59       vector<string> vals = split( file_string, ' ', 7 );
60       current.insert( new FileData( vals[0][0],
61             vals[1],
62             vals[2],
63             vals[3],
64             atoi( vals[4].c_str() ),
65             atoi( vals[5].c_str() ),
66             vals[6]) );
67     }
68   } while( ! cin.eof() );
69
70   // Get the list of previously backed up files from the database.
71   sqlite3 *db;
72
73   const char *dbname = "test.db";
74   int rc = sqlite3_open( dbname, &db );
75   assert( SQLITE_OK == rc );
76
77   char *sqliteErrMsg = 0;
78   file_set backed_up;
79   rc = sqlite3_exec( db, "select * from filedata;", populate_set, &backed_up, &sqliteErrMsg );
80   assert( SQLITE_OK == rc );
81
82   rc = sqlite3_close( db );
83   assert( SQLITE_OK == rc );
84
85   // Now divide the two sets into three sets (new, deleted and updated )
86   FileDataPtrCmp cmp;
87
88   file_set new_set;
89   set_difference( current.begin(),  current.end(),
90                   backed_up.begin(), backed_up.end(),
91                   inserter( new_set, new_set.begin() ),
92                   cmp );
93
94   file_set deleted;
95   set_difference( backed_up.begin(), backed_up.end(),
96                   current.begin(),  current.end(),
97                   inserter( deleted, deleted.begin() ),
98                   cmp );
99
100   // backed_up should *definitely* be the first set here
101   file_set updated;
102   set_difference( backed_up.begin(), backed_up.end(),
103                   current.begin(),  current.end(),
104                   inserter( updated, updated.begin() ),
105                   cmp );
106
107   // Now find the list of files to backup.
108   file_set backup_list;
109
110   // backup all new files
111   copy( new_set.begin(), new_set.end(), inserter( backup_list, backup_list.begin() ) );
112
113   // backup already backed-up files that have changed since the last backup date.
114   for( file_set::iterator i = updated.begin(); i != updated.end(); ++i ) {
115     if( (*i)->getLastBackupDate() < (*i)->getModifiedDate() ) {
116       backup_list.insert( *i );
117     }
118   }
119
120   // Now, sort the backup_list by filesize and build a list of up to SIZE
121
122   // Now, sort the non-backed-up list my last_backup_date and back-fill
123
124   // Remove deleted files from the database.
125   const char *delete_sql = "delete from filedata where filename = :filename";
126   sqlite3_stmt *ppStmt;
127
128   sqlite3_prepare( db, delete_sql, -1, &ppStmt, NULL );
129   assert( NULL != ppStmt );
130
131   for( file_set::iterator i = deleted.begin(); i != deleted.end(); ++i ) {
132     const string &name = (*i)->getFileName();
133
134     rc = sqlite3_bind_text( ppStmt, 1, name.data(), name.size(), SQLITE_TRANSIENT );
135     assert( SQLITE_OK == rc );
136
137     rc = sqlite3_step( ppStmt );
138     assert( SQLITE_OK == rc );
139   }
140   rc = sqlite3_finalize( ppStmt );
141   assert( SQLITE_OK == rc );
142
143   // Now, use the current set to update values in the database.  You should copy
144   // last_backup_date dates from the backed_up set first.  This will get all of
145   // the latest permissions but include the last_backup_date.
146
147   // Now, update the last_backup_date for all of the files that are in the list
148
149   // Clean-up
150   for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }
151   for( file_set::iterator i = current.begin();   i != current.end();   ++i ) { delete *i; }
152 }