From 84f600196c917410c482bb49fffdbfe1046213ee Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 27 May 2021 21:39:01 +0200 Subject: [PATCH] Add error code to zip::exception. --- src/main.cpp | 3 ++- src/zip.cpp | 20 ++++++++++++++++++-- src/zip.hpp | 3 +++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 97e1980..cc018ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "fs-compat.hpp" #include "options.hpp" #include "search.hpp" +#include "zip.hpp" #include #include @@ -145,7 +146,7 @@ int main(int argc, char *argv[]) std::lock_guard guard(mutex_matches_all); matches_all.emplace_back(matches); } - catch (const std::exception &e) + catch (const zip::exception &e) { cerr << translate("ERROR: ") << e.what() << '\n'; cerr << format(translate("Error while searching {0:s}.") diff --git a/src/zip.cpp b/src/zip.cpp index f2e458f..e4765d6 100644 --- a/src/zip.cpp +++ b/src/zip.cpp @@ -26,6 +26,8 @@ #include #include +#include +#include #include #include @@ -92,6 +94,18 @@ std::string read_file(const fs::path &filepath, std::string_view entry_path) struct archive *open_file(const fs::path &filepath) { + std::ifstream file{filepath}; + if (!file.good()) + { + exception e{format(translate("Could not open {0:s}: " + "Permission denied.") + .str(), + filepath.string())}; + e.code = 2; + throw exception{e}; + } + file.close(); + auto *zipfile{archive_read_new()}; archive_read_support_filter_all(zipfile); archive_read_support_format_zip(zipfile); @@ -101,8 +115,10 @@ struct archive *open_file(const fs::path &filepath) { close_file(zipfile, filepath); - throw exception{format(translate("Could not open {0:s}.").str(), - filepath.string())}; + exception e{format(translate("Could not open {0:s}.").str(), + filepath.string())}; + e.code = 1; + throw exception{e}; } return zipfile; diff --git a/src/zip.hpp b/src/zip.hpp index 18adbbf..e8719a5 100644 --- a/src/zip.hpp +++ b/src/zip.hpp @@ -21,6 +21,7 @@ #include +#include #include #include #include @@ -47,6 +48,8 @@ class exception : public std::runtime_error { public: using std::runtime_error::runtime_error; + + std::uint8_t code{0}; }; } // namespace epubgrep::zip