From: Arvin Schnell Date: Wed, 27 Apr 2022 10:13:43 +0000 (+0200) Subject: - improved error handling X-Git-Tag: v0.10.2~5^2 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=46f49d7aaa251fa99c39aeb419be7c2964ecbf3b;p=thirdparty%2Fsnapper.git - improved error handling --- diff --git a/snapper/FileUtils.cc b/snapper/FileUtils.cc index 4a4d8550..5add3d46 100644 --- a/snapper/FileUtils.cc +++ b/snapper/FileUtils.cc @@ -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()))); } diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 7e1bb7d5..31c476bd 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -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")); } } diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 941bc772..d9c01ac6 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -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(); } diff --git a/snapper/XmlFile.cc b/snapper/XmlFile.cc index bc77822a..ff621d2d 100644 --- a/snapper/XmlFile.cc +++ b/snapper/XmlFile.cc @@ -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")); }