]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- work on exceptions
authorArvin Schnell <aschnell@suse.de>
Wed, 20 Apr 2011 08:31:41 +0000 (10:31 +0200)
committerArvin Schnell <aschnell@suse.de>
Wed, 20 Apr 2011 08:31:41 +0000 (10:31 +0200)
snapper/AsciiFile.cc
snapper/AsciiFile.h
snapper/Comparison.cc
snapper/Exception.h [new file with mode: 0644]
snapper/File.cc
snapper/Makefile.am
snapper/Snapper.cc
snapper/Snapshot.cc
snapper/Snapshot.h

index bfd4dd7ae22b9f90cca52cc0c0c6a30c77214c1f..ac261d78d6fb4953076a7065cc17606edd387419 100644 (file)
@@ -27,6 +27,7 @@
 #include "snapper/AppUtil.h"
 #include "snapper/AsciiFile.h"
 #include "snapper/SnapperTypes.h"
+#include "snapper/Exception.h"
 
 
 namespace snapper
@@ -40,7 +41,7 @@ namespace snapper
        if (file == NULL)
        {
            y2err("file is NULL");
-           throw exception();  // TODO
+           throw FileNotFoundException();
        }
     }
 
@@ -52,7 +53,7 @@ namespace snapper
        if (file == NULL)
        {
            y2err("open for '" << filename << "' failed");
-           throw exception();  // TODO
+           throw FileNotFoundException();
        }
     }
 
@@ -96,27 +97,18 @@ AsciiFile::AsciiFile(const string& Name_Cv, bool remove_empty)
 }
 
 
-bool
-AsciiFile::reload()
-{
-    y2mil("loading file " << Name_C);
-    clear();
-
-    try
+    void
+    AsciiFile::reload()
     {
+       y2mil("loading file " << Name_C);
+       clear();
+
        AsciiFileReader file(Name_C);
 
        string line;
        while (file.getline(line))
            Lines_C.push_back(line);
-
-       return true;
     }
-    catch (...)                        // TODO
-    {
-       return false;
-    }
-}
 
 
 bool
index 14683d50102d9644632a0007f037d11a54cb9a86..282069ac92e4dc1fdc75ad32b3b977d92d8ccd7b 100644 (file)
@@ -63,7 +63,7 @@ namespace snapper
 
        string name() const { return Name_C; }
 
-       bool reload();
+       void reload();
        bool save();
 
        void logContent() const;
index 7bfd80b527541c0040cf86fb7a105f44b00ff966..a19e79c3f48b08abc5b780776c50d8cabd362fe6 100644 (file)
  */
 
 
-#include <assert.h>
-
 #include "snapper/Comparison.h"
 #include "snapper/Snapper.h"
 #include "snapper/AppUtil.h"
 #include "snapper/File.h"
+#include "snapper/Exception.h"
 
 
 namespace snapper
@@ -35,9 +34,10 @@ namespace snapper
                           Snapshots::const_iterator snapshot2)
        : snapper(snapper), snapshot1(snapshot1), snapshot2(snapshot2), files(this)
     {
-       assert(snapshot1 != snapper->getSnapshots().end());
-       assert(snapshot2 != snapper->getSnapshots().end());
-       assert(snapshot1 != snapshot2);
+       if (snapshot1 == snapper->getSnapshots().end() ||
+           snapshot2 == snapper->getSnapshots().end() ||
+           snapshot1 == snapshot2)
+           throw IllegalSnapshotException();
 
        y2mil("num1:" << snapshot1->getNum() << " num2:" << snapshot2->getNum());
 
diff --git a/snapper/Exception.h b/snapper/Exception.h
new file mode 100644 (file)
index 0000000..5ffed07
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2011 Novell, Inc.
+ *
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may
+ * find current contact information at www.novell.com.
+ */
+
+
+#ifndef EXCEPTION_H
+#define EXCEPTION_H
+
+
+#include <exception>
+
+
+namespace snapper
+{
+
+    struct FileNotFoundException : public std::exception
+    {
+       explicit FileNotFoundException() throw() {}
+       virtual const char* what() const throw() { return "file not found"; }
+    };
+
+
+    struct IllegalSnapshotException : public std::exception
+    {
+       explicit IllegalSnapshotException() throw() {}
+       virtual const char* what() const throw() { return "illegal snapshot"; }
+    };
+
+}
+
+
+#endif
index 84036f0afe378801316bbb04d1a9d79a479e2ed4..a9d2cee0d4a792d25ad5350ab9587ed3a6d9830f 100644 (file)
@@ -39,6 +39,7 @@
 #include "snapper/SnapperDefines.h"
 #include "snapper/Compare.h"
 #include "snapper/AsciiFile.h"
+#include "snapper/Exception.h"
 
 
 namespace snapper
@@ -143,7 +144,8 @@ namespace snapper
        y2mil("num1:" << comparison->getSnapshot1()->getNum() << " num2:" <<
              comparison->getSnapshot2()->getNum());
 
-       assert(!comparison->getSnapshot1()->isCurrent() && !comparison->getSnapshot2()->isCurrent());
+       if (comparison->getSnapshot1()->isCurrent() || comparison->getSnapshot2()->isCurrent())
+           throw IllegalSnapshotException();
 
        unsigned int num1 = comparison->getSnapshot1()->getNum();
        unsigned int num2 = comparison->getSnapshot2()->getNum();
@@ -183,10 +185,12 @@ namespace snapper
 
            return true;
        }
-       catch (...)             // TODO
+       catch (const FileNotFoundException& e)
        {
            return false;
        }
+
+       return true;
     }
 
 
@@ -195,7 +199,8 @@ namespace snapper
     {
        y2mil("num1:" << comparison->getSnapshot1()->getNum() << " num2:" << comparison->getSnapshot2()->getNum());
 
-       assert(!comparison->getSnapshot1()->isCurrent() && !comparison->getSnapshot2()->isCurrent());
+       if (comparison->getSnapshot1()->isCurrent() || comparison->getSnapshot2()->isCurrent())
+           throw IllegalSnapshotException();
 
        unsigned int num1 = comparison->getSnapshot1()->getNum();
        unsigned int num2 = comparison->getSnapshot2()->getNum();
index a977bd16d409bc123e414146dacce28f95fd352f..2b3516de6cbd22ac42b3835ebf025f39e9e44697 100644 (file)
@@ -19,6 +19,7 @@ libsnapper_la_SOURCES =                                       \
        SystemCmd.cc            SystemCmd.h             \
        AsciiFile.cc            AsciiFile.h             \
        Regex.cc                Regex.h                 \
+       Exception.h                                     \
        SnapperTmpl.h                                   \
        SnapperTypes.h                                  \
        SnapperDefines.h
@@ -32,6 +33,7 @@ pkginclude_HEADERS =                                  \
        Factory.h                                       \
        Snapper.h                                       \
        Snapshot.h                                      \
+       File.h                                          \
        Comparison.h                                    \
-       File.h
+       Exception.h
 
index 708827763d57dc670d2164556c140ae2e0d615f8..1246019e568d1f001f398aff65dbcc68c886c56a 100644 (file)
@@ -36,6 +36,7 @@
 #include "snapper/SnapperDefines.h"
 #include "snapper/File.h"
 #include "snapper/AsciiFile.h"
+#include "snapper/Exception.h"
 
 
 namespace snapper
@@ -51,7 +52,6 @@ namespace snapper
        y2mil("libsnapper version " VERSION);
        y2mil("config_name:" << config_name);
 
-       // TODO error checking
        config = new SysconfigFile(CONFIGSDIR "/" + config_name);
 
        string val;
@@ -88,7 +88,7 @@ namespace snapper
                while (asciifile.getline(line))
                    ignore_patterns.push_back(line);
            }
-           catch (...)         // TODO
+           catch (const FileNotFoundException& e)
            {
            }
        }
@@ -141,8 +141,6 @@ namespace snapper
     Snapshots::iterator
     Snapper::createPostSnapshot(Snapshots::const_iterator pre)
     {
-       assert(pre != snapshots.end());
-
        return snapshots.createPostSnapshot(pre);
     }
 
@@ -150,8 +148,6 @@ namespace snapper
     void
     Snapper::deleteSnapshot(Snapshots::iterator snapshot)
     {
-       assert(snapshot != snapshots.end());
-
        snapshots.deleteSnapshot(snapshot);
     }
 
@@ -160,7 +156,11 @@ namespace snapper
     Snapper::startBackgroundComparsion(Snapshots::const_iterator snapshot1,
                                       Snapshots::const_iterator snapshot2)
     {
-       assert(snapshot1 != snapshots.end() && snapshot2 != snapshots.end());
+       if (snapshot1 == snapshots.end() || snapshot1->isCurrent())
+           throw IllegalSnapshotException();
+
+       if (snapshot2 == snapshots.end() || snapshot2->isCurrent())
+           throw IllegalSnapshotException();
 
        y2mil("num1:" << snapshot1->getNum() << " num2:" << snapshot2->getNum());
 
index 731f5a0295b6a23cf47c1de4338282e72a50ce2f..f560286d493adf276186ed883072aaa22a0ffce8 100644 (file)
@@ -34,6 +34,7 @@
 #include "snapper/SnapperTmpl.h"
 #include "snapper/SystemCmd.h"
 #include "snapper/SnapperDefines.h"
+#include "snapper/Exception.h"
 
 
 namespace snapper
@@ -232,8 +233,8 @@ namespace snapper
     Snapshots::iterator
     Snapshots::findPost(const_iterator pre)
     {
-       assert(pre != end());
-       assert(pre->getType() == PRE);
+       if (pre == entries.end() || pre->isCurrent() || pre->getType() != PRE)
+           throw IllegalSnapshotException();
 
        for (iterator it = begin(); it != end(); ++it)
        {
@@ -248,8 +249,8 @@ namespace snapper
     Snapshots::const_iterator
     Snapshots::findPost(const_iterator pre) const
     {
-       assert(pre != end());
-       assert(pre->getType() == PRE);
+       if (pre == entries.end() || pre->isCurrent() || pre->getType() != PRE)
+           throw IllegalSnapshotException();
 
        for (const_iterator it = begin(); it != end(); ++it)
        {
@@ -356,6 +357,9 @@ namespace snapper
     Snapshots::iterator
     Snapshots::createPostSnapshot(Snapshots::const_iterator pre)
     {
+       if (pre == entries.end() || pre->isCurrent() || pre->getType() != PRE)
+           throw IllegalSnapshotException();
+
        Snapshot snapshot(snapper);
        snapshot.type = POST;
        snapshot.num = nextNumber();
@@ -372,7 +376,8 @@ namespace snapper
     void
     Snapshots::deleteSnapshot(iterator snapshot)
     {
-       assert(!snapshot->isCurrent());
+       if (snapshot == entries.end() || snapshot->isCurrent())
+           throw IllegalSnapshotException();
 
        snapshot->deleteFilesystemSnapshot();
 
index 9a4d2365e58fa9984c790159e779fabe99737bb6..74c1a078eeba202bcb4da90574f5e9f58acc7755 100644 (file)
@@ -25,7 +25,6 @@
 
 
 #include <time.h>
-
 #include <string>
 #include <list>