From: Arvin Schnell Date: Tue, 26 Apr 2011 12:22:35 +0000 (+0200) Subject: - work on exceptions X-Git-Tag: v0.1.3~400 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f9a947740652bea632f34f4293cffa02b2ff5112;p=thirdparty%2Fsnapper.git - work on exceptions --- diff --git a/snapper/File.cc b/snapper/File.cc index a9d2cee0..5ce1628a 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -30,7 +30,6 @@ #include "snapper/File.h" #include "snapper/Snapper.h" #include "snapper/Comparison.h" -#include "snapper/Factory.h" #include "snapper/AppUtil.h" #include "snapper/XmlFile.h" #include "snapper/Enum.h" diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 1a2f3db5..12512b4c 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -52,11 +52,19 @@ namespace snapper y2mil("libsnapper version " VERSION); y2mil("config_name:" << config_name); - config = new SysconfigFile(CONFIGSDIR "/" + config_name); + try + { + config = new SysconfigFile(CONFIGSDIR "/" + config_name); + } + catch (const FileNotFoundException& e) + { + throw ConfigNotFoundException(); + } string val; - if (config->getValue("SUBVOLUME", val)) - subvolume = val; + if (!config->getValue("SUBVOLUME", val)) + throw InvalidConfigException(); + subvolume = val; y2mil("subvolume:" << subvolume); diff --git a/snapper/Snapper.h b/snapper/Snapper.h index a152b6fb..d70e2a44 100644 --- a/snapper/Snapper.h +++ b/snapper/Snapper.h @@ -56,6 +56,19 @@ namespace snapper }; + struct ConfigNotFoundException : public std::exception + { + explicit ConfigNotFoundException() throw() {} + virtual const char* what() const throw() { return "config not found"; } + }; + + struct InvalidConfigException : public std::exception + { + explicit InvalidConfigException() throw() {} + virtual const char* what() const throw() { return "invalid config"; } + }; + + class Snapper { public: diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 71986292..8512769c 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -314,19 +314,21 @@ namespace snapper } - bool + void Snapshot::createFilesystemSnapshot() const { SystemCmd cmd(BTRFSBIN " subvolume snapshot " + snapper->subvolumeDir() + " " + snapshotDir()); - return cmd.retcode() == 0; + if (cmd.retcode() != 0) + throw CreateSnapshotFailedException(); } - bool + void Snapshot::deleteFilesystemSnapshot() const { - SystemCmd cmd(BTRFSBIN " subvolume delete /" + snapshotDir()); - return cmd.retcode() == 0; + SystemCmd cmd(BTRFSBIN " subvolume delete " + snapshotDir()); + if (cmd.retcode() != 0) + throw DeleteSnapshotFailedException(); } diff --git a/snapper/Snapshot.h b/snapper/Snapshot.h index 74c1a078..82a11ee0 100644 --- a/snapper/Snapshot.h +++ b/snapper/Snapshot.h @@ -41,6 +41,19 @@ namespace snapper enum SnapshotType { SINGLE, PRE, POST }; + struct CreateSnapshotFailedException : public std::exception + { + explicit CreateSnapshotFailedException() throw() {} + virtual const char* what() const throw() { return "create snapshot failed"; } + }; + + struct DeleteSnapshotFailedException : public std::exception + { + explicit DeleteSnapshotFailedException() throw() {} + virtual const char* what() const throw() { return "delete snapshot failed"; } + }; + + class Snapshot { public: @@ -87,8 +100,8 @@ namespace snapper string cleanup; bool writeInfo() const; - bool createFilesystemSnapshot() const; - bool deleteFilesystemSnapshot() const; + void createFilesystemSnapshot() const; + void deleteFilesystemSnapshot() const; }; diff --git a/tools/snapper.cc b/tools/snapper.cc index b8382835..50d2101d 100644 --- a/tools/snapper.cc +++ b/tools/snapper.cc @@ -666,7 +666,20 @@ main(int argc, char** argv) } else { - sh = createSnapper(config_name); + try + { + sh = createSnapper(config_name); + } + catch (const ConfigNotFoundException& e) + { + cerr << sformat(_("Config '%s' not found."), config_name.c_str()) << endl; + exit(EXIT_FAILURE); + } + catch (const InvalidConfigException& e) + { + cerr << sformat(_("Config '%s' is invalid."), config_name.c_str()) << endl; + exit(EXIT_FAILURE); + } if (!quiet) sh->setCompareCallback(&compare_callback_impl);