]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- improved error handling 713/head
authorArvin Schnell <aschnell@suse.de>
Wed, 27 Apr 2022 10:13:43 +0000 (12:13 +0200)
committerArvin Schnell <aschnell@suse.de>
Wed, 27 Apr 2022 10:13:43 +0000 (12:13 +0200)
snapper/FileUtils.cc
snapper/Snapper.cc
snapper/Snapshot.cc
snapper/XmlFile.cc

index 4a4d85504cfbaeb86d95e9db26b771411d2b8012..5add3d465efba885e0f6d24025c417ff67d3fdcc 100644 (file)
@@ -866,7 +866,8 @@ namespace snapper
        : base_dir(base_dir), name(name_template)
     {
        if (!base_dir.mkdtemp(name))
-           throw runtime_error_with_errno("mkdtemp failed", errno);
+           SN_THROW(IOErrorException(sformat("mkdtmp failed errno:%d (%s)", errno,
+                                             stringerror(errno).c_str())));
     }
 
 
@@ -891,7 +892,8 @@ namespace snapper
     {
        SDir subdir(base_dir, name);
        if (!subdir.mount(device, mount_type, mount_flags, mount_data))
-           throw runtime_error_with_errno("mount failed", errno);
+           SN_THROW(IOErrorException(sformat("mount failed errno:%d (%s)", errno,
+                                             stringerror(errno).c_str())));
     }
 
 
index 7e1bb7d514afe7a74b6ea6badc40a5d3b8cf231b..31c476bdcbb96000c0ee2779368158188672c24d 100644 (file)
@@ -82,6 +82,8 @@ namespace snapper
        }
        catch (const InvalidKeyException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(InvalidConfigdataException());
        }
     }
@@ -111,6 +113,8 @@ namespace snapper
        }
        catch (const FileNotFoundException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(ConfigNotFoundException());
        }
 
@@ -145,6 +149,7 @@ namespace snapper
            }
            catch (const UmountSnapshotFailedException& e)
            {
+               SN_CAUGHT(e);
            }
        }
 
@@ -310,16 +315,22 @@ namespace snapper
                }
                catch (const FileNotFoundException& e)
                {
+                   SN_CAUGHT(e);
+
                    y2err("config '" << *it << "' not found");
                }
                catch (const InvalidConfigException& e)
                {
+                   SN_CAUGHT(e);
+
                    y2err("config '" << *it << "' is invalid");
                }
            }
        }
        catch (const FileNotFoundException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(ListConfigsFailedException("sysconfig-file not found"));
        }
 
@@ -374,10 +385,14 @@ namespace snapper
        }
        catch (const InvalidConfigException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(CreateConfigFailedException("invalid filesystem type"));
        }
        catch (const ProgramNotInstalledException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(CreateConfigFailedException(e.what()));
        }
 
@@ -396,6 +411,8 @@ namespace snapper
        }
        catch (const FileNotFoundException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(CreateConfigFailedException("sysconfig-file not found"));
        }
 
@@ -410,6 +427,8 @@ namespace snapper
        }
        catch (const FileNotFoundException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(CreateConfigFailedException("modifying config failed"));
        }
 
@@ -465,6 +484,8 @@ namespace snapper
            }
            catch (const DeleteSnapshotFailedException& e)
            {
+               SN_CAUGHT(e);
+
                // ignore, Filesystem->deleteConfig will fail anyway
            }
        }
@@ -475,6 +496,8 @@ namespace snapper
        }
        catch (const DeleteConfigFailedException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(DeleteConfigFailedException("deleting snapshot failed"));
        }
 
@@ -495,6 +518,8 @@ namespace snapper
        }
        catch (const FileNotFoundException& e)
        {
+           SN_CAUGHT(e);
+
            SN_THROW(DeleteConfigFailedException("sysconfig-file not found"));
        }
     }
index 941bc7725e0eb3f2045f20e0cae953eb3f67b1fa..d9c01ac6d9ed01c62521d0245ddd302757731e38 100644 (file)
@@ -210,6 +210,9 @@ namespace snapper
            {
                SDir info_dir(infos_dir, *it1);
                int fd = info_dir.open("info.xml", O_NOFOLLOW | O_CLOEXEC);
+               if (fd < 0)
+                   SN_THROW(IOErrorException("open info.xml failed"));
+
                XmlFile file(fd, "");
 
                const xmlNode* node = file.getRootElement();
@@ -272,8 +275,10 @@ namespace snapper
 
                entries.push_back(snapshot);
            }
-           catch (const IOErrorException& e)
+           catch (const Exception& e)
            {
+               SN_CAUGHT(e);
+
                y2err("loading " << *it1 << " failed");
            }
        }
@@ -370,6 +375,8 @@ namespace snapper
        }
        catch (const IOErrorException& e)
        {
+           SN_CAUGHT(e);
+
            y2err("reading failed");
        }
 
@@ -496,12 +503,29 @@ namespace snapper
 
        SDir info_dir = openInfoDir();
 
-       xml.save(info_dir.mktemp(tmp_name));
+       int fd = info_dir.mktemp(tmp_name);
+       if (fd < 0)
+           SN_THROW(IOErrorException("mktemp failed"));
+
+       try
+       {
+           xml.save(fd);
+       }
+       catch (const Exception& e)
+       {
+           SN_CAUGHT(e);
+
+           info_dir.unlink(tmp_name, 0);
+
+           SN_RETHROW(e);
+       }
 
        if (info_dir.rename(tmp_name, file_name) != 0)
+       {
            SN_THROW(IOErrorException(sformat("rename info.xml failed infoDir:%s errno:%d (%s)",
                                              info_dir.fullname().c_str(), errno,
                                              stringerror(errno).c_str())));
+       }
 
        info_dir.fsync();
     }
index bc77822a7ed20ef072c155127adc73464055332d..ff621d2d1dcf3f2ba51c6b53ba2fa75f36cd0728 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) [2010-2012] Novell, Inc.
- * Copyright (c) 2020 SUSE LLC
+ * Copyright (c) [2020-2022] SUSE LLC
  *
  * All Rights Reserved.
  *
@@ -35,7 +35,7 @@ namespace snapper
        : doc(xmlNewDoc((const xmlChar*) "1.0"))
     {
        if (!doc)
-           throw BadAllocException();
+           SN_THROW(BadAllocException());
     }
 
 
@@ -45,7 +45,7 @@ namespace snapper
        close(fd);
 
        if (!doc)
-           throw IOErrorException("xmlReadFd failed");
+           SN_THROW(IOErrorException("xmlReadFd failed"));
     }
 
 
@@ -53,7 +53,7 @@ namespace snapper
        : doc(xmlReadFile(filename.c_str(), NULL, XML_PARSE_NOBLANKS | XML_PARSE_NONET))
     {
        if (!doc)
-           throw IOErrorException("xmlReadFile failed");
+           SN_THROW(IOErrorException("xmlReadFile failed"));
     }
 
 
@@ -69,17 +69,22 @@ namespace snapper
     {
        FILE* f = fdopen(fd, "w");
        if (!f)
-           throw IOErrorException("fdopen");
+       {
+           close(fd);
+           SN_THROW(IOErrorException("fdopen"));
+       }
 
        if (xmlDocFormatDump(f, doc, 1) == -1)
        {
            fclose(f);
-           throw IOErrorException("xmlDocFormatDump failed");
+           SN_THROW(IOErrorException("xmlDocFormatDump failed"));
        }
 
        fflush(f);
        fsync(fileno(f));
-       fclose(f);
+
+       if (fclose(f) != 0)
+           SN_THROW(IOErrorException("fclose failed"));
     }
 
 
@@ -87,7 +92,7 @@ namespace snapper
     XmlFile::save(const string& filename)
     {
        if (xmlSaveFormatFile(filename.c_str(), doc, 1) == -1)
-           throw IOErrorException("xmlSaveFormatFile failed");
+           SN_THROW(IOErrorException("xmlSaveFormatFile failed"));
     }