Better error message when local file status cannot be retrieved

This commit is contained in:
Tatsuhiro Tsujikawa 2017-01-15 16:33:58 +09:00
parent c1a0b33515
commit 744a5a550d
3 changed files with 22 additions and 1 deletions

View File

@ -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;

View File

@ -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.
*/

View File

@ -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()));
}
}