From: Arvin Schnell Date: Tue, 26 Apr 2011 14:37:03 +0000 (+0200) Subject: - work on error handling X-Git-Tag: v0.1.3~399 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=16dae3d6e8fd5ab49168472ef89e6e70b844e615;p=thirdparty%2Fsnapper.git - work on error handling --- diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 12512b4c..0ff3ad7c 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -503,31 +503,38 @@ namespace snapper list config_infos; - SysconfigFile sysconfig(SYSCONFIGFILE); - vector config_names; - sysconfig.getValue("SNAPPER_CONFIGS", config_names); - - for (vector::const_iterator it = config_names.begin(); it != config_names.end(); ++it) + try { - try - { - SysconfigFile config(CONFIGSDIR "/" + *it); + SysconfigFile sysconfig(SYSCONFIGFILE); + vector config_names; + sysconfig.getValue("SNAPPER_CONFIGS", config_names); - string subvolume = "/"; - config.getValue("SUBVOLUME", subvolume); - config_infos.push_back(ConfigInfo(*it, subvolume)); - } - catch (const FileNotFoundException& e) + for (vector::const_iterator it = config_names.begin(); it != config_names.end(); ++it) { - y2err("config '" << *it << "' not found"); + try + { + SysconfigFile config(CONFIGSDIR "/" + *it); + + string subvolume = "/"; + config.getValue("SUBVOLUME", subvolume); + config_infos.push_back(ConfigInfo(*it, subvolume)); + } + catch (const FileNotFoundException& e) + { + y2err("config '" << *it << "' not found"); + } } } + catch (const FileNotFoundException& e) + { + throw ListConfigsFailedException("sysconfig file not found"); + } return config_infos; } - bool + void Snapper::addConfig(const string& config_name, const string& subvolume, const string& template_name) { @@ -536,25 +543,46 @@ namespace snapper y2mil("config_name:" << config_name << " subvolume:" << subvolume << " template_name:" << template_name); - // TODO: error handling + try + { + SysconfigFile sysconfig(SYSCONFIGFILE); + vector config_names; + sysconfig.getValue("SNAPPER_CONFIGS", config_names); + if (find(config_names.begin(), config_names.end(), config_name) != config_names.end()) + { + throw AddConfigFailedException("config already exists"); + } - SysconfigFile sysconfig(SYSCONFIGFILE); - vector config_names; - sysconfig.getValue("SNAPPER_CONFIGS", config_names); - if (find(config_names.begin(), config_names.end(), config_name) != config_names.end()) - return false; - config_names.push_back(config_name); - sysconfig.setValue("SNAPPER_CONFIGS", config_names); + config_names.push_back(config_name); + sysconfig.setValue("SNAPPER_CONFIGS", config_names); + } + catch (const FileNotFoundException& e) + { + throw AddConfigFailedException("sysconfig file not found"); + } - SystemCmd cmd1(CPBIN " " CONFIGTEMPLATEDIR "/" + template_name + " " CONFIGSDIR "/" + - config_name); + SystemCmd cmd1(CPBIN " " + quote(CONFIGTEMPLATEDIR "/" + template_name) + " " + + quote(CONFIGSDIR "/" + config_name)); + if (cmd1.retcode() != 0) + { + throw AddConfigFailedException("copying template failed"); + } - SysconfigFile config(CONFIGSDIR "/" + config_name); - config.setValue("SUBVOLUME", subvolume); + try + { + SysconfigFile config(CONFIGSDIR "/" + config_name); + config.setValue("SUBVOLUME", subvolume); + } + catch (const FileNotFoundException& e) + { + throw AddConfigFailedException("modifying config failed"); + } SystemCmd cmd2(BTRFSBIN " subvolume create " + subvolume + "/snapshots"); - - return true; + if (cmd2.retcode() != 0) + { + throw AddConfigFailedException("creating snapshot failed"); + } } } diff --git a/snapper/Snapper.h b/snapper/Snapper.h index d70e2a44..7a63c176 100644 --- a/snapper/Snapper.h +++ b/snapper/Snapper.h @@ -68,6 +68,20 @@ namespace snapper virtual const char* what() const throw() { return "invalid config"; } }; + struct ListConfigsFailedException : public std::exception + { + explicit ListConfigsFailedException(const char* msg) throw() : msg(msg) {} + virtual const char* what() const throw() { return msg; } + const char* msg; + }; + + struct AddConfigFailedException : public std::exception + { + explicit AddConfigFailedException(const char* msg) throw() : msg(msg) {} + virtual const char* what() const throw() { return msg; } + const char* msg; + }; + class Snapper { @@ -103,7 +117,7 @@ namespace snapper const vector& getIgnorePatterns() const { return ignore_patterns; } static list getConfigs(); - static bool addConfig(const string& config_name, const string& subvolume, + static void addConfig(const string& config_name, const string& subvolume, const string& template_name); private: diff --git a/tools/snapper.cc b/tools/snapper.cc index 50d2101d..46a97f66 100644 --- a/tools/snapper.cc +++ b/tools/snapper.cc @@ -58,13 +58,21 @@ command_list_configs() header.add("Subvolume"); table.setHeader(header); - list config_infos = Snapper::getConfigs(); - for (list::const_iterator it = config_infos.begin(); it != config_infos.end(); ++it) + try { - TableRow row; - row.add(it->config_name); - row.add(it->subvolume); - table.add(row); + list config_infos = Snapper::getConfigs(); + for (list::const_iterator it = config_infos.begin(); it != config_infos.end(); ++it) + { + TableRow row; + row.add(it->config_name); + row.add(it->subvolume); + table.add(row); + } + } + catch (const ListConfigsFailedException& e) + { + cerr << sformat(_("Listing configs failed (%s)."), e.what()) << endl; + exit(EXIT_FAILURE); } cout << table; @@ -107,9 +115,13 @@ command_create_config() if ((opt = opts.find("template")) != opts.end()) template_name = opt->second; - if (!Snapper::addConfig(config_name, subvolume, template_name)) + try + { + Snapper::addConfig(config_name, subvolume, template_name); + } + catch (const AddConfigFailedException& e) { - cerr << _("Creating config failed.") << endl; + cerr << sformat(_("Creating config failed (%s)."), e.what()) << endl; exit(EXIT_FAILURE); } }