2018-07-19 01:07:11 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-21 02:28:35 +00:00
|
|
|
#include <algorithm>
|
2018-07-21 02:30:20 +00:00
|
|
|
#include <cstddef>
|
2018-07-21 02:14:59 +00:00
|
|
|
#include <iterator>
|
2018-07-21 02:23:56 +00:00
|
|
|
#include <utility>
|
2018-08-03 15:50:27 +00:00
|
|
|
#include "common/assert.h"
|
2018-07-19 01:07:11 +00:00
|
|
|
#include "common/common_paths.h"
|
2018-09-02 14:53:06 +00:00
|
|
|
#include "common/file_util.h"
|
2018-07-19 01:07:11 +00:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/file_sys/vfs_real.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-07-24 02:40:35 +00:00
|
|
|
static std::string ModeFlagsToString(Mode mode) {
|
|
|
|
std::string mode_str;
|
|
|
|
|
|
|
|
// Calculate the correct open mode for the file.
|
2020-08-03 11:11:07 +00:00
|
|
|
if (True(mode & Mode::Read) && True(mode & Mode::Write)) {
|
|
|
|
if (True(mode & Mode::Append)) {
|
2018-07-24 02:40:35 +00:00
|
|
|
mode_str = "a+";
|
2020-08-03 11:11:07 +00:00
|
|
|
} else {
|
2018-07-24 02:40:35 +00:00
|
|
|
mode_str = "r+";
|
2020-08-03 11:11:07 +00:00
|
|
|
}
|
2018-07-24 02:40:35 +00:00
|
|
|
} else {
|
2020-08-03 11:11:07 +00:00
|
|
|
if (True(mode & Mode::Read)) {
|
2018-07-24 02:40:35 +00:00
|
|
|
mode_str = "r";
|
2020-08-03 11:11:07 +00:00
|
|
|
} else if (True(mode & Mode::Append)) {
|
2018-07-24 02:40:35 +00:00
|
|
|
mode_str = "a";
|
2020-08-03 11:11:07 +00:00
|
|
|
} else if (True(mode & Mode::Write)) {
|
2018-07-24 02:40:35 +00:00
|
|
|
mode_str = "w";
|
2020-08-03 11:11:07 +00:00
|
|
|
} else {
|
2018-08-03 15:50:27 +00:00
|
|
|
UNREACHABLE_MSG("Invalid file open mode: {:02X}", static_cast<u8>(mode));
|
2020-08-03 11:11:07 +00:00
|
|
|
}
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
2018-07-24 02:40:35 +00:00
|
|
|
|
|
|
|
mode_str += "b";
|
|
|
|
|
|
|
|
return mode_str;
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 15:50:00 +00:00
|
|
|
RealVfsFilesystem::RealVfsFilesystem() : VfsFilesystem(nullptr) {}
|
2018-09-02 14:53:06 +00:00
|
|
|
RealVfsFilesystem::~RealVfsFilesystem() = default;
|
2018-08-03 15:50:00 +00: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 {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
if (!FileUtil::Exists(path))
|
|
|
|
return VfsEntryType::None;
|
|
|
|
if (FileUtil::IsDirectory(path))
|
|
|
|
return VfsEntryType::Directory;
|
|
|
|
|
|
|
|
return VfsEntryType::File;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
if (cache.find(path) != cache.end()) {
|
|
|
|
auto weak = cache[path];
|
|
|
|
if (!weak.expired()) {
|
|
|
|
return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, weak.lock(), path, perms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 11:11:07 +00:00
|
|
|
if (!FileUtil::Exists(path) && True(perms & Mode::WriteAppend)) {
|
2018-08-03 15:50:00 +00:00
|
|
|
FileUtil::CreateEmptyFile(path);
|
2020-08-03 11:11:07 +00:00
|
|
|
}
|
2018-08-03 15:50:00 +00:00
|
|
|
|
|
|
|
auto backing = std::make_shared<FileUtil::IOFile>(path, ModeFlagsToString(perms).c_str());
|
|
|
|
cache[path] = backing;
|
|
|
|
|
|
|
|
// 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) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-12 02:47:25 +00:00
|
|
|
const auto path_fwd = FileUtil::SanitizePath(path, FileUtil::DirectorySeparator::ForwardSlash);
|
2018-08-12 19:55:44 +00:00
|
|
|
if (!FileUtil::Exists(path)) {
|
|
|
|
FileUtil::CreateFullPath(path_fwd);
|
|
|
|
if (!FileUtil::CreateEmptyFile(path))
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-08-03 15:50:00 +00:00
|
|
|
return OpenFile(path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto old_path =
|
|
|
|
FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path =
|
|
|
|
FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
|
|
|
|
if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
|
|
|
|
FileUtil::IsDirectory(old_path) || !FileUtil::Copy(old_path, new_path))
|
|
|
|
return nullptr;
|
|
|
|
return OpenFile(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto old_path =
|
|
|
|
FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path =
|
|
|
|
FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
|
|
|
|
if (cache.find(old_path) != cache.end()) {
|
2020-07-07 10:57:20 +00:00
|
|
|
auto file = cache[old_path].lock();
|
|
|
|
|
|
|
|
if (!cache[old_path].expired()) {
|
|
|
|
file->Close();
|
2018-08-03 15:50:00 +00:00
|
|
|
}
|
2020-07-07 10:57:20 +00:00
|
|
|
|
|
|
|
if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
|
|
|
|
FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.erase(old_path);
|
|
|
|
file->Open(new_path, "r+b");
|
|
|
|
cache[new_path] = file;
|
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
2018-08-03 15:50:00 +00:00
|
|
|
}
|
2020-07-07 10:57:20 +00:00
|
|
|
|
2018-08-03 15:50:00 +00:00
|
|
|
return OpenFile(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
if (cache.find(path) != cache.end()) {
|
|
|
|
if (!cache[path].expired())
|
|
|
|
cache[path].lock()->Close();
|
|
|
|
cache.erase(path);
|
|
|
|
}
|
|
|
|
return FileUtil::Delete(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00: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) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-12 02:47:25 +00:00
|
|
|
const auto path_fwd = FileUtil::SanitizePath(path, FileUtil::DirectorySeparator::ForwardSlash);
|
2018-08-12 19:55:44 +00:00
|
|
|
if (!FileUtil::Exists(path)) {
|
|
|
|
FileUtil::CreateFullPath(path_fwd);
|
|
|
|
if (!FileUtil::CreateDir(path))
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-08-03 15:50:00 +00: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_) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto old_path =
|
|
|
|
FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path =
|
|
|
|
FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
|
|
|
|
!FileUtil::IsDirectory(old_path))
|
|
|
|
return nullptr;
|
|
|
|
FileUtil::CopyDir(old_path, new_path);
|
|
|
|
return OpenDirectory(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
|
|
|
|
std::string_view new_path_) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto old_path =
|
|
|
|
FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
|
|
|
const auto new_path =
|
|
|
|
FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
|
|
|
|
FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
for (auto& kv : cache) {
|
|
|
|
// Path in cache starts with old_path
|
|
|
|
if (kv.first.rfind(old_path, 0) == 0) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto file_old_path =
|
|
|
|
FileUtil::SanitizePath(kv.first, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
const auto file_new_path =
|
2018-08-07 03:21:37 +00:00
|
|
|
FileUtil::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()),
|
|
|
|
FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
auto cached = cache[file_old_path];
|
|
|
|
if (!cached.expired()) {
|
|
|
|
auto file = cached.lock();
|
|
|
|
file->Open(file_new_path, "r+b");
|
|
|
|
cache.erase(file_old_path);
|
|
|
|
cache[file_new_path] = file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OpenDirectory(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
|
2018-08-07 03:21:37 +00:00
|
|
|
const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
|
2018-08-03 15:50:00 +00:00
|
|
|
for (auto& kv : cache) {
|
|
|
|
// Path in cache starts with old_path
|
|
|
|
if (kv.first.rfind(path, 0) == 0) {
|
|
|
|
if (!cache[kv.first].expired())
|
|
|
|
cache[kv.first].lock()->Close();
|
|
|
|
cache.erase(kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FileUtil::DeleteDirRecursively(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::shared_ptr<FileUtil::IOFile> backing_,
|
|
|
|
const std::string& path_, Mode perms_)
|
|
|
|
: base(base_), backing(std::move(backing_)), path(path_),
|
2018-07-19 01:07:11 +00:00
|
|
|
parent_path(FileUtil::GetParentPath(path_)),
|
|
|
|
path_components(FileUtil::SplitPathComponents(path_)),
|
|
|
|
parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
|
|
|
|
perms(perms_) {}
|
|
|
|
|
2018-09-02 14:53:06 +00:00
|
|
|
RealVfsFile::~RealVfsFile() = default;
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
std::string RealVfsFile::GetName() const {
|
|
|
|
return path_components.back();
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t RealVfsFile::GetSize() const {
|
2018-08-03 15:50:00 +00:00
|
|
|
return backing->GetSize();
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
bool RealVfsFile::Resize(std::size_t new_size) {
|
2018-08-03 15:50:00 +00:00
|
|
|
return backing->Resize(new_size);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> RealVfsFile::GetContainingDirectory() const {
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.OpenDirectory(parent_path, perms);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFile::IsWritable() const {
|
2020-08-03 11:11:07 +00:00
|
|
|
return True(perms & Mode::WriteAppend);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFile::IsReadable() const {
|
2020-08-03 11:11:07 +00:00
|
|
|
return True(perms & Mode::ReadWrite);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
|
2018-08-03 15:50:00 +00:00
|
|
|
if (!backing->Seek(offset, SEEK_SET))
|
2018-07-19 01:07:11 +00:00
|
|
|
return 0;
|
2018-08-03 15:50:00 +00:00
|
|
|
return backing->ReadBytes(data, length);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t RealVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
|
2018-08-03 15:50:00 +00:00
|
|
|
if (!backing->Seek(offset, SEEK_SET))
|
2018-07-19 01:07:11 +00:00
|
|
|
return 0;
|
2018-08-03 15:50:00 +00:00
|
|
|
return backing->WriteBytes(data, length);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 05:23:29 +00:00
|
|
|
bool RealVfsFile::Rename(std::string_view name) {
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.MoveFile(path, parent_path + DIR_SEP + std::string(name)) != nullptr;
|
|
|
|
}
|
2018-07-22 05:23:29 +00:00
|
|
|
|
2018-08-03 15:50:00 +00: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 {
|
|
|
|
if (perms == Mode::Append)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<VirtualFile> out;
|
|
|
|
FileUtil::ForeachDirectoryEntry(
|
|
|
|
nullptr, path,
|
|
|
|
[&out, this](u64* entries_out, const std::string& directory, const std::string& filename) {
|
|
|
|
const std::string full_path = directory + DIR_SEP + filename;
|
|
|
|
if (!FileUtil::IsDirectory(full_path))
|
|
|
|
out.emplace_back(base.OpenFile(full_path, perms));
|
|
|
|
return true;
|
|
|
|
});
|
2018-07-22 05:23:29 +00:00
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-08-03 15:50:00 +00:00
|
|
|
template <>
|
|
|
|
std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDirectory>() const {
|
|
|
|
if (perms == Mode::Append)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<VirtualDir> out;
|
|
|
|
FileUtil::ForeachDirectoryEntry(
|
|
|
|
nullptr, path,
|
|
|
|
[&out, this](u64* entries_out, const std::string& directory, const std::string& filename) {
|
|
|
|
const std::string full_path = directory + DIR_SEP + filename;
|
|
|
|
if (FileUtil::IsDirectory(full_path))
|
|
|
|
out.emplace_back(base.OpenDirectory(full_path, perms));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 15:50:00 +00:00
|
|
|
RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_, Mode perms_)
|
|
|
|
: base(base_), path(FileUtil::RemoveTrailingSlash(path_)),
|
|
|
|
parent_path(FileUtil::GetParentPath(path)),
|
2018-07-19 01:07:11 +00:00
|
|
|
path_components(FileUtil::SplitPathComponents(path)),
|
|
|
|
parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
|
|
|
|
perms(perms_) {
|
2020-08-03 11:11:07 +00:00
|
|
|
if (!FileUtil::Exists(path) && True(perms & Mode::WriteAppend)) {
|
2018-07-19 01:07:11 +00:00
|
|
|
FileUtil::CreateDir(path);
|
2020-08-03 11:11:07 +00:00
|
|
|
}
|
2018-08-03 15:50:00 +00:00
|
|
|
}
|
2018-07-22 02:36:19 +00:00
|
|
|
|
2018-09-02 14:53:06 +00:00
|
|
|
RealVfsDirectory::~RealVfsDirectory() = default;
|
|
|
|
|
2018-08-03 15:50:00 +00:00
|
|
|
std::shared_ptr<VfsFile> RealVfsDirectory::GetFileRelative(std::string_view path) const {
|
|
|
|
const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
|
2018-08-10 00:50:50 +00:00
|
|
|
if (!FileUtil::Exists(full_path) || FileUtil::IsDirectory(full_path))
|
2018-08-03 15:50:00 +00:00
|
|
|
return nullptr;
|
|
|
|
return base.OpenFile(full_path, perms);
|
|
|
|
}
|
2018-07-19 01:07:11 +00:00
|
|
|
|
2018-08-03 15:50:00 +00:00
|
|
|
std::shared_ptr<VfsDirectory> RealVfsDirectory::GetDirectoryRelative(std::string_view path) const {
|
|
|
|
const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
|
2018-08-10 00:50:50 +00:00
|
|
|
if (!FileUtil::Exists(full_path) || !FileUtil::IsDirectory(full_path))
|
2018-08-03 15:50:00 +00:00
|
|
|
return nullptr;
|
|
|
|
return base.OpenDirectory(full_path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsFile> RealVfsDirectory::GetFile(std::string_view name) const {
|
|
|
|
return GetFileRelative(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> RealVfsDirectory::GetSubdirectory(std::string_view name) const {
|
|
|
|
return GetDirectoryRelative(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsFile> RealVfsDirectory::CreateFileRelative(std::string_view path) {
|
|
|
|
const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
|
|
|
|
return base.CreateFile(full_path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateDirectoryRelative(std::string_view path) {
|
|
|
|
const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
|
|
|
|
return base.CreateDirectory(full_path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
|
|
|
|
auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(name));
|
|
|
|
return base.DeleteDirectory(full_path);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<VfsFile>> RealVfsDirectory::GetFiles() const {
|
2018-08-03 15:50:00 +00:00
|
|
|
return IterateEntries<RealVfsFile, VfsFile>();
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<VfsDirectory>> RealVfsDirectory::GetSubdirectories() const {
|
2018-08-03 15:50:00 +00:00
|
|
|
return IterateEntries<RealVfsDirectory, VfsDirectory>();
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::IsWritable() const {
|
2020-08-03 11:11:07 +00:00
|
|
|
return True(perms & Mode::WriteAppend);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::IsReadable() const {
|
2020-08-03 11:11:07 +00:00
|
|
|
return True(perms & Mode::ReadWrite);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string RealVfsDirectory::GetName() const {
|
|
|
|
return path_components.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> RealVfsDirectory::GetParentDirectory() const {
|
|
|
|
if (path_components.size() <= 1)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.OpenDirectory(parent_path, perms);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 05:23:29 +00:00
|
|
|
std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateSubdirectory(std::string_view name) {
|
|
|
|
const std::string subdir_path = (path + DIR_SEP).append(name);
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.CreateDirectory(subdir_path, perms);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 05:23:29 +00:00
|
|
|
std::shared_ptr<VfsFile> RealVfsDirectory::CreateFile(std::string_view name) {
|
|
|
|
const std::string file_path = (path + DIR_SEP).append(name);
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.CreateFile(file_path, perms);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 05:23:29 +00:00
|
|
|
bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) {
|
|
|
|
const std::string subdir_path = (path + DIR_SEP).append(name);
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.DeleteDirectory(subdir_path);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 05:23:29 +00:00
|
|
|
bool RealVfsDirectory::DeleteFile(std::string_view name) {
|
|
|
|
const std::string file_path = (path + DIR_SEP).append(name);
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.DeleteFile(file_path);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 05:23:29 +00:00
|
|
|
bool RealVfsDirectory::Rename(std::string_view name) {
|
|
|
|
const std::string new_name = (parent_path + DIR_SEP).append(name);
|
2018-08-03 15:50:00 +00:00
|
|
|
return base.MoveFile(path, new_name) != nullptr;
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 22:14:03 +00:00
|
|
|
std::string RealVfsDirectory::GetFullPath() const {
|
|
|
|
auto out = path;
|
|
|
|
std::replace(out.begin(), out.end(), '\\', '/');
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-09-24 01:50:16 +00:00
|
|
|
std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries() const {
|
2018-09-20 01:54:14 +00:00
|
|
|
if (perms == Mode::Append)
|
|
|
|
return {};
|
|
|
|
|
2018-09-24 01:50:16 +00:00
|
|
|
std::map<std::string, VfsEntryType, std::less<>> out;
|
2018-09-20 01:54:14 +00:00
|
|
|
FileUtil::ForeachDirectoryEntry(
|
|
|
|
nullptr, path,
|
|
|
|
[&out](u64* entries_out, const std::string& directory, const std::string& filename) {
|
|
|
|
const std::string full_path = directory + DIR_SEP + filename;
|
|
|
|
out.emplace(filename, FileUtil::IsDirectory(full_path) ? VfsEntryType::Directory
|
|
|
|
: VfsEntryType::File);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
} // namespace FileSys
|