From 744a5a550df748dcbbbd2587bc7c84381f52ed0c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 15 Jan 2017 16:33:58 +0900 Subject: [PATCH] Better error message when local file status cannot be retrieved --- src/File.cc | 10 ++++++++++ src/File.h | 7 +++++++ src/OptionHandlerImpl.cc | 6 +++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/File.cc b/src/File.cc index 3ee6c47a..14189ef0 100644 --- a/src/File.cc +++ b/src/File.cc @@ -79,6 +79,16 @@ bool File::exists() return fillStat(fstat) == 0; } +bool File::exists(std::string &err) +{ + a2_struct_stat fstat; + if (fillStat(fstat) != 0) { + err = fmt("Could not get file status: %s", strerror(errno)); + return false; + } + return true; +} + bool File::isFile() { a2_struct_stat fstat; diff --git a/src/File.h b/src/File.h index 1e659022..6d304640 100644 --- a/src/File.h +++ b/src/File.h @@ -70,6 +70,13 @@ public: */ bool exists(); + /** + * Tests whether the file or directory denoted by name exists. If + * file does not exist, or file status could not be retrieved, this + * function stores error message to |err|. + */ + bool exists(std::string& err); + /** * Tests whether the file denoted by name is a regular file. */ diff --git a/src/OptionHandlerImpl.cc b/src/OptionHandlerImpl.cc index 6be23492..6214e84b 100644 --- a/src/OptionHandlerImpl.cc +++ b/src/OptionHandlerImpl.cc @@ -551,7 +551,11 @@ void LocalFilePathOptionHandler::parseArg(Option& option, auto path = util::replace(optarg, "${HOME}", util::getHomeDir()); if (mustExist_) { File f(path); - if (!f.exists() || f.isDir()) { + std::string err; + if (!f.exists(err)) { + throw DL_ABORT_EX(err); + } + if (f.isDir()) { throw DL_ABORT_EX(fmt(MSG_NOT_FILE, optarg.c_str())); } }