From: Arvin Schnell Date: Wed, 20 Apr 2011 08:31:41 +0000 (+0200) Subject: - work on exceptions X-Git-Tag: v0.1.3~411 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=47e4949dd510f19c19ec077a93d0206adbb6e280;p=thirdparty%2Fsnapper.git - work on exceptions --- diff --git a/snapper/AsciiFile.cc b/snapper/AsciiFile.cc index bfd4dd7a..ac261d78 100644 --- a/snapper/AsciiFile.cc +++ b/snapper/AsciiFile.cc @@ -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 diff --git a/snapper/AsciiFile.h b/snapper/AsciiFile.h index 14683d50..282069ac 100644 --- a/snapper/AsciiFile.h +++ b/snapper/AsciiFile.h @@ -63,7 +63,7 @@ namespace snapper string name() const { return Name_C; } - bool reload(); + void reload(); bool save(); void logContent() const; diff --git a/snapper/Comparison.cc b/snapper/Comparison.cc index 7bfd80b5..a19e79c3 100644 --- a/snapper/Comparison.cc +++ b/snapper/Comparison.cc @@ -20,12 +20,11 @@ */ -#include - #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 index 00000000..5ffed07d --- /dev/null +++ b/snapper/Exception.h @@ -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 + + +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 diff --git a/snapper/File.cc b/snapper/File.cc index 84036f0a..a9d2cee0 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -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(); diff --git a/snapper/Makefile.am b/snapper/Makefile.am index a977bd16..2b3516de 100644 --- a/snapper/Makefile.am +++ b/snapper/Makefile.am @@ -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 diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 70882776..1246019e 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -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()); diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 731f5a02..f560286d 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -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(); diff --git a/snapper/Snapshot.h b/snapper/Snapshot.h index 9a4d2365..74c1a078 100644 --- a/snapper/Snapshot.h +++ b/snapper/Snapshot.h @@ -25,7 +25,6 @@ #include - #include #include