Merge pull request #836 from aria2/better-file-status-error-msg

Better error message when local file status cannot be retrieved
pull/857/head
Tatsuhiro Tsujikawa 2017-01-16 21:07:17 +09:00 committed by GitHub
commit 6625f8cadb
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()));
}
}