2018-09-19 18:59:44 -07:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
#include "core/file_sys/vfs_layered.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-09-25 14:38:16 -07:00
|
|
|
LayeredVfsDirectory::LayeredVfsDirectory(std::vector<VirtualDir> dirs, std::string name)
|
|
|
|
: dirs(std::move(dirs)), name(std::move(name)) {}
|
|
|
|
|
|
|
|
LayeredVfsDirectory::~LayeredVfsDirectory() = default;
|
|
|
|
|
|
|
|
VirtualDir LayeredVfsDirectory::MakeLayeredDirectory(std::vector<VirtualDir> dirs,
|
|
|
|
std::string name) {
|
2018-09-19 18:59:44 -07:00
|
|
|
if (dirs.empty())
|
|
|
|
return nullptr;
|
|
|
|
if (dirs.size() == 1)
|
|
|
|
return dirs[0];
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
return VirtualDir(new LayeredVfsDirectory(std::move(dirs), std::move(name)));
|
2018-09-19 18:59:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualFile LayeredVfsDirectory::GetFileRelative(std::string_view path) const {
|
2018-09-19 18:59:44 -07:00
|
|
|
for (const auto& layer : dirs) {
|
2018-09-23 18:50:16 -07:00
|
|
|
const auto file = layer->GetFileRelative(path);
|
2018-09-19 18:59:44 -07:00
|
|
|
if (file != nullptr)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualDir LayeredVfsDirectory::GetDirectoryRelative(std::string_view path) const {
|
2018-09-19 18:59:44 -07:00
|
|
|
std::vector<VirtualDir> out;
|
|
|
|
for (const auto& layer : dirs) {
|
2018-09-23 18:50:16 -07:00
|
|
|
auto dir = layer->GetDirectoryRelative(path);
|
2020-12-09 22:31:58 -08:00
|
|
|
if (dir != nullptr) {
|
2018-09-23 18:50:16 -07:00
|
|
|
out.push_back(std::move(dir));
|
2020-12-09 22:31:58 -08:00
|
|
|
}
|
2018-09-19 18:59:44 -07:00
|
|
|
}
|
|
|
|
|
2018-09-25 14:38:16 -07:00
|
|
|
return MakeLayeredDirectory(std::move(out));
|
2018-09-19 18:59:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualFile LayeredVfsDirectory::GetFile(std::string_view name) const {
|
2018-09-19 18:59:44 -07:00
|
|
|
return GetFileRelative(name);
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualDir LayeredVfsDirectory::GetSubdirectory(std::string_view name) const {
|
2018-09-19 18:59:44 -07:00
|
|
|
return GetDirectoryRelative(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LayeredVfsDirectory::GetFullPath() const {
|
|
|
|
return dirs[0]->GetFullPath();
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
std::vector<VirtualFile> LayeredVfsDirectory::GetFiles() const {
|
2018-09-19 18:59:44 -07:00
|
|
|
std::vector<VirtualFile> out;
|
|
|
|
for (const auto& layer : dirs) {
|
|
|
|
for (const auto& file : layer->GetFiles()) {
|
|
|
|
if (std::find_if(out.begin(), out.end(), [&file](const VirtualFile& comp) {
|
|
|
|
return comp->GetName() == file->GetName();
|
2018-09-23 18:50:16 -07:00
|
|
|
}) == out.end()) {
|
2018-09-19 18:59:44 -07:00
|
|
|
out.push_back(file);
|
2018-09-23 18:50:16 -07:00
|
|
|
}
|
2018-09-19 18:59:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
std::vector<VirtualDir> LayeredVfsDirectory::GetSubdirectories() const {
|
2018-09-19 18:59:44 -07:00
|
|
|
std::vector<std::string> names;
|
|
|
|
for (const auto& layer : dirs) {
|
|
|
|
for (const auto& sd : layer->GetSubdirectories()) {
|
|
|
|
if (std::find(names.begin(), names.end(), sd->GetName()) == names.end())
|
|
|
|
names.push_back(sd->GetName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<VirtualDir> out;
|
2018-09-23 18:50:16 -07:00
|
|
|
out.reserve(names.size());
|
2018-09-19 18:59:44 -07:00
|
|
|
for (const auto& subdir : names)
|
|
|
|
out.push_back(GetSubdirectory(subdir));
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayeredVfsDirectory::IsWritable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayeredVfsDirectory::IsReadable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LayeredVfsDirectory::GetName() const {
|
|
|
|
return name.empty() ? dirs[0]->GetName() : name;
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualDir LayeredVfsDirectory::GetParentDirectory() const {
|
2018-09-19 18:59:44 -07:00
|
|
|
return dirs[0]->GetParentDirectory();
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualDir LayeredVfsDirectory::CreateSubdirectory(std::string_view name) {
|
2018-09-19 18:59:44 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualFile LayeredVfsDirectory::CreateFile(std::string_view name) {
|
2018-09-19 18:59:44 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayeredVfsDirectory::DeleteSubdirectory(std::string_view name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayeredVfsDirectory::DeleteFile(std::string_view name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LayeredVfsDirectory::Rename(std::string_view name_) {
|
|
|
|
name = name_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|