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.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
2018-08-03 17:46:30 +02:00
|
|
|
#include <string>
|
|
|
|
#include "common/common_paths.h"
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/logging/backend.h"
|
2018-08-21 16:48:24 +02:00
|
|
|
#include "core/file_sys/mode.h"
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "core/file_sys/vfs.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-08-03 17:46:30 +02:00
|
|
|
VfsFilesystem::VfsFilesystem(VirtualDir root_) : root(std::move(root_)) {}
|
|
|
|
|
|
|
|
VfsFilesystem::~VfsFilesystem() = default;
|
|
|
|
|
|
|
|
std::string VfsFilesystem::GetName() const {
|
|
|
|
return root->GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VfsFilesystem::IsReadable() const {
|
|
|
|
return root->IsReadable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VfsFilesystem::IsWritable() const {
|
|
|
|
return root->IsWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
VfsEntryType VfsFilesystem::GetEntryType(std::string_view path_) const {
|
|
|
|
const auto path = FileUtil::SanitizePath(path_);
|
|
|
|
if (root->GetFileRelative(path) != nullptr)
|
|
|
|
return VfsEntryType::File;
|
|
|
|
if (root->GetDirectoryRelative(path) != nullptr)
|
|
|
|
return VfsEntryType::Directory;
|
|
|
|
|
|
|
|
return VfsEntryType::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile VfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
|
|
|
|
const auto path = FileUtil::SanitizePath(path_);
|
|
|
|
return root->GetFileRelative(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile VfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
|
|
|
|
const auto path = FileUtil::SanitizePath(path_);
|
|
|
|
return root->CreateFileRelative(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile VfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) {
|
|
|
|
const auto old_path = FileUtil::SanitizePath(old_path_);
|
|
|
|
const auto new_path = FileUtil::SanitizePath(new_path_);
|
|
|
|
|
|
|
|
// VfsDirectory impls are only required to implement copy across the current directory.
|
|
|
|
if (FileUtil::GetParentPath(old_path) == FileUtil::GetParentPath(new_path)) {
|
|
|
|
if (!root->Copy(FileUtil::GetFilename(old_path), FileUtil::GetFilename(new_path)))
|
|
|
|
return nullptr;
|
|
|
|
return OpenFile(new_path, Mode::ReadWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do it using RawCopy. Non-default impls are encouraged to optimize this.
|
|
|
|
const auto old_file = OpenFile(old_path, Mode::Read);
|
|
|
|
if (old_file == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
auto new_file = OpenFile(new_path, Mode::Read);
|
|
|
|
if (new_file != nullptr)
|
|
|
|
return nullptr;
|
|
|
|
new_file = CreateFile(new_path, Mode::Write);
|
|
|
|
if (new_file == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
if (!VfsRawCopy(old_file, new_file))
|
|
|
|
return nullptr;
|
|
|
|
return new_file;
|
|
|
|
}
|
|
|
|
|
2018-08-12 10:02:27 +02:00
|
|
|
VirtualFile VfsFilesystem::MoveFile(std::string_view old_path, std::string_view new_path) {
|
|
|
|
const auto sanitized_old_path = FileUtil::SanitizePath(old_path);
|
|
|
|
const auto sanitized_new_path = FileUtil::SanitizePath(new_path);
|
2018-08-03 17:46:30 +02:00
|
|
|
|
|
|
|
// Again, non-default impls are highly encouraged to provide a more optimized version of this.
|
2018-08-12 10:02:27 +02:00
|
|
|
auto out = CopyFile(sanitized_old_path, sanitized_new_path);
|
2018-08-03 17:46:30 +02:00
|
|
|
if (out == nullptr)
|
|
|
|
return nullptr;
|
2018-08-12 10:02:27 +02:00
|
|
|
if (DeleteFile(sanitized_old_path))
|
2018-08-03 17:46:30 +02:00
|
|
|
return out;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VfsFilesystem::DeleteFile(std::string_view path_) {
|
|
|
|
const auto path = FileUtil::SanitizePath(path_);
|
|
|
|
auto parent = OpenDirectory(FileUtil::GetParentPath(path), Mode::Write);
|
|
|
|
if (parent == nullptr)
|
|
|
|
return false;
|
|
|
|
return parent->DeleteFile(FileUtil::GetFilename(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir VfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
|
|
|
|
const auto path = FileUtil::SanitizePath(path_);
|
|
|
|
return root->GetDirectoryRelative(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir VfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
|
|
|
|
const auto path = FileUtil::SanitizePath(path_);
|
|
|
|
return root->CreateDirectoryRelative(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualDir VfsFilesystem::CopyDirectory(std::string_view old_path_, std::string_view new_path_) {
|
|
|
|
const auto old_path = FileUtil::SanitizePath(old_path_);
|
|
|
|
const auto new_path = FileUtil::SanitizePath(new_path_);
|
|
|
|
|
|
|
|
// Non-default impls are highly encouraged to provide a more optimized version of this.
|
|
|
|
auto old_dir = OpenDirectory(old_path, Mode::Read);
|
|
|
|
if (old_dir == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
auto new_dir = OpenDirectory(new_path, Mode::Read);
|
|
|
|
if (new_dir != nullptr)
|
|
|
|
return nullptr;
|
|
|
|
new_dir = CreateDirectory(new_path, Mode::Write);
|
|
|
|
if (new_dir == nullptr)
|
|
|
|
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());
|
|
|
|
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());
|
|
|
|
if (x == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_dir;
|
|
|
|
}
|
|
|
|
|
2018-08-12 10:02:27 +02:00
|
|
|
VirtualDir VfsFilesystem::MoveDirectory(std::string_view old_path, std::string_view new_path) {
|
|
|
|
const auto sanitized_old_path = FileUtil::SanitizePath(old_path);
|
|
|
|
const auto sanitized_new_path = FileUtil::SanitizePath(new_path);
|
2018-08-03 17:46:30 +02:00
|
|
|
|
|
|
|
// Non-default impls are highly encouraged to provide a more optimized version of this.
|
2018-08-12 10:02:27 +02:00
|
|
|
auto out = CopyDirectory(sanitized_old_path, sanitized_new_path);
|
2018-08-03 17:46:30 +02:00
|
|
|
if (out == nullptr)
|
|
|
|
return nullptr;
|
2018-08-12 10:02:27 +02:00
|
|
|
if (DeleteDirectory(sanitized_old_path))
|
2018-08-03 17:46:30 +02:00
|
|
|
return out;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VfsFilesystem::DeleteDirectory(std::string_view path_) {
|
|
|
|
const auto path = FileUtil::SanitizePath(path_);
|
|
|
|
auto parent = OpenDirectory(FileUtil::GetParentPath(path), Mode::Write);
|
|
|
|
if (parent == nullptr)
|
|
|
|
return false;
|
|
|
|
return parent->DeleteSubdirectoryRecursive(FileUtil::GetFilename(path));
|
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
VfsFile::~VfsFile() = default;
|
|
|
|
|
|
|
|
std::string VfsFile::GetExtension() const {
|
2018-07-22 07:23:29 +02:00
|
|
|
return std::string(FileUtil::GetExtensionFromFilename(GetName()));
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VfsDirectory::~VfsDirectory() = default;
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<u8> VfsFile::ReadByte(std::size_t offset) const {
|
2018-07-19 03:07:11 +02:00
|
|
|
u8 out{};
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t size = Read(&out, 1, offset);
|
2018-07-19 03:07:11 +02:00
|
|
|
if (size == 1)
|
|
|
|
return out;
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
return {};
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::vector<u8> VfsFile::ReadBytes(std::size_t size, std::size_t offset) const {
|
2018-07-19 03:07:11 +02:00
|
|
|
std::vector<u8> out(size);
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t read_size = Read(out.data(), size, offset);
|
2018-07-19 03:07:11 +02:00
|
|
|
out.resize(read_size);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> VfsFile::ReadAllBytes() const {
|
|
|
|
return ReadBytes(GetSize());
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
bool VfsFile::WriteByte(u8 data, std::size_t offset) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return Write(&data, 1, offset) == 1;
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t VfsFile::WriteBytes(const std::vector<u8>& data, std::size_t offset) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return Write(data.data(), data.size(), offset);
|
|
|
|
}
|
|
|
|
|
2018-07-28 00:14:03 +02:00
|
|
|
std::string VfsFile::GetFullPath() const {
|
|
|
|
if (GetContainingDirectory() == nullptr)
|
|
|
|
return "/" + GetName();
|
|
|
|
|
|
|
|
return GetContainingDirectory()->GetFullPath() + "/" + GetName();
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsFile> VfsDirectory::GetFileRelative(std::string_view path) const {
|
2018-07-19 03:07:11 +02:00
|
|
|
auto vec = FileUtil::SplitPathComponents(path);
|
|
|
|
vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }),
|
|
|
|
vec.end());
|
2018-07-22 07:23:29 +02:00
|
|
|
if (vec.empty()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.size() == 1) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return GetFile(vec[0]);
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
auto dir = GetSubdirectory(vec[0]);
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t component = 1; component < vec.size() - 1; ++component) {
|
2018-07-22 07:23:29 +02:00
|
|
|
if (dir == nullptr) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
dir = dir->GetSubdirectory(vec[component]);
|
|
|
|
}
|
2018-07-22 07:23:29 +02:00
|
|
|
|
|
|
|
if (dir == nullptr) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
return dir->GetFile(vec.back());
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsFile> VfsDirectory::GetFileAbsolute(std::string_view path) const {
|
|
|
|
if (IsRoot()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return GetFileRelative(path);
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
|
|
|
|
return GetParentDirectory()->GetFileAbsolute(path);
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryRelative(std::string_view path) const {
|
2018-07-19 03:07:11 +02:00
|
|
|
auto vec = FileUtil::SplitPathComponents(path);
|
|
|
|
vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }),
|
|
|
|
vec.end());
|
2018-07-22 07:23:29 +02:00
|
|
|
if (vec.empty()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
// TODO(DarkLordZach): Return this directory if path is '/' or similar. Can't currently
|
|
|
|
// because of const-ness
|
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
auto dir = GetSubdirectory(vec[0]);
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t component = 1; component < vec.size(); ++component) {
|
2018-07-22 07:23:29 +02:00
|
|
|
if (dir == nullptr) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
dir = dir->GetSubdirectory(vec[component]);
|
|
|
|
}
|
2018-07-22 07:23:29 +02:00
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryAbsolute(std::string_view path) const {
|
|
|
|
if (IsRoot()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return GetDirectoryRelative(path);
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
|
|
|
|
return GetParentDirectory()->GetDirectoryAbsolute(path);
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsFile> VfsDirectory::GetFile(std::string_view name) const {
|
2018-07-19 03:07:11 +02:00
|
|
|
const auto& files = GetFiles();
|
|
|
|
const auto iter = std::find_if(files.begin(), files.end(),
|
|
|
|
[&name](const auto& file1) { return name == file1->GetName(); });
|
|
|
|
return iter == files.end() ? nullptr : *iter;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsDirectory> VfsDirectory::GetSubdirectory(std::string_view name) const {
|
2018-07-19 03:07:11 +02:00
|
|
|
const auto& subs = GetSubdirectories();
|
|
|
|
const auto iter = std::find_if(subs.begin(), subs.end(),
|
|
|
|
[&name](const auto& file1) { return name == file1->GetName(); });
|
|
|
|
return iter == subs.end() ? nullptr : *iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VfsDirectory::IsRoot() const {
|
|
|
|
return GetParentDirectory() == nullptr;
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t VfsDirectory::GetSize() const {
|
2018-07-19 03:07:11 +02:00
|
|
|
const auto& files = GetFiles();
|
2018-07-19 05:03:22 +02:00
|
|
|
const auto sum_sizes = [](const auto& range) {
|
|
|
|
return std::accumulate(range.begin(), range.end(), 0ULL,
|
|
|
|
[](const auto& f1, const auto& f2) { return f1 + f2->GetSize(); });
|
|
|
|
};
|
2018-07-19 03:07:11 +02:00
|
|
|
|
2018-07-19 05:03:22 +02:00
|
|
|
const auto file_total = sum_sizes(files);
|
2018-07-19 03:07:11 +02:00
|
|
|
const auto& sub_dir = GetSubdirectories();
|
2018-07-19 05:03:22 +02:00
|
|
|
const auto subdir_total = sum_sizes(sub_dir);
|
2018-07-19 03:07:11 +02:00
|
|
|
|
|
|
|
return file_total + subdir_total;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsFile> VfsDirectory::CreateFileRelative(std::string_view path) {
|
2018-07-19 03:07:11 +02:00
|
|
|
auto vec = FileUtil::SplitPathComponents(path);
|
|
|
|
vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }),
|
|
|
|
vec.end());
|
2018-07-22 07:23:29 +02:00
|
|
|
if (vec.empty()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.size() == 1) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return CreateFile(vec[0]);
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
auto dir = GetSubdirectory(vec[0]);
|
|
|
|
if (dir == nullptr) {
|
|
|
|
dir = CreateSubdirectory(vec[0]);
|
2018-07-22 07:23:29 +02:00
|
|
|
if (dir == nullptr) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return dir->CreateFileRelative(FileUtil::GetPathWithoutTop(path));
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsFile> VfsDirectory::CreateFileAbsolute(std::string_view path) {
|
|
|
|
if (IsRoot()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return CreateFileRelative(path);
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
return GetParentDirectory()->CreateFileAbsolute(path);
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsDirectory> VfsDirectory::CreateDirectoryRelative(std::string_view path) {
|
2018-07-19 03:07:11 +02:00
|
|
|
auto vec = FileUtil::SplitPathComponents(path);
|
|
|
|
vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }),
|
|
|
|
vec.end());
|
2018-07-22 07:23:29 +02:00
|
|
|
if (vec.empty()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.size() == 1) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return CreateSubdirectory(vec[0]);
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
auto dir = GetSubdirectory(vec[0]);
|
|
|
|
if (dir == nullptr) {
|
|
|
|
dir = CreateSubdirectory(vec[0]);
|
2018-07-22 07:23:29 +02:00
|
|
|
if (dir == nullptr) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
2018-07-22 07:23:29 +02:00
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
return dir->CreateDirectoryRelative(FileUtil::GetPathWithoutTop(path));
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsDirectory> VfsDirectory::CreateDirectoryAbsolute(std::string_view path) {
|
|
|
|
if (IsRoot()) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return CreateDirectoryRelative(path);
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
return GetParentDirectory()->CreateDirectoryAbsolute(path);
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool VfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
|
2018-07-19 03:07:11 +02:00
|
|
|
auto dir = GetSubdirectory(name);
|
2018-07-22 07:23:29 +02:00
|
|
|
if (dir == nullptr) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return false;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
for (const auto& file : dir->GetFiles()) {
|
2018-07-22 07:23:29 +02:00
|
|
|
if (!DeleteFile(file->GetName())) {
|
2018-07-19 03:07:11 +02:00
|
|
|
success = false;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& sdir : dir->GetSubdirectories()) {
|
2018-07-22 07:23:29 +02:00
|
|
|
if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) {
|
2018-07-19 03:07:11 +02:00
|
|
|
success = false;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:44:13 +01:00
|
|
|
bool VfsDirectory::CleanSubdirectoryRecursive(std::string_view name) {
|
|
|
|
auto dir = GetSubdirectory(name);
|
|
|
|
if (dir == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
for (const auto& file : dir->GetFiles()) {
|
|
|
|
if (!dir->DeleteFile(file->GetName())) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& sdir : dir->GetSubdirectories()) {
|
|
|
|
if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool VfsDirectory::Copy(std::string_view src, std::string_view dest) {
|
2018-07-19 03:07:11 +02:00
|
|
|
const auto f1 = GetFile(src);
|
|
|
|
auto f2 = CreateFile(dest);
|
2018-07-22 07:23:29 +02:00
|
|
|
if (f1 == nullptr || f2 == nullptr) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return false;
|
2018-07-22 07:23:29 +02:00
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
|
|
|
|
if (!f2->Resize(f1->GetSize())) {
|
|
|
|
DeleteFile(dest);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize();
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:50:16 +02:00
|
|
|
std::map<std::string, VfsEntryType, std::less<>> VfsDirectory::GetEntries() const {
|
|
|
|
std::map<std::string, VfsEntryType, std::less<>> out;
|
2018-09-20 03:54:14 +02:00
|
|
|
for (const auto& dir : GetSubdirectories())
|
|
|
|
out.emplace(dir->GetName(), VfsEntryType::Directory);
|
|
|
|
for (const auto& file : GetFiles())
|
|
|
|
out.emplace(file->GetName(), VfsEntryType::File);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-07-28 00:14:03 +02:00
|
|
|
std::string VfsDirectory::GetFullPath() const {
|
|
|
|
if (IsRoot())
|
|
|
|
return GetName();
|
|
|
|
|
|
|
|
return GetParentDirectory()->GetFullPath() + "/" + GetName();
|
|
|
|
}
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
bool ReadOnlyVfsDirectory::IsWritable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadOnlyVfsDirectory::IsReadable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectory::CreateSubdirectory(std::string_view name) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
std::shared_ptr<VfsFile> ReadOnlyVfsDirectory::CreateFile(std::string_view name) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-01 05:44:08 +01:00
|
|
|
std::shared_ptr<VfsFile> ReadOnlyVfsDirectory::CreateFileAbsolute(std::string_view path) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsFile> ReadOnlyVfsDirectory::CreateFileRelative(std::string_view path) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectory::CreateDirectoryAbsolute(std::string_view path) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectory::CreateDirectoryRelative(std::string_view path) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool ReadOnlyVfsDirectory::DeleteSubdirectory(std::string_view name) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-01 05:44:08 +01:00
|
|
|
bool ReadOnlyVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:44:13 +01:00
|
|
|
bool ReadOnlyVfsDirectory::CleanSubdirectoryRecursive(std::string_view name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool ReadOnlyVfsDirectory::DeleteFile(std::string_view name) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool ReadOnlyVfsDirectory::Rename(std::string_view name) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-28 00:14:03 +02:00
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, std::size_t block_size) {
|
2018-07-28 05:55:23 +02:00
|
|
|
if (file1->GetSize() != file2->GetSize())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::vector<u8> f1_v(block_size);
|
|
|
|
std::vector<u8> f2_v(block_size);
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < file1->GetSize(); i += block_size) {
|
2018-07-28 05:55:23 +02:00
|
|
|
auto f1_vs = file1->Read(f1_v.data(), block_size, i);
|
|
|
|
auto f2_vs = file2->Read(f2_v.data(), block_size, i);
|
|
|
|
|
|
|
|
if (f1_vs != f2_vs)
|
|
|
|
return false;
|
2018-07-29 03:39:42 +02:00
|
|
|
auto iters = std::mismatch(f1_v.begin(), f1_v.end(), f2_v.begin(), f2_v.end());
|
|
|
|
if (iters.first != f1_v.end() && iters.second != f2_v.end())
|
|
|
|
return false;
|
2018-07-28 05:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-25 23:56:14 +02:00
|
|
|
bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, std::size_t block_size) {
|
2018-09-20 03:55:47 +02:00
|
|
|
if (src == nullptr || dest == nullptr || !src->IsReadable() || !dest->IsWritable())
|
2018-07-28 00:14:03 +02:00
|
|
|
return false;
|
|
|
|
if (!dest->Resize(src->GetSize()))
|
|
|
|
return false;
|
2018-09-20 03:55:47 +02:00
|
|
|
|
|
|
|
std::vector<u8> temp(std::min(block_size, src->GetSize()));
|
2018-09-25 23:56:14 +02:00
|
|
|
for (std::size_t i = 0; i < src->GetSize(); i += block_size) {
|
2018-09-20 03:55:47 +02:00
|
|
|
const auto read = std::min(block_size, src->GetSize() - i);
|
|
|
|
|
2018-10-24 06:01:29 +02:00
|
|
|
if (src->Read(temp.data(), read, i) != read) {
|
2018-09-20 03:55:47 +02:00
|
|
|
return false;
|
2018-10-24 06:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dest->Write(temp.data(), read, i) != read) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-20 03:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-25 23:56:14 +02:00
|
|
|
bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, std::size_t block_size) {
|
2018-09-20 03:55:47 +02:00
|
|
|
if (src == nullptr || dest == nullptr || !src->IsReadable() || !dest->IsWritable())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const auto& file : src->GetFiles()) {
|
|
|
|
const auto out = dest->CreateFile(file->GetName());
|
|
|
|
if (!VfsRawCopy(file, out, block_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& dir : src->GetSubdirectories()) {
|
|
|
|
const auto out = dest->CreateSubdirectory(dir->GetName());
|
|
|
|
if (!VfsRawCopyD(dir, out, block_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-07-28 00:14:03 +02:00
|
|
|
}
|
2018-08-16 23:05:30 +02:00
|
|
|
|
|
|
|
VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path) {
|
|
|
|
const auto res = rel->GetDirectoryRelative(path);
|
|
|
|
if (res == nullptr)
|
|
|
|
return rel->CreateDirectoryRelative(path);
|
|
|
|
return res;
|
|
|
|
}
|
2018-07-19 03:07:11 +02:00
|
|
|
} // namespace FileSys
|