FileReader: More accurate description of FileReader::readFile() and its arguments

This commit is contained in:
Kamil Śliwak 2021-04-10 19:57:50 +02:00
parent 6a41b417a3
commit 0922d60ba6
2 changed files with 16 additions and 9 deletions

View File

@ -42,7 +42,7 @@ void FileReader::setSources(StringMap _sources)
m_sourceCodes = std::move(_sources);
}
ReadCallback::Result FileReader::readFile(string const& _kind, string const& _path)
ReadCallback::Result FileReader::readFile(string const& _kind, string const& _sourceUnitName)
{
try
{
@ -51,11 +51,11 @@ ReadCallback::Result FileReader::readFile(string const& _kind, string const& _pa
"ReadFile callback used as callback kind " +
_kind
));
string validPath = _path;
if (validPath.find("file://") == 0)
validPath.erase(0, 7);
string strippedSourceUnitName = _sourceUnitName;
if (strippedSourceUnitName.find("file://") == 0)
strippedSourceUnitName.erase(0, 7);
auto canonicalPath = boost::filesystem::weakly_canonical(m_basePath / validPath);
auto canonicalPath = boost::filesystem::weakly_canonical(m_basePath / strippedSourceUnitName);
bool isAllowed = false;
for (auto const& allowedDir: m_allowedDirectories)
{
@ -80,7 +80,7 @@ ReadCallback::Result FileReader::readFile(string const& _kind, string const& _pa
// NOTE: we ignore the FileNotFound exception as we manually check above
auto contents = readFileAsString(canonicalPath.string());
m_sourceCodes[_path] = contents;
m_sourceCodes[_sourceUnitName] = contents;
return ReadCallback::Result{true, contents};
}
catch (util::Exception const& _exception)

View File

@ -62,14 +62,21 @@ public:
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.
/// Does not enforce @a allowedDirectories().
void setSources(StringMap _sources);
/// Adds the source code for a given source unit ID.
/// Does not enforce @a allowedDirectories().
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);
/// Receives a @p _sourceUnitName that refers to a source unit in compiler's virtual filesystem
/// and attempts to interpret it as a path and read the corresponding file from disk.
/// The read will only succeed if the canonical path of the file is within one of the @a allowedDirectories().
/// @param _kind must be equal to "source". Other values are not supported.
/// @return Content of the loaded file or an error message. If the operation succeeds, a copy of
/// the content is retained in @a sourceCodes() under the key of @a _sourceUnitName. If the key
/// already exists, previous content is discarded.
frontend::ReadCallback::Result readFile(std::string const& _kind, std::string const& _sourceUnitName);
frontend::ReadCallback::Callback reader()
{