]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- started with actual rollback implementation
authorArvin Schnell <aschnell@suse.de>
Mon, 14 Feb 2011 17:34:25 +0000 (18:34 +0100)
committerArvin Schnell <aschnell@suse.de>
Mon, 14 Feb 2011 17:34:25 +0000 (18:34 +0100)
snapper/AppUtil.cc
snapper/AppUtil.h
snapper/File.cc
snapper/File.h
snapper/Snapper.cc
snapper/Snapper.h
snapper/SnapperDefines.h
tools/snapper.cc

index 40242081550fbc70b79cb1469f7a0fad12ce8a27..21a84b73086fb73744835efc8dbc3c4d4f517a6e 100644 (file)
@@ -93,6 +93,13 @@ setStatMode(const string& Path_Cv, mode_t val )
 }
 
 
+    bool
+    getLStat(const string& path, struct stat& fs)
+    {
+       return lstat(path.c_str(), &fs) == 0;
+    }
+
+
 bool
 checkNormalFile(const string& Path_Cv)
 {
index dcf5a50808c50ff7f26baa3d0ccc049009d7acf5..6b0661fff8df8c4a8529ef48152998d10d71ea5f 100644 (file)
@@ -46,6 +46,8 @@ bool checkDir(const string& Path_Cv);
 bool getStatMode(const string& Path_Cv, mode_t& val );
 bool setStatMode(const string& Path_Cv, mode_t val );
 
+    bool getLStat(const string& path, struct stat& fs);
+
     list<string> glob(const string& path, int flags);
 
     bool readlink(const string& path, string& buf);
index 0d4bf4ef037499f7cd14c2ae9fd03fd0bccd3011..e055d43d4492d939617a44ffda3d5e2ec2d67d78 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/types.h>
 #include <glob.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "snapper/File.h"
 #include "snapper/Snapper.h"
 namespace snapper
 {
 
+    std::ostream& operator<<(std::ostream& s, const RollbackStatistic& rs)
+    {
+       s << "numCreate:" << rs.numCreate
+         << " numModify:" << rs.numModify
+         << " numDelete:" << rs.numDelete;
+
+       return s;
+    }
+
+
+    RollbackStatistic::RollbackStatistic()
+       : numCreate(0), numModify(0), numDelete(0)
+    {
+    }
+
+
+    bool
+    RollbackStatistic::empty() const
+    {
+       return numCreate == 0 && numModify == 0 && numDelete == 0;
+    }
+
+
     std::ostream& operator<<(std::ostream& s, const File& file)
     {
        s << "name:\"" << file.name << "\"";
@@ -279,20 +303,76 @@ namespace snapper
        if (getPreToPostStatus() == CREATED)
        {
            cout << "delete " << name << endl;
+
+           struct stat fs;
+           getLStat(getAbsolutePath(LOC_POST), fs);
+
+           if (S_ISREG(fs.st_mode) || S_ISLNK(fs.st_mode))
+           {
+               unlink(getAbsolutePath(LOC_SYSTEM).c_str());
+           }
+           else if (S_ISDIR(fs.st_mode))
+           {
+               rmdir(getAbsolutePath(LOC_SYSTEM).c_str());
+           }
        }
        else if (getPreToPostStatus() == DELETED)
        {
            cout << "create " << name << endl;
+
+           struct stat fs;
+           getLStat(getAbsolutePath(LOC_PRE), fs);
+
+           if (S_ISREG(fs.st_mode) || S_ISLNK(fs.st_mode))
+           {
+               SystemCmd cmd(CPBIN " --no-dereference --preserve=mode,ownership,links " +
+                             getAbsolutePath(LOC_PRE) + " " + getAbsolutePath(LOC_SYSTEM));
+           }
+           else if (S_ISDIR(fs.st_mode))
+           {
+               mkdir(getAbsolutePath(LOC_SYSTEM).c_str(), 0777);
+           }
        }
        else
        {
            cout << "modify " << name << endl;
+
+           struct stat fs;
+           getLStat(getAbsolutePath(LOC_PRE), fs);
+
+           if (S_ISREG(fs.st_mode) || S_ISLNK(fs.st_mode))
+           {
+               SystemCmd cmd(CPBIN " --no-dereference --preserve=mode,ownership,links " +
+                             getAbsolutePath(LOC_PRE) + " " + getAbsolutePath(LOC_SYSTEM));
+           }
        }
 
        return true;
     }
 
 
+    RollbackStatistic
+    Files::getRollbackStatistic() const
+    {
+       RollbackStatistic rs;
+
+       for (vector<File>::const_iterator it = entries.begin(); it != entries.end(); ++it)
+       {
+           if (it->getRollback())
+           {
+               if (it->getPreToPostStatus() == CREATED)
+                   rs.numDelete++;
+               else if (it->getPreToPostStatus() == DELETED)
+                   rs.numCreate++;
+               else
+                   rs.numModify++;
+           }
+       }
+
+       return rs;
+    }
+
+
     bool
     Files::doRollback()
     {
index b65ac65c27cae95a46651f75cd73b8679ffe8dc7..fe0859c2cbbc08822cc119e56450d2885f81e631 100644 (file)
@@ -54,6 +54,20 @@ namespace snapper
     };
 
 
+    struct RollbackStatistic
+    {
+       RollbackStatistic();
+
+       bool empty() const;
+
+       unsigned int numCreate;
+       unsigned int numModify;
+       unsigned int numDelete;
+
+       friend std::ostream& operator<<(std::ostream& s, const RollbackStatistic& rs);
+    };
+
+
     class File
     {
     public:
@@ -129,6 +143,8 @@ namespace snapper
        bool load();
        bool save();
 
+       RollbackStatistic getRollbackStatistic() const;
+
        bool doRollback();
 
        const Snapper* snapper;
index d3fbe53bed3bb13b333469e0541c07606c004f5c..3ab585bb2ff029b17ec0b5b91334282ea1a1bb8d 100644 (file)
@@ -142,6 +142,13 @@ namespace snapper
     }
 
 
+    RollbackStatistic
+    Snapper::getRollbackStatistic() const
+    {
+       return files.getRollbackStatistic();
+    }
+
+
     bool
     Snapper::doRollback()
     {
index 5518f54079bed30d62b7ae00a3c9a1eb276ccc4f..57c13533aea64ee539e89c826704af65d8cdff0a 100644 (file)
@@ -72,6 +72,8 @@ namespace snapper
        Files& getFiles() { return files; }
        const Files& getFiles() const { return files; }
 
+       RollbackStatistic getRollbackStatistic() const;
+
        bool doRollback();
 
        void setCompareCallback(CompareCallback* p) { compare_callback = p; }
index fe4cdadf13cbd4b56f48f90ad25bc612b43d389e..fd6ded3c658f5f6e20ae3753f296ee12e7d151a8 100644 (file)
@@ -33,5 +33,7 @@
 
 #define COMPAREDIRSBIN "/usr/lib/snapper/bin/compare-dirs"
 
+#define CPBIN "/bin/cp"
+
 
 #endif
index 5a524e901b9f118018dff48ad5fc41e2b240a2bd..67017c75ffe59567a5c1f0b6982a8ebec8674518 100644 (file)
@@ -368,6 +368,17 @@ command_rollback()
            it->setRollback(true);
     }
 
+    RollbackStatistic rs = sh->getRollbackStatistic();
+
+    if (rs.empty())
+    {
+       cout << "nothing to do" << endl;
+       return;
+    }
+
+    cout << "create:" << rs.numCreate << " modify:" << rs.numModify << " delete:" << rs.numDelete
+        << endl;
+
     sh->doRollback();
 }