From: Arvin Schnell Date: Fri, 14 Jan 2011 16:00:55 +0000 (+0100) Subject: - restructured classes X-Git-Tag: v0.1.3~541 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d90c3cefab53db37ab1304fb00c237fe9d7381b1;p=thirdparty%2Fsnapper.git - restructured classes --- diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 85406871..5ab3d981 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -49,13 +49,7 @@ namespace snapper Snapshot snapshot2; - bool files_loaded = false; - - list files; - - map pre_to_post_status; - map pre_to_system_status; - map post_to_system_status; + Filelist filelist; std::ostream& operator<<(std::ostream& s, const Snapshot& x) @@ -74,12 +68,6 @@ namespace snapper } - bool operator<(const Snapshot& a, const Snapshot& b) - { - return a.num < b.num; - } - - string snapshotDir(const Snapshot& snapshot) { @@ -93,7 +81,7 @@ namespace snapper void readSnapshots() { - list infos = glob(SNAPSHOTSDIR "/*/snapshot.info", GLOB_NOSORT); + list infos = glob(SNAPSHOTSDIR "/*/info.xml", GLOB_NOSORT); for (list::const_iterator it = infos.begin(); it != infos.end(); ++it) { unsigned int num; @@ -166,7 +154,7 @@ namespace snapper } - const list& + list getSnapshots() { assertInit(); @@ -223,7 +211,7 @@ namespace snapper if (snapshot.type == POST) setChildValue(node, "pre_num", snapshot.pre_num); - xml.save(SNAPSHOTSDIR "/" + decString(snapshot.num) + "/snapshot.info"); + xml.save(SNAPSHOTSDIR "/" + decString(snapshot.num) + "/info.xml"); return true; } @@ -307,75 +295,91 @@ namespace snapper bool setComparisonNums(unsigned int num1, unsigned int num2) { - if (num1 == 0 || !getSnapshot(num1, snapshot1)) - return false; + y2mil("num1:" << num1 << " num2:" << num2); - if (num2 != 0 && !getSnapshot(num2, snapshot2)) + if (!getSnapshot(num1, snapshot1)) return false; - if (snapshot1.num != snapshot2.pre_num) + if (!getSnapshot(num2, snapshot2)) return false; + filelist.assertInit(); + return true; } void - log(const string& file, unsigned int status) + Filelist::append(const string& name, unsigned int status) { - files.push_back(file); - pre_to_post_status[file] = status; + files.push_back(File(name, status)); } void - createFilelist() + append_helper(const string& name, unsigned int status) + { + filelist.append(name, status); + } + + + void + Filelist::create() { y2mil("num1:" << snapshot1.num << " num2:" << snapshot2.num); files.clear(); - pre_to_post_status.clear(); - cmpDirs(snapshotDir(snapshot1), snapshotDir(snapshot2), log); + cmpDirs(snapshotDir(snapshot1), snapshotDir(snapshot2), append_helper); + + sort(files.begin(), files.end()); + + y2mil("found " << files.size() << " lines"); } bool - loadFilelist() + Filelist::load() { y2mil("num1:" << snapshot1.num << " num2:" << snapshot2.num); files.clear(); - pre_to_post_status.clear(); string input = SNAPSHOTSDIR "/" + decString(snapshot2.num) + "/filelist-" + decString(snapshot1.num) + ".txt"; FILE* file = fopen(input.c_str(), "r"); if (file == NULL) + { + y2mil("file not found"); return false; + } char* line = NULL; size_t len = 0; while (getline(&line, &len, file) != -1) { - string file = string(line, 5, strlen(line) - 6); + string name = string(line, 5, strlen(line) - 6); + File file(name, stringToStatus(string(line, 0, 4))); files.push_back(file); - pre_to_post_status[file] = stringToStatus(string(line, 0, 4)); } free(line); fclose(file); + sort(files.begin(), files.end()); + + y2mil("read " << files.size() << " lines"); + return true; } bool - saveFilelist() + Filelist::save() { y2mil("num1:" << snapshot1.num << " num2:" << snapshot2.num); @@ -390,8 +394,8 @@ namespace snapper FILE* file = fdopen(fd, "w"); - for (list::const_iterator it = files.begin(); it != files.end(); ++it) - fprintf(file, "%s %s\n", statusToString(getStatus(*it, CMP_PRE_TO_POST)).c_str(), it->c_str()); + for (vector::const_iterator it = files.begin(); it != files.end(); ++it) + fprintf(file, "%s %s\n", statusToString(it->pre_to_post_status).c_str(), it->name.c_str()); fclose(file); @@ -403,28 +407,81 @@ namespace snapper } - const list& - getFiles() + void + Filelist::assertInit() { - if (!files_loaded) - { - if (!loadFilelist()) - { - createFilelist(); - saveFilelist(); - } + if (!initialized) + initialize(); + } + - files_loaded = true; + void + Filelist::initialize() + { + if (initialized) + return; + + if (!load()) + { + create(); + save(); } - return files; + initialized = true; } - unsigned int - getStatus(const string& file, Cmp cmp) + list + getFiles() { - return pre_to_post_status[file]; + filelist.assertInit(); + + list ret; + for (vector::const_iterator it = filelist.begin(); it != filelist.end(); ++it) + ret.push_back(it->name); + + return ret; } + + inline bool + file_name_less(const File& file, const string& name) + { + return file.name < name; + } + + + vector::iterator + Filelist::find(const string& name) + { + return lower_bound(files.begin(), files.end(), name, file_name_less); + } + + + vector::const_iterator + Filelist::find(const string& name) const + { + return lower_bound(files.begin(), files.end(), name, file_name_less); + } + + + unsigned int + getStatus(const string& name, Cmp cmp) + { + vector::const_iterator it = filelist.find(name); + if (it != filelist.end()) + { + switch (cmp) + { + case CMP_PRE_TO_POST: + return it->pre_to_post_status; + case CMP_PRE_TO_SYSTEM: + return it->pre_to_system_status; + case CMP_POST_TO_SYSTEM: + return it->post_to_system_status; + } + } + + return -1; + } } diff --git a/snapper/Snapper.h b/snapper/Snapper.h index d0b7bcbb..73eb7ac5 100644 --- a/snapper/Snapper.h +++ b/snapper/Snapper.h @@ -24,6 +24,8 @@ #define SNAPPER_H +#include + #include "snapper/SnapperInterface.h" @@ -35,19 +37,76 @@ namespace snapper extern bool initialized; + inline bool operator<(const Snapshot& a, const Snapshot& b) + { + return a.num < b.num; + } + extern list snapshots; extern Snapshot snapshot1; extern Snapshot snapshot2; - extern list files; - extern map pre_to_post_status; - extern map pre_to_system_status; - extern map post_to_system_status; + class File + { + public: + + File(const string& name, unsigned int pre_to_post_status) + : name(name), pre_to_post_status(pre_to_post_status), pre_to_system_status(-1), + post_to_system_status(-1), rollback(false) + {} + + string name; + + unsigned int pre_to_post_status; // -1 if invalid + unsigned int pre_to_system_status; // -1 if invalid + unsigned int post_to_system_status; // -1 if invalid + + bool rollback; + + }; + + + inline int operator<(const File& a, const File& b) + { + return a.name < b.name; + } + + + class Filelist + { + public: + + Filelist() : initialized(false) {} + + void assertInit(); + + vector::const_iterator begin() const { return files.begin(); } + vector::const_iterator end() const { return files.end(); } + + vector::iterator find(const string& name); + vector::const_iterator find(const string& name) const; + + void append(const string& name, unsigned int status); // should be private + + private: + + void initialize(); + + void create(); + bool load(); + bool save(); + + bool initialized; + + vector files; + + }; + + extern Filelist filelist; - extern list files_to_rollback; }; diff --git a/snapper/SnapperInterface.h b/snapper/SnapperInterface.h index b6a5c446..3b996f11 100644 --- a/snapper/SnapperInterface.h +++ b/snapper/SnapperInterface.h @@ -60,7 +60,7 @@ namespace snapper bool getSnapshot(unsigned int num, Snapshot& snapshot); - const list& getSnapshots(); + list getSnapshots(); void listSnapshots(); // only for testing @@ -99,7 +99,7 @@ namespace snapper unsigned int getComparisonNum1(); unsigned int getComparisonNum2(); - const list& getFiles(); + list getFiles(); // return bitfield of StatusFlags unsigned int getStatus(const string& file, Cmp cmp); diff --git a/tools/snapper.cc b/tools/snapper.cc index 7acde7f9..6569165c 100644 --- a/tools/snapper.cc +++ b/tools/snapper.cc @@ -87,9 +87,9 @@ void showDifference( const list& args ) y2mil( "n1:" << n1 << " n2:" << n2 ); setComparisonNums(n1, n2); - const list& files = getFiles(); - for (list::const_iterator it = files.begin(); it != files.end(); ++it) - cout << statusToString(getStatus(*it, CMP_PRE_TO_POST)) << " " << *it << endl; + + for (vector::const_iterator it = filelist.begin(); it != filelist.end(); ++it) + cout << statusToString(it->pre_to_post_status) << " " << it->name << endl; } int