Merge pull request #1768 from ethereum/abortiffileexists

Do not overwrite files unless forced.
This commit is contained in:
Yoichi Hirai 2017-03-13 16:37:55 +01:00 committed by GitHub
commit 9aab3b8639
5 changed files with 33 additions and 5 deletions

View File

@ -1,6 +1,7 @@
### 0.4.10 (unreleased) ### 0.4.10 (unreleased)
Features: Features:
* Commandline interface: Do not overwrite files unless forced.
* Add ``assert(condition)``, which throws if condition is false. * Add ``assert(condition)``, which throws if condition is false.
* Introduce ``.transfer(value)`` for sending Ether. * Introduce ``.transfer(value)`` for sending Ether.
* Code generator: Support ``revert()`` to abort with rolling back, but not consuming all gas. * Code generator: Support ``revert()`` to abort with rolling back, but not consuming all gas.

View File

@ -93,6 +93,7 @@ static string const g_strOpcodes = "opcodes";
static string const g_strOptimize = "optimize"; static string const g_strOptimize = "optimize";
static string const g_strOptimizeRuns = "optimize-runs"; static string const g_strOptimizeRuns = "optimize-runs";
static string const g_strOutputDir = "output-dir"; static string const g_strOutputDir = "output-dir";
static string const g_strOverwrite = "overwrite";
static string const g_strSignatureHashes = "hashes"; static string const g_strSignatureHashes = "hashes";
static string const g_strSources = "sources"; static string const g_strSources = "sources";
static string const g_strSourceList = "sourceList"; static string const g_strSourceList = "sourceList";
@ -465,6 +466,12 @@ void CommandLineInterface::createFile(string const& _fileName, string const& _da
if (p.filename() != "." && p.filename() != "..") if (p.filename() != "." && p.filename() != "..")
fs::create_directories(p); fs::create_directories(p);
string pathName = (p / _fileName).string(); string pathName = (p / _fileName).string();
if (fs::exists(pathName) && !m_args.count(g_strOverwrite))
{
cerr << "Refusing to overwrite existing file \"" << pathName << "\" (use --overwrite to force)." << endl;
m_error = true;
return;
}
ofstream outFile(pathName); ofstream outFile(pathName);
outFile << _data; outFile << _data;
if (!outFile) if (!outFile)
@ -510,6 +517,7 @@ Allowed options)",
po::value<string>()->value_name("path"), po::value<string>()->value_name("path"),
"If given, creates one file per component and contract/file at the specified directory." "If given, creates one file per component and contract/file at the specified directory."
) )
(g_strOverwrite.c_str(), "Overwrite existing files (used together with -o).")
( (
g_argCombinedJson.c_str(), g_argCombinedJson.c_str(),
po::value<string>()->value_name(boost::join(g_combinedJsonArgs, ",")), po::value<string>()->value_name(boost::join(g_combinedJsonArgs, ",")),
@ -858,7 +866,7 @@ void CommandLineInterface::handleAst(string const& _argStr)
} }
} }
void CommandLineInterface::actOnInput() bool CommandLineInterface::actOnInput()
{ {
if (m_onlyAssemble) if (m_onlyAssemble)
outputAssembly(); outputAssembly();
@ -866,6 +874,7 @@ void CommandLineInterface::actOnInput()
writeLinkedFiles(); writeLinkedFiles();
else else
outputCompilationResults(); outputCompilationResults();
return !m_error;
} }
bool CommandLineInterface::link() bool CommandLineInterface::link()

View File

@ -47,7 +47,8 @@ public:
/// Parse the files and create source code objects /// Parse the files and create source code objects
bool processInput(); bool processInput();
/// Perform actions on the input depending on provided compiler arguments /// Perform actions on the input depending on provided compiler arguments
void actOnInput(); /// @returns true on success.
bool actOnInput();
private: private:
bool link(); bool link();
@ -81,6 +82,8 @@ private:
/// @arg _data to be written /// @arg _data to be written
void createFile(std::string const& _fileName, std::string const& _data); void createFile(std::string const& _fileName, std::string const& _data);
bool m_error = false; ///< If true, some error occurred.
bool m_onlyAssemble = false; bool m_onlyAssemble = false;
bool m_onlyLink = false; bool m_onlyLink = false;

View File

@ -58,15 +58,16 @@ int main(int argc, char** argv)
return 1; return 1;
if (!cli.processInput()) if (!cli.processInput())
return 1; return 1;
bool success = false;
try try
{ {
cli.actOnInput(); success = cli.actOnInput();
} }
catch (boost::exception const& _exception) catch (boost::exception const& _exception)
{ {
cerr << "Exception during output generation: " << boost::diagnostic_information(_exception) << endl; cerr << "Exception during output generation: " << boost::diagnostic_information(_exception) << endl;
return 1; success = false;
} }
return 0; return success ? 0 : 1;
} }

View File

@ -50,9 +50,23 @@ echo "Testing library checksum..."
echo '' | "$SOLC" --link --libraries a:0x90f20564390eAe531E810af625A22f51385Cd222 echo '' | "$SOLC" --link --libraries a:0x90f20564390eAe531E810af625A22f51385Cd222
! echo '' | "$SOLC" --link --libraries a:0x80f20564390eAe531E810af625A22f51385Cd222 2>/dev/null ! echo '' | "$SOLC" --link --libraries a:0x80f20564390eAe531E810af625A22f51385Cd222 2>/dev/null
echo "Testing overwriting files"
TMPDIR=$(mktemp -d)
(
set -e
# First time it works
echo 'contract C {} ' | "$SOLC" --bin -o "$TMPDIR/non-existing-stuff-to-create" 2>/dev/null
# Second time it fails
! echo 'contract C {} ' | "$SOLC" --bin -o "$TMPDIR/non-existing-stuff-to-create" 2>/dev/null
# Unless we force
echo 'contract C {} ' | "$SOLC" --overwrite --bin -o "$TMPDIR/non-existing-stuff-to-create" 2>/dev/null
)
rm -rf "$TMPDIR"
echo "Testing soljson via the fuzzer..." echo "Testing soljson via the fuzzer..."
TMPDIR=$(mktemp -d) TMPDIR=$(mktemp -d)
( (
set -e
cd "$REPO_ROOT" cd "$REPO_ROOT"
REPO_ROOT=$(pwd) # make it absolute REPO_ROOT=$(pwd) # make it absolute
cd "$TMPDIR" cd "$TMPDIR"