From: Carl N Baldwin Date: Thu, 20 Oct 2005 03:17:23 +0000 (-0600) Subject: Regular commit X-Git-Tag: release-0.4~97 X-Git-Url: http://git.pippins.net/embedvideo/.git/static/images/%7Blink_ppi%7D?a=commitdiff_plain;h=e2cc1e337cc5461d93a94931097ceb34e325c7ac;p=backups%2F.git Regular commit --- diff --git a/filedata.cpp b/filedata.cpp index c66dadc..cd82b54 100644 --- a/filedata.cpp +++ b/filedata.cpp @@ -8,7 +8,7 @@ FileData::FileData( char _type, string _permissions, string _user, string _group, - unsigned long _size, + unsigned long long _size, unsigned long long _modified_date, string _name, unsigned long long _last_backup ) diff --git a/filedata.hpp b/filedata.hpp index 8c2951b..3df81d9 100644 --- a/filedata.hpp +++ b/filedata.hpp @@ -3,6 +3,7 @@ #include #include +#include class FileData { public: @@ -11,7 +12,7 @@ class FileData { std::string, std::string, std::string, - unsigned long, + unsigned long long, unsigned long long, std::string, unsigned long long = 0 @@ -21,7 +22,7 @@ class FileData { 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; } @@ -30,7 +31,7 @@ class FileData { 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; } @@ -43,23 +44,34 @@ class FileData { 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 cmp; + std::less cmp; }; -typedef std::set file_set; +typedef std::set file_set; +typedef std::vector file_vector; #endif diff --git a/main.cc b/main.cc index 407d572..1a1b9e7 100644 --- a/main.cc +++ b/main.cc @@ -1,6 +1,5 @@ #include #include -#include #include #include @@ -84,7 +83,7 @@ int main() { 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(), @@ -119,48 +118,79 @@ int main() { } // 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; }