mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Extract file reading logic from CommandLineInterface into FileReader class.
This commit is contained in:
@@ -133,6 +133,8 @@ set(sources
|
||||
interface/CompilerStack.cpp
|
||||
interface/CompilerStack.h
|
||||
interface/DebugSettings.h
|
||||
interface/FileReader.cpp
|
||||
interface/FileReader.h
|
||||
interface/ImportRemapper.cpp
|
||||
interface/ImportRemapper.h
|
||||
interface/GasEstimator.cpp
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
#include <libsolidity/interface/FileReader.h>
|
||||
|
||||
#include <liblangutil/Exceptions.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <libsolutil/CommonIO.h>
|
||||
#include <libsolutil/Exceptions.h>
|
||||
|
||||
using solidity::frontend::ReadCallback;
|
||||
using solidity::langutil::InternalCompilerError;
|
||||
using solidity::util::errinfo_comment;
|
||||
using solidity::util::readFileAsString;
|
||||
using std::string;
|
||||
|
||||
namespace solidity::frontend
|
||||
{
|
||||
|
||||
void FileReader::setSource(boost::filesystem::path const& _path, SourceCode _source)
|
||||
{
|
||||
m_sourceCodes[_path.generic_string()] = std::move(_source);
|
||||
}
|
||||
|
||||
void FileReader::setSources(StringMap _sources)
|
||||
{
|
||||
m_sourceCodes = std::move(_sources);
|
||||
}
|
||||
|
||||
ReadCallback::Result FileReader::readFile(string const& _kind, string const& _path)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_kind != ReadCallback::kindString(ReadCallback::Kind::ReadFile))
|
||||
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment(
|
||||
"ReadFile callback used as callback kind " +
|
||||
_kind
|
||||
));
|
||||
string validPath = _path;
|
||||
if (validPath.find("file://") == 0)
|
||||
validPath.erase(0, 7);
|
||||
|
||||
auto const path = m_basePath / validPath;
|
||||
auto canonicalPath = boost::filesystem::weakly_canonical(path);
|
||||
bool isAllowed = false;
|
||||
for (auto const& allowedDir: m_allowedDirectories)
|
||||
{
|
||||
// If dir is a prefix of boostPath, we are fine.
|
||||
if (
|
||||
std::distance(allowedDir.begin(), allowedDir.end()) <= std::distance(canonicalPath.begin(), canonicalPath.end()) &&
|
||||
std::equal(allowedDir.begin(), allowedDir.end(), canonicalPath.begin())
|
||||
)
|
||||
{
|
||||
isAllowed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isAllowed)
|
||||
return ReadCallback::Result{false, "File outside of allowed directories."};
|
||||
|
||||
if (!boost::filesystem::exists(canonicalPath))
|
||||
return ReadCallback::Result{false, "File not found."};
|
||||
|
||||
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};
|
||||
}
|
||||
catch (util::Exception const& _exception)
|
||||
{
|
||||
return ReadCallback::Result{false, "Exception in read callback: " + boost::diagnostic_information(_exception)};
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return ReadCallback::Result{false, "Unknown exception in read callback."};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
#pragma once
|
||||
|
||||
#include <libsolidity/interface/ImportRemapper.h>
|
||||
#include <libsolidity/interface/ReadFile.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace solidity::frontend
|
||||
{
|
||||
|
||||
/// FileReader - used for progressively loading source code.
|
||||
///
|
||||
/// It is used in solc to load files from CLI parameters, stdin, or from JSON and
|
||||
/// also used in the solc language server where solc is a long running process.
|
||||
class FileReader
|
||||
{
|
||||
public:
|
||||
using StringMap = std::map<SourceUnitName, SourceCode>;
|
||||
using PathMap = std::map<SourceUnitName, boost::filesystem::path>;
|
||||
using FileSystemPathSet = std::set<boost::filesystem::path>;
|
||||
|
||||
/// Constructs a FileReader with a base path and a set of allowed directories that
|
||||
/// will be used when requesting files from this file reader instance.
|
||||
explicit FileReader(
|
||||
boost::filesystem::path _basePath = {},
|
||||
FileSystemPathSet _allowedDirectories = {}
|
||||
):
|
||||
m_basePath(std::move(_basePath)),
|
||||
m_allowedDirectories(std::move(_allowedDirectories)),
|
||||
m_sourceCodes()
|
||||
{}
|
||||
|
||||
void setBasePath(boost::filesystem::path _path) { m_basePath = std::move(_path); }
|
||||
boost::filesystem::path const& basePath() const noexcept { return m_basePath; }
|
||||
|
||||
void allowDirectory(boost::filesystem::path _path) { m_allowedDirectories.insert(std::move(_path)); }
|
||||
FileSystemPathSet const& allowedDirectories() const noexcept { return m_allowedDirectories; }
|
||||
|
||||
StringMap const& sourceCodes() const noexcept { return m_sourceCodes; }
|
||||
|
||||
/// Retrieves the source code for a given source unit ID.
|
||||
SourceCode const& sourceCode(SourceUnitName const& _sourceUnitName) const { return m_sourceCodes.at(_sourceUnitName); }
|
||||
|
||||
/// Resets all sources to the given map of source unit ID to source codes.
|
||||
void setSources(StringMap _sources);
|
||||
|
||||
/// Adds the source code for a given source unit ID.
|
||||
void setSource(boost::filesystem::path const& _path, SourceCode _source);
|
||||
|
||||
/// Reads a given file at @p _path of kind @p _kind from the local filesystem and returns the result.
|
||||
/// @p _kind must always be passed as "source".
|
||||
frontend::ReadCallback::Result readFile(std::string const& _kind, std::string const& _path);
|
||||
|
||||
frontend::ReadCallback::Callback reader()
|
||||
{
|
||||
return [this](std::string const& _kind, std::string const& _path) { return readFile(_kind, _path); };
|
||||
}
|
||||
|
||||
private:
|
||||
/// Base path, used for resolving relative paths in imports.
|
||||
boost::filesystem::path m_basePath;
|
||||
|
||||
/// list of allowed directories to read files from
|
||||
FileSystemPathSet m_allowedDirectories;
|
||||
|
||||
/// map of input files to source code strings
|
||||
StringMap m_sourceCodes;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user