From: Arvin Schnell Date: Thu, 3 Feb 2011 13:10:31 +0000 (+0100) Subject: - use list for snapshots since iterators are not invalidated on add/remove X-Git-Tag: v0.1.3~521 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=774a184b4097647aae0910281ef7a2f367b3e111;p=thirdparty%2Fsnapper.git - use list for snapshots since iterators are not invalidated on add/remove --- diff --git a/examples/List.cc b/examples/List.cc index d23e9dcb..ad668817 100644 --- a/examples/List.cc +++ b/examples/List.cc @@ -12,7 +12,7 @@ main(int argc, char** argv) { snapshots.assertInit(); - for (vector::const_iterator it = snapshots.begin(); it != snapshots.end(); ++it) + for (list::const_iterator it = snapshots.begin(); it != snapshots.end(); ++it) { cout << *it << endl; } diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 073ddf83..7f9339f2 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -40,49 +40,30 @@ namespace snapper using namespace std; -/* - void - listSnapshots() - { - assertInit(); - - for (list::const_iterator it = snapshots.begin(); - it != snapshots.end(); ++it) - { - cout << *it << endl; - } - } -*/ - - void - startBackgroundComparsion(unsigned int num1, unsigned int num2) + startBackgroundComparsion(list::const_iterator snapshot1, + list::const_iterator snapshot2) { - y2mil("num1:" << num1 << " num2:" << num2); + y2mil("num1:" << snapshot1->getNum() << " num2:" << snapshot2->getNum()); - string dir1 = SNAPSHOTSDIR "/" + decString(num1) + "/snapshot"; - string dir2 = SNAPSHOTSDIR "/" + decString(num2) + "/snapshot"; + string dir1 = snapshot1->snapshotDir(); + string dir2 = snapshot2->snapshotDir(); - string output = SNAPSHOTSDIR "/" + decString(num2) + "/filelist-" + decString(num1) + ".txt"; + string output = snapshot2->baseDir() + "/filelist-" + decString(snapshot1->getNum()) + + ".txt"; SystemCmd(COMPAREDIRSBIN " " + quote(dir1) + " " + quote(dir2) + " " + quote(output)); } bool - setComparisonNums(unsigned int num1, unsigned int num2) + setComparisonNums(list::const_iterator new_snapshot1, + list::const_iterator new_snapshot2) { - y2mil("num1:" << num1 << " num2:" << num2); - - snapshots.assertInit(); + y2mil("num1:" << new_snapshot1->getNum() << " num2:" << new_snapshot2->getNum()); - snapshot1 = snapshots.find(num1); - if (snapshot1 == snapshots.end()) - return false; - - snapshot2 = snapshots.find(num2); - if (snapshot2 == snapshots.end()) - return false; + snapshot1 = new_snapshot1; + snapshot2 = new_snapshot2; files.assertInit(); @@ -90,51 +71,6 @@ namespace snapper } -/* - list - getFiles() - { - filelist.assertInit(); - - list ret; - for (vector::const_iterator it = filelist.begin(); it != filelist.end(); ++it) - ret.push_back(it->name); - - return ret; - } -*/ - - - unsigned int - getStatus(const string& name, Cmp cmp) - { - vector::iterator it = files.find(name); - if (it != files.end()) - return it->getStatus(cmp); - - return -1; - } - - - void - setRollback(const string& name, bool rollback) - { - vector::iterator it = files.find(name); - if (it != files.end()) - it->setRollback(rollback); - } - - - bool - getRollback(const string& name, bool rollback) - { - vector::const_iterator it = files.find(name); - if (it != files.end()) - return it->getRollback(); - return false; - } - - CompareCallback* compare_callback = NULL; void diff --git a/snapper/SnapperInterface.h b/snapper/SnapperInterface.h index 978ab22b..0939ee44 100644 --- a/snapper/SnapperInterface.h +++ b/snapper/SnapperInterface.h @@ -25,11 +25,12 @@ #include +#include namespace snapper { - using std::string; + using namespace std; enum StatusFlags @@ -49,28 +50,13 @@ namespace snapper }; - // use num = 0 for current system + class Snapshot; - void startBackgroundComparsion(unsigned int num1, unsigned int num2); + void startBackgroundComparsion(list::const_iterator snapshot1, + list::const_iterator snapshot2); - bool setComparisonNums(unsigned int num1, unsigned int num2); - - unsigned int getComparisonNum1(); - unsigned int getComparisonNum2(); - - - // return bitfield of StatusFlags - unsigned int getStatus(const string& name, Cmp cmp); - - string getAbsolutePath(const string& name, Location loc); - - void setRollback(const string& name, bool rollback); - bool getRollback(const string& name); - - // check rollback? (e.g. to be deleted dirs are empty, required type changes) - bool checkRollback(); - - bool doRollback(); + bool setComparisonNums(list::const_iterator snapshot1, + list::const_iterator snapshot2); // progress callbacks, e.g. during snapshot comparision, during rollback, diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 928c4103..2cce33f5 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -39,8 +39,8 @@ namespace snapper using std::list; - vector::const_iterator snapshot1; - vector::const_iterator snapshot2; + list::const_iterator snapshot1; + list::const_iterator snapshot2; Snapshots snapshots; @@ -116,7 +116,7 @@ namespace snapper entries.push_back(snapshot); } - sort(entries.begin(), entries.end()); + entries.sort(); y2mil("found " << entries.size() << " snapshots"); } @@ -258,14 +258,14 @@ namespace snapper } - vector::iterator + list::iterator Snapshots::find(unsigned int num) { return lower_bound(entries.begin(), entries.end(), num, snapshot_num_less); } - vector::const_iterator + list::const_iterator Snapshots::find(unsigned int num) const { return lower_bound(entries.begin(), entries.end(), num, snapshot_num_less); diff --git a/snapper/Snapshot.h b/snapper/Snapshot.h index 2bf08278..4a65ced3 100644 --- a/snapper/Snapshot.h +++ b/snapper/Snapshot.h @@ -24,7 +24,7 @@ #define SNAPSHOT_H -#include +#include namespace snapper @@ -84,8 +84,8 @@ namespace snapper } - extern vector::const_iterator snapshot1; - extern vector::const_iterator snapshot2; + extern list::const_iterator snapshot1; + extern list::const_iterator snapshot2; class Snapshots @@ -96,11 +96,11 @@ namespace snapper void assertInit(); - vector::const_iterator begin() const { return entries.begin(); } - vector::const_iterator end() const { return entries.end(); } + list::const_iterator begin() const { return entries.begin(); } + list::const_iterator end() const { return entries.end(); } - vector::iterator find(unsigned int num); - vector::const_iterator find(unsigned int num) const; + list::iterator find(unsigned int num); + list::const_iterator find(unsigned int num) const; unsigned int createSingleSnapshot(string description); unsigned int createPreSnapshot(string description); @@ -116,7 +116,7 @@ namespace snapper bool initialized; - vector entries; + list entries; }; diff --git a/tools/snapper.cc b/tools/snapper.cc index 087584e6..efc2d58e 100644 --- a/tools/snapper.cc +++ b/tools/snapper.cc @@ -40,7 +40,7 @@ void listSnap( const list& args ) { snapshots.assertInit(); - for (vector::const_iterator it = snapshots.begin(); it != snapshots.end(); ++it) + for (list::const_iterator it = snapshots.begin(); it != snapshots.end(); ++it) { cout << *it << endl; } @@ -90,7 +90,7 @@ void createSnap( const list& args ) unsigned int number2 = snapshots.createPostSnapshot(number1); if (print_number) cout << number2 << endl; - startBackgroundComparsion(number1, number2); + startBackgroundComparsion(snapshots.find(number1), snapshots.find(number2)); } else y2war( "unknown type:\"" << type << "\"" ); @@ -123,7 +123,7 @@ void showDifference( const list& args ) readNums(args, num1, num2); - setComparisonNums(num1, num2); + setComparisonNums(snapshots.find(num1), snapshots.find(num2)); for (vector::const_iterator it = files.begin(); it != files.end(); ++it) cout << statusToString(it->getPreToPostStatus()) << " " << it->getName() << endl; @@ -137,7 +137,7 @@ void doRollback( const list& args ) readNums(args, num1, num2); - setComparisonNums(num1, num2); + setComparisonNums(snapshots.find(num1), snapshots.find(num2)); for (vector::iterator it = files.begin(); it != files.end(); ++it) it->setRollback(true); @@ -180,6 +180,8 @@ main(int argc, char** argv) setCompareCallback(&compare_callback_impl); + snapshots.assertInit(); + int cnt = optind; while( cnt