2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-19 03:07:11 +02:00
|
|
|
|
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"
|
2021-05-26 01:32:56 +02:00
|
|
|
#include "common/fs/file.h"
|
|
|
|
#include "common/fs/fs.h"
|
|
|
|
#include "common/fs/path_util.h"
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/file_sys/vfs_real.h"
|
|
|
|
|
2021-09-14 05:02:53 +02:00
|
|
|
// For FileTimeStampRaw
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define stat _stat64
|
|
|
|
#endif
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
namespace FileSys {
|
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
namespace FS = Common::FS;
|
|
|
|
|
2021-05-26 01:32:56 +02:00
|
|
|
namespace {
|
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
constexpr size_t MaxOpenFiles = 512;
|
|
|
|
|
2021-05-26 01:32:56 +02:00
|
|
|
constexpr FS::FileAccessMode ModeFlagsToFileAccessMode(Mode mode) {
|
|
|
|
switch (mode) {
|
|
|
|
case Mode::Read:
|
|
|
|
return FS::FileAccessMode::Read;
|
|
|
|
case Mode::Write:
|
|
|
|
case Mode::ReadWrite:
|
|
|
|
case Mode::Append:
|
|
|
|
case Mode::ReadAppend:
|
|
|
|
case Mode::WriteAppend:
|
|
|
|
case Mode::All:
|
2021-06-19 12:25:53 +02:00
|
|
|
return FS::FileAccessMode::ReadWrite;
|
2021-05-26 01:32:56 +02:00
|
|
|
default:
|
|
|
|
return {};
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:32:56 +02:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
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
|
|
|
}
|
2021-05-26 01:32:56 +02:00
|
|
|
if (FS::IsDir(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
|
|
|
|
2023-06-13 23:16:10 +02:00
|
|
|
if (auto it = cache.find(path); it != cache.end()) {
|
|
|
|
if (auto file = it->second.lock(); file) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 05:53:23 +02:00
|
|
|
auto reference = std::make_unique<FileReference>();
|
2023-06-05 00:44:50 +02:00
|
|
|
this->InsertReferenceIntoList(*reference);
|
2023-06-12 05:53:23 +02:00
|
|
|
|
2023-06-13 23:16:10 +02:00
|
|
|
auto file =
|
|
|
|
std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, std::move(reference), path, perms));
|
|
|
|
cache[path] = file;
|
|
|
|
|
|
|
|
return file;
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-06-13 23:16:10 +02:00
|
|
|
cache.erase(path);
|
|
|
|
|
2021-05-26 01:32:56 +02:00
|
|
|
// 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()) {
|
2018-08-12 21:55:44 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2021-05-26 01:32:56 +02:00
|
|
|
|
|
|
|
temp.Close();
|
|
|
|
|
|
|
|
return OpenFile(path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FS::NewFile(path)) {
|
|
|
|
return nullptr;
|
2018-08-12 21:55:44 +02:00
|
|
|
}
|
2021-05-26 01:32:56 +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_) {
|
2021-05-26 01:32:56 +02:00
|
|
|
// Unused
|
|
|
|
return nullptr;
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-06-13 23:16:10 +02:00
|
|
|
cache.erase(old_path);
|
|
|
|
cache.erase(new_path);
|
2023-06-05 00:44:50 +02:00
|
|
|
if (!FS::RenameFile(old_path, new_path)) {
|
2020-07-07 12:57:20 +02:00
|
|
|
return nullptr;
|
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);
|
2023-06-13 23:16:10 +02:00
|
|
|
cache.erase(path);
|
2021-05-26 01:32:56 +02:00
|
|
|
return FS::RemoveFile(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
|
|
|
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);
|
2021-05-26 01:32:56 +02:00
|
|
|
if (!FS::CreateDirs(path)) {
|
|
|
|
return nullptr;
|
2018-08-12 21:55:44 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_,
|
|
|
|
std::string_view new_path_) {
|
2021-05-26 01:32:56 +02:00
|
|
|
// Unused
|
|
|
|
return nullptr;
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-05-26 01:32:56 +02:00
|
|
|
if (!FS::RenameDir(old_path, new_path)) {
|
2018-08-03 17:50:00 +02:00
|
|
|
return nullptr;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2023-06-05 00:44:50 +02:00
|
|
|
return OpenDirectory(new_path, Mode::ReadWrite);
|
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
|
|
|
|
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
|
|
|
|
return FS::RemoveDirRecursively(path);
|
|
|
|
}
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
void RealVfsFilesystem::RefreshReference(const std::string& path, Mode perms,
|
|
|
|
FileReference& reference) {
|
|
|
|
// Temporarily remove from list.
|
|
|
|
this->RemoveReferenceFromList(reference);
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
// Restore file if needed.
|
|
|
|
if (!reference.file) {
|
|
|
|
this->EvictSingleReference();
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
reference.file =
|
|
|
|
FS::FileOpen(path, ModeFlagsToFileAccessMode(perms), FS::FileType::BinaryFile);
|
|
|
|
if (reference.file) {
|
|
|
|
num_open_files++;
|
2020-08-31 16:56:19 +02:00
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
// Reinsert into list.
|
|
|
|
this->InsertReferenceIntoList(reference);
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
void RealVfsFilesystem::DropReference(std::unique_ptr<FileReference>&& reference) {
|
|
|
|
// Remove from list.
|
|
|
|
this->RemoveReferenceFromList(*reference);
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
// Close the file.
|
|
|
|
if (reference->file) {
|
|
|
|
reference->file.reset();
|
|
|
|
num_open_files--;
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
void RealVfsFilesystem::EvictSingleReference() {
|
|
|
|
if (num_open_files < MaxOpenFiles || open_references.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and remove from list.
|
|
|
|
auto& reference = open_references.back();
|
|
|
|
this->RemoveReferenceFromList(reference);
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
// Close the file.
|
|
|
|
if (reference.file) {
|
|
|
|
reference.file.reset();
|
|
|
|
num_open_files--;
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2020-08-18 20:45:18 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
// Reinsert into closed list.
|
|
|
|
this->InsertReferenceIntoList(reference);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealVfsFilesystem::InsertReferenceIntoList(FileReference& reference) {
|
|
|
|
if (reference.file) {
|
|
|
|
open_references.push_front(reference);
|
|
|
|
} else {
|
|
|
|
closed_references.push_front(reference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealVfsFilesystem::RemoveReferenceFromList(FileReference& reference) {
|
|
|
|
if (reference.file) {
|
|
|
|
open_references.erase(open_references.iterator_to(reference));
|
|
|
|
} else {
|
|
|
|
closed_references.erase(closed_references.iterator_to(reference));
|
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::unique_ptr<FileReference> reference_,
|
2018-08-03 17:50:00 +02:00
|
|
|
const std::string& path_, Mode perms_)
|
2023-06-05 00:44:50 +02:00
|
|
|
: base(base_), reference(std::move(reference_)), path(path_),
|
|
|
|
parent_path(FS::GetParentPath(path_)), path_components(FS::SplitPathComponents(path_)),
|
|
|
|
perms(perms_) {}
|
2018-07-19 03:07:11 +02:00
|
|
|
|
2023-06-05 00:44:50 +02:00
|
|
|
RealVfsFile::~RealVfsFile() {
|
|
|
|
base.DropReference(std::move(reference));
|
|
|
|
}
|
2018-09-02 16:53:06 +02:00
|
|
|
|
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 {
|
2023-06-05 00:44:50 +02:00
|
|
|
base.RefreshReference(path, perms, *reference);
|
|
|
|
return reference->file ? reference->file->GetSize() : 0;
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
bool RealVfsFile::Resize(std::size_t new_size) {
|
2023-06-05 00:44:50 +02:00
|
|
|
base.RefreshReference(path, perms, *reference);
|
|
|
|
return reference->file ? reference->file->SetSize(new_size) : false;
|
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 {
|
2021-05-26 01:32:56 +02:00
|
|
|
return True(perms & Mode::Write);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsFile::IsReadable() const {
|
2021-05-26 01:32:56 +02:00
|
|
|
return True(perms & Mode::Read);
|
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 {
|
2023-06-05 00:44:50 +02:00
|
|
|
base.RefreshReference(path, perms, *reference);
|
|
|
|
if (!reference->file || !reference->file->Seek(static_cast<s64>(offset))) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return 0;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2023-06-05 00:44:50 +02:00
|
|
|
return reference->file->ReadSpan(std::span{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) {
|
2023-06-05 00:44:50 +02:00
|
|
|
base.RefreshReference(path, perms, *reference);
|
|
|
|
if (!reference->file || !reference->file->Seek(static_cast<s64>(offset))) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return 0;
|
2020-08-15 14:33:16 +02:00
|
|
|
}
|
2023-06-05 00:44:50 +02:00
|
|
|
return reference->file->WriteSpan(std::span{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) {
|
2021-05-26 01:32:56 +02:00
|
|
|
return base.MoveFile(path, parent_path + '/' + std::string(name)) != nullptr;
|
2018-08-03 17:50:00 +02:00
|
|
|
}
|
2018-07-22 07:23:29 +02:00
|
|
|
|
2018-08-03 17:50:00 +02:00
|
|
|
// 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;
|
2021-05-26 01:32:56 +02:00
|
|
|
|
|
|
|
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);
|
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;
|
2021-05-26 01:32:56 +02:00
|
|
|
|
|
|
|
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);
|
2018-08-03 17:50:00 +02:00
|
|
|
|
|
|
|
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)),
|
2021-05-26 01:32:56 +02:00
|
|
|
path_components(FS::SplitPathComponents(path)), perms(perms_) {
|
|
|
|
if (!FS::Exists(path) && True(perms & Mode::Write)) {
|
|
|
|
void(FS::CreateDirs(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;
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualFile RealVfsDirectory::GetFileRelative(std::string_view relative_path) const {
|
2021-05-26 01:32:56 +02:00
|
|
|
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
|
|
|
if (!FS::Exists(full_path) || FS::IsDir(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
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualDir RealVfsDirectory::GetDirectoryRelative(std::string_view relative_path) const {
|
2021-05-26 01:32:56 +02:00
|
|
|
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
|
|
|
if (!FS::Exists(full_path) || !FS::IsDir(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);
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualFile RealVfsDirectory::CreateFileRelative(std::string_view relative_path) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
|
|
|
if (!FS::CreateParentDirs(full_path)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.CreateFile(full_path, perms);
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:34:40 +02:00
|
|
|
VirtualDir RealVfsDirectory::CreateDirectoryRelative(std::string_view relative_path) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
|
2018-08-03 17:50:00 +02:00
|
|
|
return base.CreateDirectory(full_path, perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const auto full_path = FS::SanitizePath(this->path + '/' + 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
|
|
|
}
|
|
|
|
|
2021-09-14 05:02:53 +02:00
|
|
|
FileTimeStampRaw RealVfsDirectory::GetFileTimeStamp(std::string_view path_) const {
|
|
|
|
const auto full_path = FS::SanitizePath(path + '/' + std::string(path_));
|
|
|
|
const auto fs_path = std::filesystem::path{FS::ToU8String(full_path)};
|
|
|
|
struct stat file_status;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
const auto stat_result = _wstat64(fs_path.c_str(), &file_status);
|
|
|
|
#else
|
|
|
|
const auto stat_result = stat(fs_path.c_str(), &file_status);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (stat_result != 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
.created{static_cast<u64>(file_status.st_ctime)},
|
|
|
|
.accessed{static_cast<u64>(file_status.st_atime)},
|
|
|
|
.modified{static_cast<u64>(file_status.st_mtime)},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-26 01:32:56 +02:00
|
|
|
return True(perms & Mode::Write);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RealVfsDirectory::IsReadable() const {
|
2021-05-26 01:32:56 +02:00
|
|
|
return True(perms & Mode::Read);
|
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) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const std::string subdir_path = (path + '/').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) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const std::string file_path = (path + '/').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) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const std::string subdir_path = (path + '/').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) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const std::string file_path = (path + '/').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) {
|
2021-05-26 01:32:56 +02:00
|
|
|
const std::string new_name = (parent_path + '/').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;
|
2021-05-26 01:32:56 +02:00
|
|
|
|
|
|
|
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);
|
2018-09-20 03:54:14 +02:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
} // namespace FileSys
|