common: fs: Rework the Common Filesystem interface to make use of std::filesystem (#6270)
* common: fs: fs_types: Create filesystem types Contains various filesystem types used by the Common::FS library * common: fs: fs_util: Add std::string to std::u8string conversion utility * common: fs: path_util: Add utlity functions for paths Contains various utility functions for getting or manipulating filesystem paths used by the Common::FS library * common: fs: file: Rewrite the IOFile implementation * common: fs: Reimplement Common::FS library using std::filesystem * common: fs: fs_paths: Add fs_paths to replace common_paths * common: fs: path_util: Add the rest of the path functions * common: Remove the previous Common::FS implementation * general: Remove unused fs includes * string_util: Remove unused function and include * nvidia_flags: Migrate to the new Common::FS library * settings: Migrate to the new Common::FS library * logging: backend: Migrate to the new Common::FS library * core: Migrate to the new Common::FS library * perf_stats: Migrate to the new Common::FS library * reporter: Migrate to the new Common::FS library * telemetry_session: Migrate to the new Common::FS library * key_manager: Migrate to the new Common::FS library * bis_factory: Migrate to the new Common::FS library * registered_cache: Migrate to the new Common::FS library * xts_archive: Migrate to the new Common::FS library * service: acc: Migrate to the new Common::FS library * applets/profile: Migrate to the new Common::FS library * applets/web: Migrate to the new Common::FS library * service: filesystem: Migrate to the new Common::FS library * loader: Migrate to the new Common::FS library * gl_shader_disk_cache: Migrate to the new Common::FS library * nsight_aftermath_tracker: Migrate to the new Common::FS library * vulkan_library: Migrate to the new Common::FS library * configure_debug: Migrate to the new Common::FS library * game_list_worker: Migrate to the new Common::FS library * config: Migrate to the new Common::FS library * configure_filesystem: Migrate to the new Common::FS library * configure_per_game_addons: Migrate to the new Common::FS library * configure_profile_manager: Migrate to the new Common::FS library * configure_ui: Migrate to the new Common::FS library * input_profiles: Migrate to the new Common::FS library * yuzu_cmd: config: Migrate to the new Common::FS library * yuzu_cmd: Migrate to the new Common::FS library * vfs_real: Migrate to the new Common::FS library * vfs: Migrate to the new Common::FS library * vfs_libzip: Migrate to the new Common::FS library * service: bcat: Migrate to the new Common::FS library * yuzu: main: Migrate to the new Common::FS library * vfs_real: Delete the contents of an existing file in CreateFile Current usages of CreateFile expect to delete the contents of an existing file, retain this behavior for now. * input_profiles: Don't iterate the input profile dir if it does not exist Silences an error produced in the log if the directory does not exist. * game_list_worker: Skip parsing file if the returned VfsFile is nullptr Prevents crashes in GetLoader when the virtual file is nullptr * common: fs: Validate paths for path length * service: filesystem: Open the mod load directory as read only
This commit is contained in:
parent
08a5cf0b5b
commit
065867e2c2
74 changed files with 3785 additions and 2169 deletions
|
@ -3,7 +3,7 @@
|
|||
// Refer to the license.txt file included.
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include "common/file_util.h"
|
||||
#include "common/fs/path_util.h"
|
||||
#include "core/file_sys/bis_factory.h"
|
||||
#include "core/file_sys/mode.h"
|
||||
#include "core/file_sys/registered_cache.h"
|
||||
|
@ -85,7 +85,7 @@ VirtualFile BISFactory::OpenPartitionStorage(BisPartitionId id,
|
|||
VirtualFilesystem file_system) const {
|
||||
auto& keys = Core::Crypto::KeyManager::Instance();
|
||||
Core::Crypto::PartitionDataManager pdm{file_system->OpenDirectory(
|
||||
Common::FS::GetUserPath(Common::FS::UserPath::SysDataDir), Mode::Read)};
|
||||
Common::FS::GetYuzuPathString(Common::FS::YuzuPath::NANDDir), Mode::Read)};
|
||||
keys.PopulateFromPartitionData(pdm);
|
||||
|
||||
switch (id) {
|
||||
|
|
|
@ -10,11 +10,13 @@
|
|||
namespace FileSys {
|
||||
|
||||
enum class Mode : u32 {
|
||||
Read = 1,
|
||||
Write = 2,
|
||||
Read = 1 << 0,
|
||||
Write = 1 << 1,
|
||||
ReadWrite = Read | Write,
|
||||
Append = 4,
|
||||
Append = 1 << 2,
|
||||
ReadAppend = Read | Append,
|
||||
WriteAppend = Write | Append,
|
||||
All = ReadWrite | Append,
|
||||
};
|
||||
|
||||
DECLARE_ENUM_FLAG_OPERATORS(Mode)
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
#include <iterator>
|
||||
#include <utility>
|
||||
|
||||
#include "common/file_util.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "core/file_sys/partition_filesystem.h"
|
||||
#include "core/file_sys/vfs_offset.h"
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
#include <cstddef>
|
||||
#include <cstring>
|
||||
|
||||
#include "common/file_util.h"
|
||||
#include "common/hex_util.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/settings.h"
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include <regex>
|
||||
#include <mbedtls/sha256.h>
|
||||
#include "common/assert.h"
|
||||
#include "common/file_util.h"
|
||||
#include "common/fs/path_util.h"
|
||||
#include "common/hex_util.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "core/crypto/key_manager.h"
|
||||
|
|
|
@ -5,8 +5,7 @@
|
|||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include "common/common_paths.h"
|
||||
#include "common/file_util.h"
|
||||
#include "common/fs/path_util.h"
|
||||
#include "common/logging/backend.h"
|
||||
#include "core/file_sys/mode.h"
|
||||
#include "core/file_sys/vfs.h"
|
||||
|
@ -122,15 +121,14 @@ VirtualDir VfsFilesystem::CopyDirectory(std::string_view old_path_, std::string_
|
|||
return nullptr;
|
||||
|
||||
for (const auto& file : old_dir->GetFiles()) {
|
||||
const auto x =
|
||||
CopyFile(old_path + DIR_SEP + file->GetName(), new_path + DIR_SEP + file->GetName());
|
||||
const auto x = CopyFile(old_path + '/' + file->GetName(), new_path + '/' + file->GetName());
|
||||
if (x == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (const auto& dir : old_dir->GetSubdirectories()) {
|
||||
const auto x =
|
||||
CopyDirectory(old_path + DIR_SEP + dir->GetName(), new_path + DIR_SEP + dir->GetName());
|
||||
CopyDirectory(old_path + '/' + dir->GetName(), new_path + '/' + dir->GetName());
|
||||
if (x == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#include "common/fs/path_util.h"
|
||||
#include "common/logging/backend.h"
|
||||
#include "core/file_sys/vfs.h"
|
||||
#include "core/file_sys/vfs_libzip.h"
|
||||
|
|
|
@ -7,8 +7,9 @@
|
|||
#include <iterator>
|
||||
#include <utility>
|
||||
#include "common/assert.h"
|
||||
#include "common/common_paths.h"
|
||||
#include "common/file_util.h"
|
||||
#include "common/fs/file.h"
|
||||
#include "common/fs/fs.h"
|
||||
#include "common/fs/path_util.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "core/file_sys/vfs_real.h"
|
||||
|
||||
|
@ -16,33 +17,31 @@ namespace FileSys {
|
|||
|
||||
namespace FS = Common::FS;
|
||||
|
||||
static std::string ModeFlagsToString(Mode mode) {
|
||||
std::string mode_str;
|
||||
namespace {
|
||||
|
||||
// Calculate the correct open mode for the file.
|
||||
if (True(mode & Mode::Read) && True(mode & Mode::Write)) {
|
||||
if (True(mode & Mode::Append)) {
|
||||
mode_str = "a+";
|
||||
} else {
|
||||
mode_str = "r+";
|
||||
}
|
||||
} else {
|
||||
if (True(mode & Mode::Read)) {
|
||||
mode_str = "r";
|
||||
} else if (True(mode & Mode::Append)) {
|
||||
mode_str = "a";
|
||||
} else if (True(mode & Mode::Write)) {
|
||||
mode_str = "w";
|
||||
} else {
|
||||
UNREACHABLE_MSG("Invalid file open mode: {:02X}", static_cast<u8>(mode));
|
||||
}
|
||||
constexpr FS::FileAccessMode ModeFlagsToFileAccessMode(Mode mode) {
|
||||
switch (mode) {
|
||||
case Mode::Read:
|
||||
return FS::FileAccessMode::Read;
|
||||
case Mode::Write:
|
||||
return FS::FileAccessMode::Write;
|
||||
case Mode::ReadWrite:
|
||||
return FS::FileAccessMode::ReadWrite;
|
||||
case Mode::Append:
|
||||
return FS::FileAccessMode::Append;
|
||||
case Mode::ReadAppend:
|
||||
return FS::FileAccessMode::ReadAppend;
|
||||
case Mode::WriteAppend:
|
||||
return FS::FileAccessMode::Append;
|
||||
case Mode::All:
|
||||
return FS::FileAccessMode::ReadAppend;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
|
||||
mode_str += "b";
|
||||
|
||||
return mode_str;
|
||||
}
|
||||
|
||||
} // Anonymous namespace
|
||||
|
||||
RealVfsFilesystem::RealVfsFilesystem() : VfsFilesystem(nullptr) {}
|
||||
RealVfsFilesystem::~RealVfsFilesystem() = default;
|
||||
|
||||
|
@ -63,7 +62,7 @@ VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
|
|||
if (!FS::Exists(path)) {
|
||||
return VfsEntryType::None;
|
||||
}
|
||||
if (FS::IsDirectory(path)) {
|
||||
if (FS::IsDir(path)) {
|
||||
return VfsEntryType::Directory;
|
||||
}
|
||||
|
||||
|
@ -81,12 +80,13 @@ VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
|
|||
}
|
||||
}
|
||||
|
||||
if (!FS::Exists(path) && True(perms & Mode::WriteAppend)) {
|
||||
FS::CreateEmptyFile(path);
|
||||
auto backing = FS::FileOpen(path, ModeFlagsToFileAccessMode(perms), FS::FileType::BinaryFile);
|
||||
|
||||
if (!backing) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto backing = std::make_shared<FS::IOFile>(path, ModeFlagsToString(perms).c_str());
|
||||
cache.insert_or_assign(path, backing);
|
||||
cache.insert_or_assign(path, std::move(backing));
|
||||
|
||||
// Cannot use make_shared as RealVfsFile constructor is private
|
||||
return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, backing, path, perms));
|
||||
|
@ -94,25 +94,29 @@ VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
|
|||
|
||||
VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
|
||||
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::CreateEmptyFile(path)) {
|
||||
// Current usages of CreateFile expect to delete the contents of an existing file.
|
||||
if (FS::IsFile(path)) {
|
||||
FS::IOFile temp{path, FS::FileAccessMode::Write, FS::FileType::BinaryFile};
|
||||
|
||||
if (!temp.IsOpen()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
temp.Close();
|
||||
|
||||
return OpenFile(path, perms);
|
||||
}
|
||||
|
||||
if (!FS::NewFile(path)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return OpenFile(path, perms);
|
||||
}
|
||||
|
||||
VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) {
|
||||
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::Copy(old_path, new_path)) {
|
||||
return nullptr;
|
||||
}
|
||||
return OpenFile(new_path, Mode::ReadWrite);
|
||||
// Unused
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) {
|
||||
|
@ -127,13 +131,13 @@ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_
|
|||
file->Close();
|
||||
}
|
||||
|
||||
if (!FS::Exists(old_path) || FS::Exists(new_path) || FS::IsDirectory(old_path) ||
|
||||
!FS::Rename(old_path, new_path)) {
|
||||
if (!FS::RenameFile(old_path, new_path)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
cache.erase(old_path);
|
||||
if (file->Open(new_path, "r+b")) {
|
||||
file->Open(new_path, FS::FileAccessMode::Read, FS::FileType::BinaryFile);
|
||||
if (file->IsOpen()) {
|
||||
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);
|
||||
|
@ -157,7 +161,7 @@ bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
|
|||
cache.erase(path);
|
||||
}
|
||||
|
||||
return FS::Delete(path);
|
||||
return FS::RemoveFile(path);
|
||||
}
|
||||
|
||||
VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
|
||||
|
@ -168,12 +172,8 @@ VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms)
|
|||
|
||||
VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
|
||||
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)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!FS::CreateDirs(path)) {
|
||||
return nullptr;
|
||||
}
|
||||
// Cannot use make_shared as RealVfsDirectory constructor is private
|
||||
return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
|
||||
|
@ -181,13 +181,8 @@ VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms
|
|||
|
||||
VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_,
|
||||
std::string_view new_path_) {
|
||||
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)) {
|
||||
return nullptr;
|
||||
}
|
||||
FS::CopyDir(old_path, new_path);
|
||||
return OpenDirectory(new_path, Mode::ReadWrite);
|
||||
// Unused
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
|
||||
|
@ -195,8 +190,7 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
|
|||
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)) {
|
||||
if (!FS::RenameDir(old_path, new_path)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -208,7 +202,7 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
|
|||
|
||||
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()),
|
||||
auto file_new_path = FS::SanitizePath(new_path + '/' + kv.first.substr(old_path.size()),
|
||||
FS::DirectorySeparator::PlatformDefault);
|
||||
const auto& cached = cache[file_old_path];
|
||||
|
||||
|
@ -218,7 +212,8 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
|
|||
|
||||
auto file = cached.lock();
|
||||
cache.erase(file_old_path);
|
||||
if (file->Open(file_new_path, "r+b")) {
|
||||
file->Open(file_new_path, FS::FileAccessMode::Read, FS::FileType::BinaryFile);
|
||||
if (file->IsOpen()) {
|
||||
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);
|
||||
|
@ -245,15 +240,13 @@ bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
|
|||
cache.erase(kv.first);
|
||||
}
|
||||
|
||||
return FS::DeleteDirRecursively(path);
|
||||
return FS::RemoveDirRecursively(path);
|
||||
}
|
||||
|
||||
RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::shared_ptr<FS::IOFile> backing_,
|
||||
const std::string& path_, Mode perms_)
|
||||
: 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)),
|
||||
perms(perms_) {}
|
||||
path_components(FS::SplitPathComponents(path_)), perms(perms_) {}
|
||||
|
||||
RealVfsFile::~RealVfsFile() = default;
|
||||
|
||||
|
@ -266,7 +259,7 @@ std::size_t RealVfsFile::GetSize() const {
|
|||
}
|
||||
|
||||
bool RealVfsFile::Resize(std::size_t new_size) {
|
||||
return backing->Resize(new_size);
|
||||
return backing->SetSize(new_size);
|
||||
}
|
||||
|
||||
VirtualDir RealVfsFile::GetContainingDirectory() const {
|
||||
|
@ -274,33 +267,33 @@ VirtualDir RealVfsFile::GetContainingDirectory() const {
|
|||
}
|
||||
|
||||
bool RealVfsFile::IsWritable() const {
|
||||
return True(perms & Mode::WriteAppend);
|
||||
return True(perms & Mode::Write);
|
||||
}
|
||||
|
||||
bool RealVfsFile::IsReadable() const {
|
||||
return True(perms & Mode::ReadWrite);
|
||||
return True(perms & Mode::Read);
|
||||
}
|
||||
|
||||
std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
|
||||
if (!backing->Seek(static_cast<s64>(offset), SEEK_SET)) {
|
||||
if (!backing->Seek(static_cast<s64>(offset))) {
|
||||
return 0;
|
||||
}
|
||||
return backing->ReadBytes(data, length);
|
||||
return backing->ReadSpan(std::span{data, length});
|
||||
}
|
||||
|
||||
std::size_t RealVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
|
||||
if (!backing->Seek(static_cast<s64>(offset), SEEK_SET)) {
|
||||
if (!backing->Seek(static_cast<s64>(offset))) {
|
||||
return 0;
|
||||
}
|
||||
return backing->WriteBytes(data, length);
|
||||
return backing->WriteSpan(std::span{data, length});
|
||||
}
|
||||
|
||||
bool RealVfsFile::Rename(std::string_view name) {
|
||||
return base.MoveFile(path, parent_path + DIR_SEP + std::string(name)) != nullptr;
|
||||
return base.MoveFile(path, parent_path + '/' + std::string(name)) != nullptr;
|
||||
}
|
||||
|
||||
bool RealVfsFile::Close() {
|
||||
return backing->Close();
|
||||
void RealVfsFile::Close() {
|
||||
backing->Close();
|
||||
}
|
||||
|
||||
// TODO(DarkLordZach): MSVC would not let me combine the following two functions using 'if
|
||||
|
@ -313,15 +306,16 @@ std::vector<VirtualFile> RealVfsDirectory::IterateEntries<RealVfsFile, VfsFile>(
|
|||
}
|
||||
|
||||
std::vector<VirtualFile> out;
|
||||
FS::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 (!FS::IsDirectory(full_path)) {
|
||||
out.emplace_back(base.OpenFile(full_path, perms));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
const FS::DirEntryCallable callback = [this, &out](const std::filesystem::path& full_path) {
|
||||
const auto full_path_string = FS::PathToUTF8String(full_path);
|
||||
|
||||
out.emplace_back(base.OpenFile(full_path_string, perms));
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
FS::IterateDirEntries(path, callback, FS::DirEntryFilter::File);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
@ -333,42 +327,41 @@ std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDi
|
|||
}
|
||||
|
||||
std::vector<VirtualDir> out;
|
||||
FS::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 (FS::IsDirectory(full_path)) {
|
||||
out.emplace_back(base.OpenDirectory(full_path, perms));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
const FS::DirEntryCallable callback = [this, &out](const std::filesystem::path& full_path) {
|
||||
const auto full_path_string = FS::PathToUTF8String(full_path);
|
||||
|
||||
out.emplace_back(base.OpenDirectory(full_path_string, perms));
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
FS::IterateDirEntries(path, callback, FS::DirEntryFilter::Directory);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_, Mode perms_)
|
||||
: 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)),
|
||||
perms(perms_) {
|
||||
if (!FS::Exists(path) && True(perms & Mode::WriteAppend)) {
|
||||
FS::CreateDir(path);
|
||||
path_components(FS::SplitPathComponents(path)), perms(perms_) {
|
||||
if (!FS::Exists(path) && True(perms & Mode::Write)) {
|
||||
void(FS::CreateDirs(path));
|
||||
}
|
||||
}
|
||||
|
||||
RealVfsDirectory::~RealVfsDirectory() = default;
|
||||
|
||||
VirtualFile RealVfsDirectory::GetFileRelative(std::string_view relative_path) const {
|
||||
const auto full_path = FS::SanitizePath(path + DIR_SEP + std::string(relative_path));
|
||||
if (!FS::Exists(full_path) || FS::IsDirectory(full_path)) {
|
||||
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
||||
if (!FS::Exists(full_path) || FS::IsDir(full_path)) {
|
||||
return nullptr;
|
||||
}
|
||||
return base.OpenFile(full_path, perms);
|
||||
}
|
||||
|
||||
VirtualDir RealVfsDirectory::GetDirectoryRelative(std::string_view relative_path) const {
|
||||
const auto full_path = FS::SanitizePath(path + DIR_SEP + std::string(relative_path));
|
||||
if (!FS::Exists(full_path) || !FS::IsDirectory(full_path)) {
|
||||
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
||||
if (!FS::Exists(full_path) || !FS::IsDir(full_path)) {
|
||||
return nullptr;
|
||||
}
|
||||
return base.OpenDirectory(full_path, perms);
|
||||
|
@ -383,17 +376,20 @@ VirtualDir RealVfsDirectory::GetSubdirectory(std::string_view name) const {
|
|||
}
|
||||
|
||||
VirtualFile RealVfsDirectory::CreateFileRelative(std::string_view relative_path) {
|
||||
const auto full_path = FS::SanitizePath(path + DIR_SEP + std::string(relative_path));
|
||||
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
||||
if (!FS::CreateParentDirs(full_path)) {
|
||||
return nullptr;
|
||||
}
|
||||
return base.CreateFile(full_path, perms);
|
||||
}
|
||||
|
||||
VirtualDir RealVfsDirectory::CreateDirectoryRelative(std::string_view relative_path) {
|
||||
const auto full_path = FS::SanitizePath(path + DIR_SEP + std::string(relative_path));
|
||||
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
||||
return base.CreateDirectory(full_path, perms);
|
||||
}
|
||||
|
||||
bool RealVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
|
||||
const auto full_path = FS::SanitizePath(this->path + DIR_SEP + std::string(name));
|
||||
const auto full_path = FS::SanitizePath(this->path + '/' + std::string(name));
|
||||
return base.DeleteDirectory(full_path);
|
||||
}
|
||||
|
||||
|
@ -406,11 +402,11 @@ std::vector<VirtualDir> RealVfsDirectory::GetSubdirectories() const {
|
|||
}
|
||||
|
||||
bool RealVfsDirectory::IsWritable() const {
|
||||
return True(perms & Mode::WriteAppend);
|
||||
return True(perms & Mode::Write);
|
||||
}
|
||||
|
||||
bool RealVfsDirectory::IsReadable() const {
|
||||
return True(perms & Mode::ReadWrite);
|
||||
return True(perms & Mode::Read);
|
||||
}
|
||||
|
||||
std::string RealVfsDirectory::GetName() const {
|
||||
|
@ -426,27 +422,27 @@ VirtualDir RealVfsDirectory::GetParentDirectory() const {
|
|||
}
|
||||
|
||||
VirtualDir RealVfsDirectory::CreateSubdirectory(std::string_view name) {
|
||||
const std::string subdir_path = (path + DIR_SEP).append(name);
|
||||
const std::string subdir_path = (path + '/').append(name);
|
||||
return base.CreateDirectory(subdir_path, perms);
|
||||
}
|
||||
|
||||
VirtualFile RealVfsDirectory::CreateFile(std::string_view name) {
|
||||
const std::string file_path = (path + DIR_SEP).append(name);
|
||||
const std::string file_path = (path + '/').append(name);
|
||||
return base.CreateFile(file_path, perms);
|
||||
}
|
||||
|
||||
bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) {
|
||||
const std::string subdir_path = (path + DIR_SEP).append(name);
|
||||
const std::string subdir_path = (path + '/').append(name);
|
||||
return base.DeleteDirectory(subdir_path);
|
||||
}
|
||||
|
||||
bool RealVfsDirectory::DeleteFile(std::string_view name) {
|
||||
const std::string file_path = (path + DIR_SEP).append(name);
|
||||
const std::string file_path = (path + '/').append(name);
|
||||
return base.DeleteFile(file_path);
|
||||
}
|
||||
|
||||
bool RealVfsDirectory::Rename(std::string_view name) {
|
||||
const std::string new_name = (parent_path + DIR_SEP).append(name);
|
||||
const std::string new_name = (parent_path + '/').append(name);
|
||||
return base.MoveFile(path, new_name) != nullptr;
|
||||
}
|
||||
|
||||
|
@ -462,14 +458,17 @@ std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries()
|
|||
}
|
||||
|
||||
std::map<std::string, VfsEntryType, std::less<>> out;
|
||||
FS::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,
|
||||
FS::IsDirectory(full_path) ? VfsEntryType::Directory : VfsEntryType::File);
|
||||
return true;
|
||||
});
|
||||
|
||||
const FS::DirEntryCallable callback = [&out](const std::filesystem::path& full_path) {
|
||||
const auto filename = FS::PathToUTF8String(full_path.filename());
|
||||
|
||||
out.insert_or_assign(filename,
|
||||
FS::IsDir(full_path) ? VfsEntryType::Directory : VfsEntryType::File);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
FS::IterateDirEntries(path, callback);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
|
|
@ -61,14 +61,13 @@ private:
|
|||
RealVfsFile(RealVfsFilesystem& base, std::shared_ptr<Common::FS::IOFile> backing,
|
||||
const std::string& path, Mode perms = Mode::Read);
|
||||
|
||||
bool Close();
|
||||
void Close();
|
||||
|
||||
RealVfsFilesystem& base;
|
||||
std::shared_ptr<Common::FS::IOFile> backing;
|
||||
std::string path;
|
||||
std::string parent_path;
|
||||
std::vector<std::string> path_components;
|
||||
std::vector<std::string> parent_components;
|
||||
Mode perms;
|
||||
};
|
||||
|
||||
|
@ -110,7 +109,6 @@ private:
|
|||
std::string path;
|
||||
std::string parent_path;
|
||||
std::vector<std::string> path_components;
|
||||
std::vector<std::string> parent_components;
|
||||
Mode perms;
|
||||
};
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <mbedtls/md.h>
|
||||
#include <mbedtls/sha256.h>
|
||||
|
||||
#include "common/file_util.h"
|
||||
#include "common/fs/path_util.h"
|
||||
#include "common/hex_util.h"
|
||||
#include "common/string_util.h"
|
||||
#include "core/crypto/aes_util.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue