Atlas-Launcher/external/rvfs/source/RestrictedVirtualFileSystem.cpp

65 lines
No EOL
2.2 KiB
C++

#include "RestrictedVirtualFileSystem.hpp"
namespace rvfs {
RestrictedVirtualFileSystem::RestrictedVirtualFileSystem() = default;
QMap<QString, QString> RestrictedVirtualFileSystem::getMountedDirectories() {
return this->mounts;
}
void RestrictedVirtualFileSystem::mountDirectory(const QDir& destination, const QDir& source) {
// check if the source directory does exists
if (!source.exists())
// TODO(Faraphel): should be a custom error ?
throw std::runtime_error("The source directory does not exists.");
// mount it by associating our virtual folder with our real one
this->mounts[destination.absolutePath()] = source.absolutePath();
}
void RestrictedVirtualFileSystem::unmountDirectory(const QDir& destination) {
// remove the mounted directory from the map
this->mounts.remove(destination.absolutePath());
}
QDir RestrictedVirtualFileSystem::getWorkingDirectory() {
return this->workingDirectory;
}
void RestrictedVirtualFileSystem::setWorkingDirectory(const QDir& path) {
this->workingDirectory = path;
}
QString RestrictedVirtualFileSystem::resolvePath(QString path) {
if (QFileInfo(path).isRelative()) {
// if the path is relative, make it absolute with the current working directory
path = this->workingDirectory.absoluteFilePath(path);
} else {
// if the path is absolute, make sure to resolve the special syntax like ".."
path = QFileInfo(path).absoluteFilePath();
}
bool resolved = false;
// go through all the mounted directories
for (auto const& [destination, source] : this->mounts.asKeyValueRange()) {
// if the path is inside a mounted directory
if (path.startsWith(destination)) {
// replace the mounted directory by the original real one
path = path.replace(0, destination.length(), source);
// mark the path as resolved and break
resolved = true;
break;
}
}
// if the path has not been resolved, it is in an invalid or forbidden location
if (!resolved)
// TODO(Faraphel): should be a custom error ?
throw std::runtime_error("cannot resolve path: invalid.");
return path;
}
}