]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- work on error handling
authorArvin Schnell <aschnell@suse.de>
Tue, 26 Apr 2011 14:37:03 +0000 (16:37 +0200)
committerArvin Schnell <aschnell@suse.de>
Tue, 26 Apr 2011 14:37:03 +0000 (16:37 +0200)
snapper/Snapper.cc
snapper/Snapper.h
tools/snapper.cc

index 12512b4cae1e7dee360718f2153b31d7c3585459..0ff3ad7cb82dded4bcc674e5ad18b61f2479c162 100644 (file)
@@ -503,31 +503,38 @@ namespace snapper
 
        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)
     {
@@ -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<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");
+       }
     }
 
 }
index d70e2a44786690276170255f88e7ded9762ce44d..7a63c176bc8f224c2de43edc30e50bb1ea7a6c0f 100644 (file)
@@ -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<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:
index 50d2101ddaacf7c30785e3e52faf74dff8f5d56a..46a97f66b5a41ece70b4f6e11a0ddeada74fedde 100644 (file)
@@ -58,13 +58,21 @@ command_list_configs()
     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;
@@ -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);
     }
 }