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