list<ConfigInfo> config_infos;
- SysconfigFile sysconfig(SYSCONFIGFILE);
- vector<string> config_names;
- sysconfig.getValue("SNAPPER_CONFIGS", config_names);
-
- for (vector<string>::const_iterator it = config_names.begin(); it != config_names.end(); ++it)
+ try
{
- try
- {
- SysconfigFile config(CONFIGSDIR "/" + *it);
+ SysconfigFile sysconfig(SYSCONFIGFILE);
+ vector<string> 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<string>::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)
{
y2mil("config_name:" << config_name << " subvolume:" << subvolume <<
" template_name:" << template_name);
- // TODO: error handling
+ try
+ {
+ SysconfigFile sysconfig(SYSCONFIGFILE);
+ vector<string> 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<string> 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");
+ }
}
}
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
{
const vector<string>& getIgnorePatterns() const { return ignore_patterns; }
static list<ConfigInfo> 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:
header.add("Subvolume");
table.setHeader(header);
- list<ConfigInfo> config_infos = Snapper::getConfigs();
- for (list<ConfigInfo>::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<ConfigInfo> config_infos = Snapper::getConfigs();
+ for (list<ConfigInfo>::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;
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);
}
}