From aed35f04f974370bcb7398d5379c6e082efaf6ef Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 13 Dec 2020 22:43:02 +0100 Subject: [PATCH] Improved error handling --- CMakeLists.txt | 10 +- include/dwarfs/error.h | 56 ++++++--- include/dwarfs/logger.h | 3 +- src/dwarfs.cpp | 34 +++--- src/dwarfs/block_compressor.cpp | 67 +++++------ src/dwarfs/block_manager.cpp | 10 +- src/dwarfs/entry.cpp | 6 +- src/dwarfs/error.cpp | 80 +++++++++++++ src/dwarfs/filesystem_v2.cpp | 30 ++--- src/dwarfs/inode_manager.cpp | 14 +-- src/dwarfs/logger.cpp | 2 +- src/dwarfs/metadata_v2.cpp | 35 +++--- src/dwarfs/mmap.cpp | 14 +-- src/dwarfs/options.cpp | 3 +- src/dwarfs/os_access_posix.cpp | 16 +-- src/dwarfs/python_script.cpp | 32 ++--- src/dwarfs/scanner.cpp | 5 +- src/dwarfs/util.cpp | 4 +- src/dwarfs/worker_group.cpp | 4 +- src/dwarfsck.cpp | 44 ++++--- src/mkdwarfs.cpp | 202 ++++++++++++++++++++------------ 21 files changed, 418 insertions(+), 253 deletions(-) create mode 100644 src/dwarfs/error.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index df0d5e30..ea8fc60b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,7 @@ list( src/dwarfs/block_manager.cpp src/dwarfs/console_writer.cpp src/dwarfs/entry.cpp + src/dwarfs/error.cpp src/dwarfs/filesystem_v2.cpp src/dwarfs/filesystem_writer.cpp src/dwarfs/fstypes.cpp @@ -351,7 +352,7 @@ target_link_libraries( PkgConfig::LIBLZ4 PkgConfig::LIBLZMA) -foreach(tgt dwarfs-bin mkdwarfs dwarfsck dwarfsbench) +foreach(tgt ${BINARY_TARGETS}) target_link_libraries(${tgt} dwarfs) target_link_libraries(${tgt} -Wl,--whole-archive folly_exception_tracer_base folly_exception_tracer -Wl,--no-whole-archive) @@ -363,13 +364,8 @@ if(WITH_PYTHON) endif() target_link_libraries(mkdwarfs ${BOOST_PYTHON_LIBS} ${Python3_LIBRARIES}) -endif() -if(WITH_TESTS) - foreach(tgt dwarfs_test dwarfs_compat_test) - target_link_libraries(${tgt} dwarfs) - endforeach() - if(WITH_PYTHON) + if(WITH_TESTS) foreach(tgt dwarfs_test dwarfs_compat_test) target_link_libraries(${tgt} ${BOOST_PYTHON_LIBS} ${Python3_LIBRARIES}) endforeach() diff --git a/include/dwarfs/error.h b/include/dwarfs/error.h index 1d03a819..4e7ca7fd 100644 --- a/include/dwarfs/error.h +++ b/include/dwarfs/error.h @@ -22,34 +22,56 @@ #pragma once #include +#include +#include + +#include +#include namespace dwarfs { class error : public std::exception { public: - error(const std::string& str, int err_no) noexcept - : what_(str) - , errno_(err_no) {} + error(std::string const& s, char const* file, int line) noexcept + : what_(s) + , file_(file) + , line_(line) {} - error(const error& e) noexcept - : what_(e.what_) - , errno_(e.errno_) {} + char const* what() const noexcept override { return what_.c_str(); } - error& operator=(const error& e) noexcept { - if (&e != this) { - what_ = e.what_; - errno_ = e.errno_; - } - return *this; - } + char const* file() const { return file_; } - const char* what() const noexcept override { return what_.c_str(); } - - int get_errno() const { return errno_; } + int line() const { return line_; } private: std::string what_; - int errno_; + char const* file_; + int line_; }; +class system_error : public boost::system::system_error { + public: + system_error(char const* file, int line) noexcept; + system_error(std::string const& s, char const* file, int line) noexcept; + system_error(std::string const& s, int err, char const* file, + int line) noexcept; + system_error(int err, char const* file, int line) noexcept; + + int get_errno() const { return code().value(); } + + char const* file() const { return file_; } + + int line() const { return line_; } + + private: + char const* file_; + int line_; +}; + +#define DWARFS_THROW(cls, ...) throw cls(__VA_ARGS__, __FILE__, __LINE__) + +void dump_exceptions(); + +int safe_main(std::function fn); + } // namespace dwarfs diff --git a/include/dwarfs/logger.h b/include/dwarfs/logger.h index 6d06875a..0abbd258 100644 --- a/include/dwarfs/logger.h +++ b/include/dwarfs/logger.h @@ -36,6 +36,7 @@ #include #include +#include "dwarfs/error.h" #include "dwarfs/util.h" namespace dwarfs { @@ -302,7 +303,7 @@ template