diff --git a/src/main.cpp b/src/main.cpp index 46767fc..4aeb390 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,12 +32,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include @@ -70,8 +72,8 @@ int main(int argc, char *argv[]) } catch (std::exception &e) { // Exceptions we can't recover from or ones we don't know. - cerr << translate("ERROR: ") << e.what() << '\n'; - cerr << translate("Error while parsing options.") << '\n'; + cerr << translate("ERROR: ") << e.what(); + cerr << translate(" (while parsing options)") << '\n'; return EXIT_FAILURE; } @@ -150,17 +152,33 @@ int main(int argc, char *argv[]) catch (const zip::exception &e) { if (opts.ignore_archive_errors && e.code == 1) - { + { // File is probably not an EPUB. return EXIT_SUCCESS; } - cerr << translate("ERROR: ") << e.what() << '\n'; - cerr << format(translate("Error while searching {0:s}.") - .str(), + cerr << translate("ERROR: ") << e.what(); + cerr << format(translate(" (while searching {0:s})").str(), filepath) << '\n'; return EXIT_FAILURE; } + catch (const std::ifstream::failure &e) + { + cerr << translate("ERROR: "); + if (e.code() == std::errc::permission_denied) + { + cerr << translate("Permission denied."); + } + else + { // std::ifstream seems to always return a generic error? + cerr << translate("Probably permission denied.") << " (" + << e.what() << ')'; + } + + cerr << format(translate(" (while opening {0:s})").str(), + filepath) + << '\n'; + } } return EXIT_SUCCESS; diff --git a/src/zip.cpp b/src/zip.cpp index 28be71f..67d9b70 100644 --- a/src/zip.cpp +++ b/src/zip.cpp @@ -106,16 +106,10 @@ 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}; - } + // Throw exception if we can't open the file. + std::ifstream file; + file.exceptions(std::ios::failbit); + file.open(filepath); file.close(); auto *zipfile{archive_read_new()};