2018-07-19 03:07:11 +02:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-21 04:28:35 +02:00
|
|
|
#include <algorithm>
|
2018-07-21 04:30:20 +02:00
|
|
|
#include <cstddef>
|
2018-07-21 04:14:59 +02:00
|
|
|
#include <iterator>
|
2018-07-21 04:23:56 +02:00
|
|
|
#include <utility>
|
2018-08-03 17:50:27 +02:00
|
|
|
#include "common/assert.h"
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "common/common_paths.h"
|
2018-09-02 16:53:06 +02:00
|
|
|
#include "common/file_util.h"
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/file_sys/vfs_real.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
namespace FS = Common::FS;
|
|
|
|
|
2018-07-24 04:40:35 +02:00
|
|
|
static std::string ModeFlagsToString(Mode mode) {
|
|
|
|
std::string mode_str;
|
|
|
|
|
|
|
|
// Calculate the correct open mode for the file.
|
2020-08-03 13:11:07 +02:00
|
|
|
if (True(mode & Mode::Read) && True(mode & Mode::Write)) {
|
|
|
|
if (True(mode & Mode::Append)) {
|
2018-07-24 04:40:35 +02:00
|
|
|
mode_str = "a+";
|
2020-08-03 13:11:07 +02:00
|
|
|
} else {
|
2018-07-24 04:40:35 +02:00
|
|
|
mode_str = "r+";
|
2020-08-03 13:11:07 +02:00
|
|
|
}
|
2018-07-24 04:40:35 +02:00
|
|
|
} else {
|
2020-08-03 13:11:07 +02:00
|
|
|
if (True(mode & Mode::Read)) {
|
2018-07-24 04:40:35 +02:00
|
|
|
mode_str = "r";
|
2020-08-03 13:11:07 +02:00
|
|
|
} else if (True(mode & Mode::Append)) {
|
2018-07-24 04:40:35 +02:00
|
|
|
mode_str = "a";
|
2020-08-03 13:11:07 +02:00
|
|
|
} else if (True(mode & Mode::Write)) {
|
2018-07-24 04:40:35 +02:00
|
|
|
mode_str = "w";
|
2020-08-03 13:11:07 +02:00
|
|
|
} else {
|
2018-08-03 17:50:27 +02:00
|
|
|
UNREACHABLE_MSG("Invalid file open mode: {:02X}", static_cast<u8>(mode));
|
2020-08-03 13:11:07 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
2018-07-24 04:40:35 +02:00
|
|
|
|
|
|
|
mode_str += "b";
|
|
|
|
|
|
|
|
return mode_str;
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
RealVfsFilesystem::RealVfsFilesystem() : VfsFilesystem(nullptr) {}
|
2018-09-02 16:53:06 +02:00
|
|
|
RealVfsFilesystem::~RealVfsFilesystem() = default;
|
2018-08-03 17:50:00 +02:00
|
|
|
|
|
|
|
std::string RealVfsFilesystem::GetName() const {
|
|
|
|
return "Real";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFilesystem::IsReadable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFilesystem::IsWritable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
if (!FS::Exists(path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return VfsEntryType::None;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
|
|
|
if (FS::IsDirectory(path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return VfsEntryType::Directory;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
|
|
|
|
return VfsEntryType::File;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
2020-08-18 20:45:18 +02:00
|
|
|
|
|
|
|
if (const auto weak_iter = cache.find(path); weak_iter != cache.cend()) {
|
|
|
|
const auto& weak = weak_iter->second;
|
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
if (!weak.expired()) {
|
|
|
|
return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, weak.lock(), path, perms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
if (!FS::Exists(path) && True(perms & Mode::WriteAppend)) {
|
|
|
|
FS::CreateEmptyFile(path);
|
2020-08-03 13:11:07 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
auto backing = std::make_shared<FS::IOFile>(path, ModeFlagsToString(perms).c_str());
|
2020-08-18 20:45:18 +02:00
|
|
|
cache.insert_or_assign(path, backing);
|
2018-08-03 17:50:00 +02:00
|
|
|
|
|
|
|
// Cannot use make_shared as RealVfsFile constructor is private
|
|
|
|
return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, backing, path, perms));
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
2020-12-12 02:23:40 +01:00
|
|
|
const auto path_fwd = FS::SanitizePath(path, FS::DirectorySeparator::ForwardSlash);
|
2020-08-15 14:33:16 +02:00
|
|
|
if (!FS::Exists(path)) {
|
2020-12-12 02:23:40 +01:00
|
|
|
FS::CreateFullPath(path_fwd);
|
2020-08-15 14:33:16 +02:00
|
|
|
if (!FS::CreateEmptyFile(path)) {
|
2018-08-12 21:55:44 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-12 21:55:44 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return OpenFile(path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault);
|
2018-08-03 17:50:00 +02:00
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
if (!FS::Exists(old_path) || FS::Exists(new_path) || FS::IsDirectory(old_path) ||
|
|
|
|
!FS::Copy(old_path, new_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return OpenFile(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault);
|
2020-08-18 20:45:18 +02:00
|
|
|
const auto cached_file_iter = cache.find(old_path);
|
2018-08-03 17:50:00 +02:00
|
|
|
|
2020-08-18 20:45:18 +02:00
|
|
|
if (cached_file_iter != cache.cend()) {
|
|
|
|
auto file = cached_file_iter->second.lock();
|
2020-07-07 12:57:20 +02:00
|
|
|
|
2020-08-18 20:45:18 +02:00
|
|
|
if (!cached_file_iter->second.expired()) {
|
2020-07-07 12:57:20 +02:00
|
|
|
file->Close();
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2020-07-07 12:57:20 +02:00
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
if (!FS::Exists(old_path) || FS::Exists(new_path) || FS::IsDirectory(old_path) ||
|
|
|
|
!FS::Rename(old_path, new_path)) {
|
2020-07-07 12:57:20 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.erase(old_path);
|
2020-08-31 16:56:19 +02:00
|
|
|
if (file->Open(new_path, "r+b")) {
|
|
|
|
cache.insert_or_assign(new_path, std::move(file));
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Service_FS, "Failed to open path {} in order to re-cache it", new_path);
|
|
|
|
}
|
2020-07-07 12:57:20 +02:00
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2020-07-07 12:57:20 +02:00
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
return OpenFile(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
2020-08-18 20:45:18 +02:00
|
|
|
const auto cached_iter = cache.find(path);
|
|
|
|
|
|
|
|
if (cached_iter != cache.cend()) {
|
|
|
|
if (!cached_iter->second.expired()) {
|
|
|
|
cached_iter->second.lock()->Close();
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
cache.erase(path);
|
|
|
|
}
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
return FS::Delete(path);
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
2018-08-03 17:50:00 +02:00
|
|
|
// Cannot use make_shared as RealVfsDirectory constructor is private
|
|
|
|
return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto path_fwd = FS::SanitizePath(path, FS::DirectorySeparator::ForwardSlash);
|
|
|
|
if (!FS::Exists(path)) {
|
|
|
|
FS::CreateFullPath(path_fwd);
|
|
|
|
if (!FS::CreateDir(path)) {
|
2018-08-12 21:55:44 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-12 21:55:44 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
// Cannot use make_shared as RealVfsDirectory constructor is private
|
|
|
|
return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_,
|
|
|
|
std::string_view new_path_) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
if (!FS::Exists(old_path) || FS::Exists(new_path) || !FS::IsDirectory(old_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
|
|
|
FS::CopyDir(old_path, new_path);
|
2018-08-03 17:50:00 +02:00
|
|
|
return OpenDirectory(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
|
|
|
|
std::string_view new_path_) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
|
|
|
|
if (!FS::Exists(old_path) || FS::Exists(new_path) || FS::IsDirectory(old_path) ||
|
|
|
|
!FS::Rename(old_path, new_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
|
|
|
|
for (auto& kv : cache) {
|
2020-08-18 20:45:18 +02:00
|
|
|
// If the path in the cache doesn't start with old_path, then bail on this file.
|
|
|
|
if (kv.first.rfind(old_path, 0) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto file_old_path =
|
|
|
|
FS::SanitizePath(kv.first, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
auto file_new_path = FS::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()),
|
|
|
|
FS::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto& cached = cache[file_old_path];
|
|
|
|
|
|
|
|
if (cached.expired()) {
|
|
|
|
continue;
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2020-08-18 20:45:18 +02:00
|
|
|
|
|
|
|
auto file = cached.lock();
|
|
|
|
cache.erase(file_old_path);
|
2020-08-31 16:56:19 +02:00
|
|
|
if (file->Open(file_new_path, "r+b")) {
|
|
|
|
cache.insert_or_assign(std::move(file_new_path), std::move(file));
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Service_FS, "Failed to open path {} in order to re-cache it", file_new_path);
|
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OpenDirectory(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
for (auto& kv : cache) {
|
2020-08-18 20:45:18 +02:00
|
|
|
// If the path in the cache doesn't start with path, then bail on this file.
|
|
|
|
if (kv.first.rfind(path, 0) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& entry = cache[kv.first];
|
|
|
|
if (!entry.expired()) {
|
|
|
|
entry.lock()->Close();
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2020-08-18 20:45:18 +02:00
|
|
|
|
|
|
|
cache.erase(kv.first);
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
return FS::DeleteDirRecursively(path);
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::shared_ptr<FS::IOFile> backing_,
|
2018-08-03 17:50:00 +02:00
|
|
|
const std::string& path_, Mode perms_)
|
2020-08-15 14:33:16 +02:00
|
|
|
: base(base_), backing(std::move(backing_)), path(path_), parent_path(FS::GetParentPath(path_)),
|
|
|
|
path_components(FS::SplitPathComponents(path_)),
|
|
|
|
parent_components(FS::SliceVector(path_components, 0, path_components.size() - 1)),
|
2018-07-19 03:07:11 +02:00
|
|
|
perms(perms_) {}
|
|
|
|
|
2018-09-02 16:53:06 +02:00
|
|
|
RealVfsFile::~RealVfsFile() = default;
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
std::string RealVfsFile::GetName() const {
|
|
|
|
return path_components.back();
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t RealVfsFile::GetSize() const {
|
2018-08-03 17:50:00 +02:00
|
|
|
return backing->GetSize();
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
bool RealVfsFile::Resize(std::size_t new_size) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return backing->Resize(new_size);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualDir RealVfsFile::GetContainingDirectory() const {
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.OpenDirectory(parent_path, perms);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFile::IsWritable() const {
|
2020-08-03 13:11:07 +02:00
|
|
|
return True(perms & Mode::WriteAppend);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFile::IsReadable() const {
|
2020-08-03 13:11:07 +02:00
|
|
|
return True(perms & Mode::ReadWrite);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
|
2020-08-18 20:53:26 +02:00
|
|
|
if (!backing->Seek(static_cast<s64>(offset), SEEK_SET)) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return 0;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return backing->ReadBytes(data, length);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t RealVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
|
2020-08-18 20:53:26 +02:00
|
|
|
if (!backing->Seek(static_cast<s64>(offset), SEEK_SET)) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return 0;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return backing->WriteBytes(data, length);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool RealVfsFile::Rename(std::string_view name) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.MoveFile(path, parent_path + DIR_SEP + std::string(name)) != nullptr;
|
|
|
|
}
|
2018-07-22 07:23:29 +02:00
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
bool RealVfsFile::Close() {
|
|
|
|
return backing->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(DarkLordZach): MSVC would not let me combine the following two functions using 'if
|
|
|
|
// constexpr' because there is a compile error in the branch not used.
|
|
|
|
|
|
|
|
template <>
|
|
|
|
std::vector<VirtualFile> RealVfsDirectory::IterateEntries<RealVfsFile, VfsFile>() const {
|
2020-08-15 14:33:16 +02:00
|
|
|
if (perms == Mode::Append) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return {};
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
|
|
|
|
std::vector<VirtualFile> out;
|
2020-08-15 14:33:16 +02:00
|
|
|
FS::ForeachDirectoryEntry(
|
2018-08-03 17:50:00 +02:00
|
|
|
nullptr, path,
|
|
|
|
[&out, this](u64* entries_out, const std::string& directory, const std::string& filename) {
|
|
|
|
const std::string full_path = directory + DIR_SEP + filename;
|
2020-08-15 14:33:16 +02:00
|
|
|
if (!FS::IsDirectory(full_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
out.emplace_back(base.OpenFile(full_path, perms));
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return true;
|
|
|
|
});
|
2018-07-22 07:23:29 +02:00
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
template <>
|
|
|
|
std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDirectory>() const {
|
2020-08-15 14:33:16 +02:00
|
|
|
if (perms == Mode::Append) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return {};
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
|
|
|
|
std::vector<VirtualDir> out;
|
2020-08-15 14:33:16 +02:00
|
|
|
FS::ForeachDirectoryEntry(
|
2018-08-03 17:50:00 +02:00
|
|
|
nullptr, path,
|
|
|
|
[&out, this](u64* entries_out, const std::string& directory, const std::string& filename) {
|
|
|
|
const std::string full_path = directory + DIR_SEP + filename;
|
2020-08-15 14:33:16 +02:00
|
|
|
if (FS::IsDirectory(full_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
out.emplace_back(base.OpenDirectory(full_path, perms));
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_, Mode perms_)
|
2020-08-15 14:33:16 +02:00
|
|
|
: base(base_), path(FS::RemoveTrailingSlash(path_)), parent_path(FS::GetParentPath(path)),
|
|
|
|
path_components(FS::SplitPathComponents(path)),
|
|
|
|
parent_components(FS::SliceVector(path_components, 0, path_components.size() - 1)),
|
2018-07-19 03:07:11 +02:00
|
|
|
perms(perms_) {
|
2020-08-15 14:33:16 +02:00
|
|
|
if (!FS::Exists(path) && True(perms & Mode::WriteAppend)) {
|
|
|
|
FS::CreateDir(path);
|
2020-08-03 13:11:07 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2018-07-22 04:36:19 +02:00
|
|
|
|
2018-09-02 16:53:06 +02:00
|
|
|
RealVfsDirectory::~RealVfsDirectory() = default;
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualFile RealVfsDirectory::GetFileRelative(std::string_view path) const {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto full_path = FS::SanitizePath(this->path + DIR_SEP + std::string(path));
|
|
|
|
if (!FS::Exists(full_path) || FS::IsDirectory(full_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.OpenFile(full_path, perms);
|
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualDir RealVfsDirectory::GetDirectoryRelative(std::string_view path) const {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto full_path = FS::SanitizePath(this->path + DIR_SEP + std::string(path));
|
|
|
|
if (!FS::Exists(full_path) || !FS::IsDirectory(full_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.OpenDirectory(full_path, perms);
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualFile RealVfsDirectory::GetFile(std::string_view name) const {
|
2018-08-03 17:50:00 +02:00
|
|
|
return GetFileRelative(name);
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualDir RealVfsDirectory::GetSubdirectory(std::string_view name) const {
|
2018-08-03 17:50:00 +02:00
|
|
|
return GetDirectoryRelative(name);
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualFile RealVfsDirectory::CreateFileRelative(std::string_view path) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto full_path = FS::SanitizePath(this->path + DIR_SEP + std::string(path));
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.CreateFile(full_path, perms);
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualDir RealVfsDirectory::CreateDirectoryRelative(std::string_view path) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto full_path = FS::SanitizePath(this->path + DIR_SEP + std::string(path));
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.CreateDirectory(full_path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto full_path = FS::SanitizePath(this->path + DIR_SEP + std::string(name));
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.DeleteDirectory(full_path);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
std::vector<VirtualFile> RealVfsDirectory::GetFiles() const {
|
2018-08-03 17:50:00 +02:00
|
|
|
return IterateEntries<RealVfsFile, VfsFile>();
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
std::vector<VirtualDir> RealVfsDirectory::GetSubdirectories() const {
|
2018-08-03 17:50:00 +02:00
|
|
|
return IterateEntries<RealVfsDirectory, VfsDirectory>();
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::IsWritable() const {
|
2020-08-03 13:11:07 +02:00
|
|
|
return True(perms & Mode::WriteAppend);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::IsReadable() const {
|
2020-08-03 13:11:07 +02:00
|
|
|
return True(perms & Mode::ReadWrite);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string RealVfsDirectory::GetName() const {
|
|
|
|
return path_components.back();
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualDir RealVfsDirectory::GetParentDirectory() const {
|
2020-08-15 14:33:16 +02:00
|
|
|
if (path_components.size() <= 1) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.OpenDirectory(parent_path, perms);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualDir RealVfsDirectory::CreateSubdirectory(std::string_view name) {
|
2018-07-22 07:23:29 +02:00
|
|
|
const std::string subdir_path = (path + DIR_SEP).append(name);
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.CreateDirectory(subdir_path, perms);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 07:31:58 +01:00
|
|
|
VirtualFile RealVfsDirectory::CreateFile(std::string_view name) {
|
2018-07-22 07:23:29 +02:00
|
|
|
const std::string file_path = (path + DIR_SEP).append(name);
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.CreateFile(file_path, perms);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) {
|
|
|
|
const std::string subdir_path = (path + DIR_SEP).append(name);
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.DeleteDirectory(subdir_path);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool RealVfsDirectory::DeleteFile(std::string_view name) {
|
|
|
|
const std::string file_path = (path + DIR_SEP).append(name);
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.DeleteFile(file_path);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool RealVfsDirectory::Rename(std::string_view name) {
|
|
|
|
const std::string new_name = (parent_path + DIR_SEP).append(name);
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.MoveFile(path, new_name) != nullptr;
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 00:14:03 +02:00
|
|
|
std::string RealVfsDirectory::GetFullPath() const {
|
|
|
|
auto out = path;
|
|
|
|
std::replace(out.begin(), out.end(), '\\', '/');
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries() const {
|
2020-08-15 14:33:16 +02:00
|
|
|
if (perms == Mode::Append) {
|
2018-09-20 03:54:14 +02:00
|
|
|
return {};
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2018-09-20 03:54:14 +02:00
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
std::map<std::string, VfsEntryType, std::less<>> out;
|
2020-08-15 14:33:16 +02:00
|
|
|
FS::ForeachDirectoryEntry(
|
2018-09-20 03:54:14 +02:00
|
|
|
nullptr, path,
|
|
|
|
[&out](u64* entries_out, const std::string& directory, const std::string& filename) {
|
|
|
|
const std::string full_path = directory + DIR_SEP + filename;
|
2020-08-15 14:33:16 +02:00
|
|
|
out.emplace(filename,
|
|
|
|
FS::IsDirectory(full_path) ? VfsEntryType::Directory : VfsEntryType::File);
|
2018-09-20 03:54:14 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
} // namespace FileSys
|