Merge pull request #10199 from ethereum/readfile

[CLI] Improve error handling of missing/unwriteable files
This commit is contained in:
chriseth
2020-11-10 13:50:40 +01:00
committed by GitHub
10 changed files with 55 additions and 9 deletions
+22 -2
View File
@@ -614,6 +614,7 @@ bool CommandLineInterface::readInputFilesAndConfigureRemappings()
continue;
}
// NOTE: we ignore the FileNotFound exception as we manually check above
m_sourceCodes[infile.generic_string()] = readFileAsString(infile.string());
path = boost::filesystem::canonical(infile).string();
}
@@ -643,6 +644,10 @@ bool CommandLineInterface::parseLibraryOption(string const& _input)
{
// Thrown e.g. if path is too long.
}
catch (FileNotFound const&)
{
// Should not happen if `fs::is_regular_file` is correct.
}
vector<string> libraries;
boost::split(libraries, data, boost::is_space() || boost::is_any_of(","), boost::token_compress_on);
@@ -738,7 +743,11 @@ void CommandLineInterface::createFile(string const& _fileName, string const& _da
ofstream outFile(pathName);
outFile << _data;
if (!outFile)
BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + pathName));
{
serr() << "Could not write to file \"" << pathName << "\"." << endl;
m_error = true;
return;
}
}
void CommandLineInterface::createJson(string const& _fileName, string const& _json)
@@ -1146,6 +1155,7 @@ bool CommandLineInterface::processInput()
if (!boost::filesystem::is_regular_file(canonicalPath))
return ReadCallback::Result{false, "Not a valid file."};
// NOTE: we ignore the FileNotFound exception as we manually check above
auto contents = readFileAsString(canonicalPath.string());
m_sourceCodes[path.generic_string()] = contents;
return ReadCallback::Result{true, contents};
@@ -1232,7 +1242,17 @@ bool CommandLineInterface::processInput()
if (jsonFile.empty())
input = readStandardInput();
else
input = readFileAsString(jsonFile);
{
try
{
input = readFileAsString(jsonFile);
}
catch (FileNotFound const&)
{
serr() << "File not found: " << jsonFile << endl;
return false;
}
}
StandardCompiler compiler(fileReader);
sout() << compiler.compile(std::move(input)) << endl;
return true;