#include <string>
#include <set>
+#include <vector>
class FileData {
public:
std::string,
std::string,
std::string,
- unsigned long,
+ unsigned long long,
unsigned long long,
std::string,
unsigned long long = 0
const std::string &getPermissions() const { return permissions; }
const std::string &getUserName() const { return username; }
const std::string &getGroupName() const { return groupname; }
- unsigned long getFileSize() const { return filesize; }
+ unsigned long long getFileSize() const { return filesize; }
unsigned long long getModifiedDate() const { return modified_date; }
const std::string &getFileName() const { return filename; }
unsigned long long getLastBackupDate() const { return last_backup_date; }
void setPermissions( const std::string &arg ) { permissions = arg; }
void setUserName( const std::string &arg ) { username = arg; }
void setGroupName( const std::string &arg ) { groupname = arg; }
- void setFileSize( unsigned long arg ) { filesize = arg; }
+ void setFileSize( unsigned long long arg ) { filesize = arg; }
void setModifiedDate( unsigned long long arg ) { modified_date = arg; }
void setFileName( const std::string &arg ) { filename = arg; }
void setLastBackupDate( unsigned long long arg ) { last_backup_date = arg; }
std::string permissions;
std::string username;
std::string groupname;
- unsigned long filesize;
+ unsigned long long filesize;
unsigned long long modified_date;
std::string filename;
unsigned long long last_backup_date;
};
-class FileDataPtrCmp {
- public:
+struct FileDataLastBackupCmp {
+ bool operator()( const FileData *a, const FileData *b ) {
+ return a->getLastBackupDate() < b->getLastBackupDate();
+ }
+};
+
+struct FileDataSizeCmp {
+ bool operator()( const FileData *a, const FileData *b ) {
+ return a->getFileSize() < b->getFileSize();
+ }
+};
+struct FileDataNameCmp {
bool operator()( const FileData *a, const FileData *b ) {
return cmp( a->getFileName(), b->getFileName() );
}
private:
- std::less<std::string> cmp;
+ std::less<std::string> cmp;
};
-typedef std::set<FileData*,FileDataPtrCmp> file_set;
+typedef std::set<FileData*,FileDataNameCmp> file_set;
+typedef std::vector<FileData*> file_vector;
#endif
#include <iostream>
#include <iterator>
-#include <vector>
#include <algorithm>
#include <cassert>
assert( SQLITE_OK == rc );
// Now divide the two sets into three sets (new, deleted and updated )
- FileDataPtrCmp cmp;
+ FileDataNameCmp cmp;
file_set added;
set_difference( current.begin(), current.end(),
}
// Now find the list of files to backup.
- file_set backup_list;
+ file_set backup_set;
// backup all new files
- copy( added.begin(), added.end(), inserter( backup_list, backup_list.begin() ) );
+ copy( added.begin(), added.end(), inserter( backup_set, backup_set.begin() ) );
// backup already backed-up files that have changed since the last backup date.
for( file_set::iterator i = updated.begin(); i != updated.end(); ++i ) {
if( (*i)->getLastBackupDate() < (*i)->getModifiedDate() ) {
- backup_list.insert( *i );
+ backup_set.insert( *i );
}
}
- // Now, sort the backup_list by filesize and build a list of up to SIZE
-
- // Now, sort the non-backed-up list my last_backup_date and back-fill
+ // Now, sort the backup_set by filesize and build a list of up to SIZE
+ file_vector backups_bysize;
+ copy( backup_set.begin(), backup_set.end(), back_inserter( backups_bysize ) );
+ FileDataSizeCmp sizecmp;
+ sort( backups_bysize.begin(), backups_bysize.end(), sizecmp );
+
+ file_set final_set;
+ unsigned long long bytes_available = 4700000000ULL; // 4.3 GBytes
+
+ unsigned long long block_size = 512ULL;
+
+ // Copy files over until full or out of files
+ file_vector::reverse_iterator i = backups_bysize.rbegin();
+ while( 0 != bytes_available && i != backups_bysize.rend() ) {
+ unsigned long long size = (*i)->getFileSize();
+ unsigned long long blocks = size & ( ~(block_size-1) );
+ if( blocks < size ) blocks += block_size;
+ if( blocks <= bytes_available ) {
+ bytes_available -= blocks;
+ final_set.insert( *i );
+ }
+ ++i;
+ }
- // Remove deleted files from the database.
- // TODO CNB You were working in here. Actually, just delete all records in
- // the database and re-populate with the current list.
- const char *delete_sql = "delete from filedata where filename = :filename";
- sqlite3_stmt *ppStmt;
+ // Now, sort the non-backed-up list by last_backup_date and back-fill
+ if( 0 != bytes_available ) {
+ file_vector leftovers;
+ set_difference( current.begin(), current.end(),
+ final_set.begin(), final_set.end(),
+ back_inserter( leftovers ),
+ cmp );
- sqlite3_prepare( db, delete_sql, -1, &ppStmt, NULL );
- assert( NULL != ppStmt );
+ FileDataLastBackupCmp lastbackupcmp;
+ sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
- for( file_set::iterator i = deleted.begin(); i != deleted.end(); ++i ) {
- const string &name = (*i)->getFileName();
+ // Copy files over until full or out of files
+ file_vector::const_iterator j = leftovers.begin();
+ while( 0 != bytes_available && j != leftovers.end() ) {
+ unsigned long long size = (*j)->getFileSize();
+ unsigned long long blocks = size & ( ~(block_size-1) );
- rc = sqlite3_bind_text( ppStmt, 1, name.data(), name.size(), SQLITE_TRANSIENT );
- assert( SQLITE_OK == rc );
+ if( blocks < size ) blocks += block_size;
- rc = sqlite3_step( ppStmt );
- assert( SQLITE_OK == rc );
+ if( blocks <= bytes_available ) {
+ bytes_available -= blocks;
+ final_set.insert( *j );
+ }
+ ++j;
+ }
}
- rc = sqlite3_finalize( ppStmt );
- assert( SQLITE_OK == rc );
- // Now, use the current set to update values in the database. You should copy
- // last_backup_date dates from the backed_up set first. This will get all of
- // the latest permissions but include the last_backup_date.
+ unsigned long long now = 20051019211200ULL;
+ for( file_set::iterator k = final_set.begin(); k != final_set.end(); ++k ) {
+ (*k)->setLastBackupDate( now );
+ }
- // Now, update the last_backup_date for all of the files that are in the list
+ // Clean out the database
+ rc = sqlite3_exec( db, "delete from filedata;", NULL, NULL, &sqliteErrMsg );
+ assert( SQLITE_OK == rc );
+ rc = sqlite3_exec( db, "vacuum;", NULL, NULL, &sqliteErrMsg );
+ assert( SQLITE_OK == rc );
// Clean-up
for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }