2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-09-20 03:59:44 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
#include "core/file_sys/vfs_layered.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2021-05-16 07:46:30 +02:00
|
|
|
LayeredVfsDirectory::LayeredVfsDirectory(std::vector<VirtualDir> dirs_, std::string name_)
|
|
|
|
: dirs(std::move(dirs_)), name(std::move(name_)) {}
|
2018-09-25 23:38:16 +02:00
|
|
|
|
|
|
|
LayeredVfsDirectory::~LayeredVfsDirectory() = default;
|
|
|
|
|
|
|
|
VirtualDir LayeredVfsDirectory::MakeLayeredDirectory(std::vector<VirtualDir> dirs,
|
|
|
|
std::string name) {
|
2018-09-20 03:59:44 +02:00
|
|
|
if (dirs.empty())
|
|
|
|
return nullptr;
|
|
|
|
if (dirs.size() == 1)
|
|
|
|
return dirs[0];
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
return VirtualDir(new LayeredVfsDirectory(std::move(dirs), std::move(name)));
|
2018-09-20 03:59:44 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualFile LayeredVfsDirectory::GetFileRelative(std::string_view path) const {
|
2018-09-20 03:59:44 +02:00
|
|
|
for (const auto& layer : dirs) {
|
2018-09-24 03:50:16 +02:00
|
|
|
const auto file = layer->GetFileRelative(path);
|
2018-09-20 03:59:44 +02:00
|
|
|
if (file != nullptr)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualDir LayeredVfsDirectory::GetDirectoryRelative(std::string_view path) const {
|
2018-09-20 03:59:44 +02:00
|
|
|
std::vector<VirtualDir> out;
|
|
|
|
for (const auto& layer : dirs) {
|
2018-09-24 03:50:16 +02:00
|
|
|
auto dir = layer->GetDirectoryRelative(path);
|
2020-12-10 07:31:58 +01:00
|
|
|
if (dir != nullptr) {
|
2018-09-24 03:50:16 +02:00
|
|
|
out.push_back(std::move(dir));
|
2020-12-10 07:31:58 +01:00
|
|
|
}
|
2018-09-20 03:59:44 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 23:38:16 +02:00
|
|
|
return MakeLayeredDirectory(std::move(out));
|
2018-09-20 03:59:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualFile LayeredVfsDirectory::GetFile(std::string_view file_name) const {
|
|
|
|
return GetFileRelative(file_name);
|
2018-09-20 03:59:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualDir LayeredVfsDirectory::GetSubdirectory(std::string_view subdir_name) const {
|
|
|
|
return GetDirectoryRelative(subdir_name);
|
2018-09-20 03:59:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string LayeredVfsDirectory::GetFullPath() const {
|
|
|
|
return dirs[0]->GetFullPath();
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
std::vector<VirtualFile> LayeredVfsDirectory::GetFiles() const {
|
2018-09-20 03:59:44 +02: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-24 03:50:16 +02:00
|
|
|
}) == out.end()) {
|
2018-09-20 03:59:44 +02:00
|
|
|
out.push_back(file);
|
2018-09-24 03:50:16 +02:00
|
|
|
}
|
2018-09-20 03:59:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
std::vector<VirtualDir> LayeredVfsDirectory::GetSubdirectories() const {
|
2018-09-20 03:59:44 +02: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-24 03:50:16 +02:00
|
|
|
out.reserve(names.size());
|
2018-09-20 03:59:44 +02: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 07:31:58 +01:00
|
|
|
VirtualDir LayeredVfsDirectory::GetParentDirectory() const {
|
2018-09-20 03:59:44 +02:00
|
|
|
return dirs[0]->GetParentDirectory();
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualDir LayeredVfsDirectory::CreateSubdirectory(std::string_view subdir_name) {
|
2018-09-20 03:59:44 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualFile LayeredVfsDirectory::CreateFile(std::string_view file_name) {
|
2018-09-20 03:59:44 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
bool LayeredVfsDirectory::DeleteSubdirectory(std::string_view subdir_name) {
|
2018-09-20 03:59:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
bool LayeredVfsDirectory::DeleteFile(std::string_view file_name) {
|
2018-09-20 03:59:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
bool LayeredVfsDirectory::Rename(std::string_view new_name) {
|
|
|
|
name = new_name;
|
2018-09-20 03:59:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|