9 #include "filedata.hpp"
13 vector<string> split( const string &line, char c, int limit = -1 ) {
14 string::size_type start = 0, end = 0;
17 while( 0 != limit-- && end != line.size() ) {
21 end = line.find( c, start );
22 if( end == string::npos ) {
26 out.push_back( line.substr( start, end-start ) );
32 // Callback function for getting files from the database
33 int populate_set( void *files_v, int, char **vals, char ** ) {
34 file_set *files = reinterpret_cast<file_set*>( files_v );
35 files->insert( new FileData( vals[0][0],
50 // Parse the list of files on stdin
53 for( int c = cin.get(); 0 != c && ! cin.eof(); c = cin.get() ) {
54 file_string.push_back( c );
56 if( 0 != file_string.size() ) {
58 // type perms user group size datemodified name (7 total)
59 // f 0600 cnb cnb 424 20051015205340 ./.git/index
60 vector<string> vals = split( file_string, ' ', 7 );
61 current.insert( new FileData( vals[0][0],
65 atoi( vals[4].c_str() ),
66 atoi( vals[5].c_str() ),
69 } while( ! cin.eof() );
71 // Get the list of previously backed up files from the database.
74 const char *dbname = "test.db";
75 int rc = sqlite3_open( dbname, &db );
76 assert( SQLITE_OK == rc );
78 char *sqliteErrMsg = 0;
80 rc = sqlite3_exec( db, "select * from filedata;", populate_set, &backed_up, &sqliteErrMsg );
81 assert( SQLITE_OK == rc );
83 rc = sqlite3_close( db );
84 assert( SQLITE_OK == rc );
86 // Now divide the two sets into three sets (new, deleted and updated )
90 set_difference( current.begin(), current.end(),
91 backed_up.begin(), backed_up.end(),
92 inserter( added, added.begin() ),
96 set_difference( backed_up.begin(), backed_up.end(),
97 current.begin(), current.end(),
98 inserter( deleted, deleted.begin() ),
102 set_union( current.begin(), current.end(),
103 backed_up.begin(), backed_up.end(),
104 inserter( updated, updated.begin() ),
107 { // This little block will copy the last_backup_date from the second set to the first
108 file_set updated_mirror;
109 set_union( current.begin(), current.end(),
110 backed_up.begin(), backed_up.end(),
111 inserter( updated_mirror, updated_mirror.begin() ),
114 // TODO Now we need to copy the last_backup_date from
115 file_set::iterator i = updated.begin(), j = updated_mirror.begin();
116 for( ; i != updated.end(); ++i, ++j ) {
117 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
121 // Now find the list of files to backup.
122 file_set backup_list;
124 // backup all new files
125 copy( added.begin(), added.end(), inserter( backup_list, backup_list.begin() ) );
127 // backup already backed-up files that have changed since the last backup date.
128 for( file_set::iterator i = updated.begin(); i != updated.end(); ++i ) {
129 if( (*i)->getLastBackupDate() < (*i)->getModifiedDate() ) {
130 backup_list.insert( *i );
134 // Now, sort the backup_list by filesize and build a list of up to SIZE
136 // Now, sort the non-backed-up list my last_backup_date and back-fill
138 // Remove deleted files from the database.
139 // TODO CNB You were working in here. Actually, just delete all records in
140 // the database and re-populate with the current list.
141 const char *delete_sql = "delete from filedata where filename = :filename";
142 sqlite3_stmt *ppStmt;
144 sqlite3_prepare( db, delete_sql, -1, &ppStmt, NULL );
145 assert( NULL != ppStmt );
147 for( file_set::iterator i = deleted.begin(); i != deleted.end(); ++i ) {
148 const string &name = (*i)->getFileName();
150 rc = sqlite3_bind_text( ppStmt, 1, name.data(), name.size(), SQLITE_TRANSIENT );
151 assert( SQLITE_OK == rc );
153 rc = sqlite3_step( ppStmt );
154 assert( SQLITE_OK == rc );
156 rc = sqlite3_finalize( ppStmt );
157 assert( SQLITE_OK == rc );
159 // Now, use the current set to update values in the database. You should copy
160 // last_backup_date dates from the backed_up set first. This will get all of
161 // the latest permissions but include the last_backup_date.
163 // Now, update the last_backup_date for all of the files that are in the list
166 for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }
167 for( file_set::iterator i = current.begin(); i != current.end(); ++i ) { delete *i; }