2018-09-19 21:59:44 -04: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 {
|
|
|
|
|
2021-05-16 01:46:30 -04:00
|
|
|
LayeredVfsDirectory::LayeredVfsDirectory(std::vector<VirtualDir> dirs_, std::string name_)
|
|
|
|
: dirs(std::move(dirs_)), name(std::move(name_)) {}
|
2018-09-25 17:38:16 -04:00
|
|
|
|
|
|
|
LayeredVfsDirectory::~LayeredVfsDirectory() = default;
|
|
|
|
|
|
|
|
VirtualDir LayeredVfsDirectory::MakeLayeredDirectory(std::vector<VirtualDir> dirs,
|
|
|
|
std::string name) {
|
2018-09-19 21:59:44 -04:00
|
|
|
if (dirs.empty())
|
|
|
|
return nullptr;
|
|
|
|
if (dirs.size() == 1)
|
|
|
|
return dirs[0];
|
|
|
|
|
2020-12-10 01:31:58 -05:00
|
|
|
return VirtualDir(new LayeredVfsDirectory(std::move(dirs), std::move(name)));
|
2018-09-19 21:59:44 -04:00
|
|
|
}
|
|
|
|
|
2020-12-10 01:31:58 -05:00
|
|
|
VirtualFile LayeredVfsDirectory::GetFileRelative(std::string_view path) const {
|
2018-09-19 21:59:44 -04:00
|
|
|
for (const auto& layer : dirs) {
|
2018-09-23 21:50:16 -04:00
|
|
|
const auto file = layer->GetFileRelative(path);
|
2018-09-19 21:59:44 -04:00
|
|
|
if (file != nullptr)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:31:58 -05:00
|
|
|
VirtualDir LayeredVfsDirectory::GetDirectoryRelative(std::string_view path) const {
|
2018-09-19 21:59:44 -04:00
|
|
|
std::vector<VirtualDir> out;
|
|
|
|
for (const auto& layer : dirs) {
|
2018-09-23 21:50:16 -04:00
|
|
|
auto dir = layer->GetDirectoryRelative(path);
|
2020-12-10 01:31:58 -05:00
|
|
|
if (dir != nullptr) {
|
2018-09-23 21:50:16 -04:00
|
|
|
out.push_back(std::move(dir));
|
2020-12-10 01:31:58 -05:00
|
|
|
}
|
2018-09-19 21:59:44 -04:00
|
|
|
}
|
|
|
|
|
2018-09-25 17:38:16 -04:00
|
|
|
return MakeLayeredDirectory(std::move(out));
|
2018-09-19 21:59:44 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
VirtualFile LayeredVfsDirectory::GetFile(std::string_view file_name) const {
|
|
|
|
return GetFileRelative(file_name);
|
2018-09-19 21:59:44 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
VirtualDir LayeredVfsDirectory::GetSubdirectory(std::string_view subdir_name) const {
|
|
|
|
return GetDirectoryRelative(subdir_name);
|
2018-09-19 21:59:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string LayeredVfsDirectory::GetFullPath() const {
|
|
|
|
return dirs[0]->GetFullPath();
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:31:58 -05:00
|
|
|
std::vector<VirtualFile> LayeredVfsDirectory::GetFiles() const {
|
2018-09-19 21:59:44 -04: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 21:50:16 -04:00
|
|
|
}) == out.end()) {
|
2018-09-19 21:59:44 -04:00
|
|
|
out.push_back(file);
|
2018-09-23 21:50:16 -04:00
|
|
|
}
|
2018-09-19 21:59:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:31:58 -05:00
|
|
|
std::vector<VirtualDir> LayeredVfsDirectory::GetSubdirectories() const {
|
2018-09-19 21:59:44 -04: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 21:50:16 -04:00
|
|
|
out.reserve(names.size());
|
2018-09-19 21:59:44 -04: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-10 01:31:58 -05:00
|
|
|
VirtualDir LayeredVfsDirectory::GetParentDirectory() const {
|
2018-09-19 21:59:44 -04:00
|
|
|
return dirs[0]->GetParentDirectory();
|
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
VirtualDir LayeredVfsDirectory::CreateSubdirectory(std::string_view subdir_name) {
|
2018-09-19 21:59:44 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
VirtualFile LayeredVfsDirectory::CreateFile(std::string_view file_name) {
|
2018-09-19 21:59:44 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
bool LayeredVfsDirectory::DeleteSubdirectory(std::string_view subdir_name) {
|
2018-09-19 21:59:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
bool LayeredVfsDirectory::DeleteFile(std::string_view file_name) {
|
2018-09-19 21:59:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-02 02:34:40 -04:00
|
|
|
bool LayeredVfsDirectory::Rename(std::string_view new_name) {
|
|
|
|
name = new_name;
|
2018-09-19 21:59:44 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|