diff options
| author | 2018-08-11 10:35:47 +1000 | |
|---|---|---|
| committer | 2018-08-11 10:35:47 +1000 | |
| commit | b76ddb7647cbb390cce4143d91a1db171b0fa503 (patch) | |
| tree | a6e2e334e82b035923c41458150604dd5fb31d65 /src/core/file_sys | |
| parent | Added IsUserRegistrationRequestPermitted (diff) | |
| parent | Merge pull request #1007 from MerryMage/dynarmic (diff) | |
| download | yuzu-b76ddb7647cbb390cce4143d91a1db171b0fa503.tar.gz yuzu-b76ddb7647cbb390cce4143d91a1db171b0fa503.tar.xz yuzu-b76ddb7647cbb390cce4143d91a1db171b0fa503.zip | |
Merge remote-tracking branch 'origin/master' into better-account
Diffstat (limited to 'src/core/file_sys')
| -rw-r--r-- | src/core/file_sys/card_image.cpp | 1 | ||||
| -rw-r--r-- | src/core/file_sys/content_archive.cpp | 4 | ||||
| -rw-r--r-- | src/core/file_sys/content_archive.h | 1 | ||||
| -rw-r--r-- | src/core/file_sys/control_metadata.h | 7 | ||||
| -rw-r--r-- | src/core/file_sys/directory.h | 12 | ||||
| -rw-r--r-- | src/core/file_sys/savedata_factory.h | 1 | ||||
| -rw-r--r-- | src/core/file_sys/vfs.cpp | 148 | ||||
| -rw-r--r-- | src/core/file_sys/vfs.h | 66 | ||||
| -rw-r--r-- | src/core/file_sys/vfs_real.cpp | 341 | ||||
| -rw-r--r-- | src/core/file_sys/vfs_real.h | 56 |
10 files changed, 547 insertions, 90 deletions
diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index 395eea8ae..e897d9913 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #include <array> | 5 | #include <array> |
| 6 | #include <string> | 6 | #include <string> |
| 7 | #include <core/loader/loader.h> | 7 | #include <core/loader/loader.h> |
| 8 | #include "common/logging/log.h" | ||
| 8 | #include "core/file_sys/card_image.h" | 9 | #include "core/file_sys/card_image.h" |
| 9 | #include "core/file_sys/partition_filesystem.h" | 10 | #include "core/file_sys/partition_filesystem.h" |
| 10 | #include "core/file_sys/vfs_offset.h" | 11 | #include "core/file_sys/vfs_offset.h" |
diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp index 3529166ac..d3007d981 100644 --- a/src/core/file_sys/content_archive.cpp +++ b/src/core/file_sys/content_archive.cpp | |||
| @@ -170,6 +170,10 @@ VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting | |||
| 170 | } | 170 | } |
| 171 | 171 | ||
| 172 | NCA::NCA(VirtualFile file_) : file(std::move(file_)) { | 172 | NCA::NCA(VirtualFile file_) : file(std::move(file_)) { |
| 173 | if (file == nullptr) { | ||
| 174 | status = Loader::ResultStatus::ErrorInvalidFormat; | ||
| 175 | return; | ||
| 176 | } | ||
| 173 | if (sizeof(NCAHeader) != file->ReadObject(&header)) | 177 | if (sizeof(NCAHeader) != file->ReadObject(&header)) |
| 174 | LOG_ERROR(Loader, "File reader errored out during header read."); | 178 | LOG_ERROR(Loader, "File reader errored out during header read."); |
| 175 | 179 | ||
diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h index a8879d9a8..5cfd5031a 100644 --- a/src/core/file_sys/content_archive.h +++ b/src/core/file_sys/content_archive.h | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | #include "common/common_funcs.h" | 12 | #include "common/common_funcs.h" |
| 13 | #include "common/common_types.h" | 13 | #include "common/common_types.h" |
| 14 | #include "common/swap.h" | 14 | #include "common/swap.h" |
| 15 | #include "control_metadata.h" | ||
| 15 | #include "core/crypto/key_manager.h" | 16 | #include "core/crypto/key_manager.h" |
| 16 | #include "core/file_sys/partition_filesystem.h" | 17 | #include "core/file_sys/partition_filesystem.h" |
| 17 | #include "core/loader/loader.h" | 18 | #include "core/loader/loader.h" |
diff --git a/src/core/file_sys/control_metadata.h b/src/core/file_sys/control_metadata.h index cc3b745f7..6582cc240 100644 --- a/src/core/file_sys/control_metadata.h +++ b/src/core/file_sys/control_metadata.h | |||
| @@ -62,6 +62,13 @@ enum class Language : u8 { | |||
| 62 | Chinese = 14, | 62 | Chinese = 14, |
| 63 | }; | 63 | }; |
| 64 | 64 | ||
| 65 | static constexpr std::array<const char*, 15> LANGUAGE_NAMES = { | ||
| 66 | "AmericanEnglish", "BritishEnglish", "Japanese", | ||
| 67 | "French", "German", "LatinAmericanSpanish", | ||
| 68 | "Spanish", "Italian", "Dutch", | ||
| 69 | "CanadianFrench", "Portugese", "Russian", | ||
| 70 | "Korean", "Taiwanese", "Chinese"}; | ||
| 71 | |||
| 65 | // A class representing the format used by NX metadata files, typically named Control.nacp. | 72 | // A class representing the format used by NX metadata files, typically named Control.nacp. |
| 66 | // These store application name, dev name, title id, and other miscellaneous data. | 73 | // These store application name, dev name, title id, and other miscellaneous data. |
| 67 | class NACP { | 74 | class NACP { |
diff --git a/src/core/file_sys/directory.h b/src/core/file_sys/directory.h index 213ce1826..3759e743a 100644 --- a/src/core/file_sys/directory.h +++ b/src/core/file_sys/directory.h | |||
| @@ -4,8 +4,9 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | ||
| 8 | #include <cstddef> | 7 | #include <cstddef> |
| 8 | #include <iterator> | ||
| 9 | #include <string_view> | ||
| 9 | #include "common/common_funcs.h" | 10 | #include "common/common_funcs.h" |
| 10 | #include "common/common_types.h" | 11 | #include "common/common_types.h" |
| 11 | 12 | ||
| @@ -21,9 +22,14 @@ enum EntryType : u8 { | |||
| 21 | 22 | ||
| 22 | // Structure of a directory entry, from | 23 | // Structure of a directory entry, from |
| 23 | // http://switchbrew.org/index.php?title=Filesystem_services#DirectoryEntry | 24 | // http://switchbrew.org/index.php?title=Filesystem_services#DirectoryEntry |
| 24 | const size_t FILENAME_LENGTH = 0x300; | ||
| 25 | struct Entry { | 25 | struct Entry { |
| 26 | char filename[FILENAME_LENGTH]; | 26 | Entry(std::string_view view, EntryType entry_type, u64 entry_size) |
| 27 | : type{entry_type}, file_size{entry_size} { | ||
| 28 | const size_t copy_size = view.copy(filename, std::size(filename) - 1); | ||
| 29 | filename[copy_size] = '\0'; | ||
| 30 | } | ||
| 31 | |||
| 32 | char filename[0x300]; | ||
| 27 | INSERT_PADDING_BYTES(4); | 33 | INSERT_PADDING_BYTES(4); |
| 28 | EntryType type; | 34 | EntryType type; |
| 29 | INSERT_PADDING_BYTES(3); | 35 | INSERT_PADDING_BYTES(3); |
diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h index e3a578c0f..f3cf50d5a 100644 --- a/src/core/file_sys/savedata_factory.h +++ b/src/core/file_sys/savedata_factory.h | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include <string> | 8 | #include <string> |
| 9 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | #include "common/swap.h" | ||
| 10 | #include "core/hle/result.h" | 11 | #include "core/hle/result.h" |
| 11 | 12 | ||
| 12 | namespace FileSys { | 13 | namespace FileSys { |
diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index dae1c16ef..24e158962 100644 --- a/src/core/file_sys/vfs.cpp +++ b/src/core/file_sys/vfs.cpp | |||
| @@ -4,12 +4,160 @@ | |||
| 4 | 4 | ||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <numeric> | 6 | #include <numeric> |
| 7 | #include <string> | ||
| 8 | #include "common/common_paths.h" | ||
| 7 | #include "common/file_util.h" | 9 | #include "common/file_util.h" |
| 8 | #include "common/logging/backend.h" | 10 | #include "common/logging/backend.h" |
| 9 | #include "core/file_sys/vfs.h" | 11 | #include "core/file_sys/vfs.h" |
| 10 | 12 | ||
| 11 | namespace FileSys { | 13 | namespace FileSys { |
| 12 | 14 | ||
| 15 | VfsFilesystem::VfsFilesystem(VirtualDir root_) : root(std::move(root_)) {} | ||
| 16 | |||
| 17 | VfsFilesystem::~VfsFilesystem() = default; | ||
| 18 | |||
| 19 | std::string VfsFilesystem::GetName() const { | ||
| 20 | return root->GetName(); | ||
| 21 | } | ||
| 22 | |||
| 23 | bool VfsFilesystem::IsReadable() const { | ||
| 24 | return root->IsReadable(); | ||
| 25 | } | ||
| 26 | |||
| 27 | bool VfsFilesystem::IsWritable() const { | ||
| 28 | return root->IsWritable(); | ||
| 29 | } | ||
| 30 | |||
| 31 | VfsEntryType VfsFilesystem::GetEntryType(std::string_view path_) const { | ||
| 32 | const auto path = FileUtil::SanitizePath(path_); | ||
| 33 | if (root->GetFileRelative(path) != nullptr) | ||
| 34 | return VfsEntryType::File; | ||
| 35 | if (root->GetDirectoryRelative(path) != nullptr) | ||
| 36 | return VfsEntryType::Directory; | ||
| 37 | |||
| 38 | return VfsEntryType::None; | ||
| 39 | } | ||
| 40 | |||
| 41 | VirtualFile VfsFilesystem::OpenFile(std::string_view path_, Mode perms) { | ||
| 42 | const auto path = FileUtil::SanitizePath(path_); | ||
| 43 | return root->GetFileRelative(path); | ||
| 44 | } | ||
| 45 | |||
| 46 | VirtualFile VfsFilesystem::CreateFile(std::string_view path_, Mode perms) { | ||
| 47 | const auto path = FileUtil::SanitizePath(path_); | ||
| 48 | return root->CreateFileRelative(path); | ||
| 49 | } | ||
| 50 | |||
| 51 | VirtualFile VfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) { | ||
| 52 | const auto old_path = FileUtil::SanitizePath(old_path_); | ||
| 53 | const auto new_path = FileUtil::SanitizePath(new_path_); | ||
| 54 | |||
| 55 | // VfsDirectory impls are only required to implement copy across the current directory. | ||
| 56 | if (FileUtil::GetParentPath(old_path) == FileUtil::GetParentPath(new_path)) { | ||
| 57 | if (!root->Copy(FileUtil::GetFilename(old_path), FileUtil::GetFilename(new_path))) | ||
| 58 | return nullptr; | ||
| 59 | return OpenFile(new_path, Mode::ReadWrite); | ||
| 60 | } | ||
| 61 | |||
| 62 | // Do it using RawCopy. Non-default impls are encouraged to optimize this. | ||
| 63 | const auto old_file = OpenFile(old_path, Mode::Read); | ||
| 64 | if (old_file == nullptr) | ||
| 65 | return nullptr; | ||
| 66 | auto new_file = OpenFile(new_path, Mode::Read); | ||
| 67 | if (new_file != nullptr) | ||
| 68 | return nullptr; | ||
| 69 | new_file = CreateFile(new_path, Mode::Write); | ||
| 70 | if (new_file == nullptr) | ||
| 71 | return nullptr; | ||
| 72 | if (!VfsRawCopy(old_file, new_file)) | ||
| 73 | return nullptr; | ||
| 74 | return new_file; | ||
| 75 | } | ||
| 76 | |||
| 77 | VirtualFile VfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) { | ||
| 78 | const auto old_path = FileUtil::SanitizePath(old_path_); | ||
| 79 | const auto new_path = FileUtil::SanitizePath(new_path_); | ||
| 80 | |||
| 81 | // Again, non-default impls are highly encouraged to provide a more optimized version of this. | ||
| 82 | auto out = CopyFile(old_path_, new_path_); | ||
| 83 | if (out == nullptr) | ||
| 84 | return nullptr; | ||
| 85 | if (DeleteFile(old_path)) | ||
| 86 | return out; | ||
| 87 | return nullptr; | ||
| 88 | } | ||
| 89 | |||
| 90 | bool VfsFilesystem::DeleteFile(std::string_view path_) { | ||
| 91 | const auto path = FileUtil::SanitizePath(path_); | ||
| 92 | auto parent = OpenDirectory(FileUtil::GetParentPath(path), Mode::Write); | ||
| 93 | if (parent == nullptr) | ||
| 94 | return false; | ||
| 95 | return parent->DeleteFile(FileUtil::GetFilename(path)); | ||
| 96 | } | ||
| 97 | |||
| 98 | VirtualDir VfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) { | ||
| 99 | const auto path = FileUtil::SanitizePath(path_); | ||
| 100 | return root->GetDirectoryRelative(path); | ||
| 101 | } | ||
| 102 | |||
| 103 | VirtualDir VfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) { | ||
| 104 | const auto path = FileUtil::SanitizePath(path_); | ||
| 105 | return root->CreateDirectoryRelative(path); | ||
| 106 | } | ||
| 107 | |||
| 108 | VirtualDir VfsFilesystem::CopyDirectory(std::string_view old_path_, std::string_view new_path_) { | ||
| 109 | const auto old_path = FileUtil::SanitizePath(old_path_); | ||
| 110 | const auto new_path = FileUtil::SanitizePath(new_path_); | ||
| 111 | |||
| 112 | // Non-default impls are highly encouraged to provide a more optimized version of this. | ||
| 113 | auto old_dir = OpenDirectory(old_path, Mode::Read); | ||
| 114 | if (old_dir == nullptr) | ||
| 115 | return nullptr; | ||
| 116 | auto new_dir = OpenDirectory(new_path, Mode::Read); | ||
| 117 | if (new_dir != nullptr) | ||
| 118 | return nullptr; | ||
| 119 | new_dir = CreateDirectory(new_path, Mode::Write); | ||
| 120 | if (new_dir == nullptr) | ||
| 121 | return nullptr; | ||
| 122 | |||
| 123 | for (const auto& file : old_dir->GetFiles()) { | ||
| 124 | const auto x = | ||
| 125 | CopyFile(old_path + DIR_SEP + file->GetName(), new_path + DIR_SEP + file->GetName()); | ||
| 126 | if (x == nullptr) | ||
| 127 | return nullptr; | ||
| 128 | } | ||
| 129 | |||
| 130 | for (const auto& dir : old_dir->GetSubdirectories()) { | ||
| 131 | const auto x = | ||
| 132 | CopyDirectory(old_path + DIR_SEP + dir->GetName(), new_path + DIR_SEP + dir->GetName()); | ||
| 133 | if (x == nullptr) | ||
| 134 | return nullptr; | ||
| 135 | } | ||
| 136 | |||
| 137 | return new_dir; | ||
| 138 | } | ||
| 139 | |||
| 140 | VirtualDir VfsFilesystem::MoveDirectory(std::string_view old_path_, std::string_view new_path_) { | ||
| 141 | const auto old_path = FileUtil::SanitizePath(old_path_); | ||
| 142 | const auto new_path = FileUtil::SanitizePath(new_path_); | ||
| 143 | |||
| 144 | // Non-default impls are highly encouraged to provide a more optimized version of this. | ||
| 145 | auto out = CopyDirectory(old_path_, new_path_); | ||
| 146 | if (out == nullptr) | ||
| 147 | return nullptr; | ||
| 148 | if (DeleteDirectory(old_path)) | ||
| 149 | return out; | ||
| 150 | return nullptr; | ||
| 151 | } | ||
| 152 | |||
| 153 | bool VfsFilesystem::DeleteDirectory(std::string_view path_) { | ||
| 154 | const auto path = FileUtil::SanitizePath(path_); | ||
| 155 | auto parent = OpenDirectory(FileUtil::GetParentPath(path), Mode::Write); | ||
| 156 | if (parent == nullptr) | ||
| 157 | return false; | ||
| 158 | return parent->DeleteSubdirectoryRecursive(FileUtil::GetFilename(path)); | ||
| 159 | } | ||
| 160 | |||
| 13 | VfsFile::~VfsFile() = default; | 161 | VfsFile::~VfsFile() = default; |
| 14 | 162 | ||
| 15 | std::string VfsFile::GetExtension() const { | 163 | std::string VfsFile::GetExtension() const { |
diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index fab9e2b45..141a053ce 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h | |||
| @@ -11,14 +11,74 @@ | |||
| 11 | #include <vector> | 11 | #include <vector> |
| 12 | #include "boost/optional.hpp" | 12 | #include "boost/optional.hpp" |
| 13 | #include "common/common_types.h" | 13 | #include "common/common_types.h" |
| 14 | #include "core/file_sys/mode.h" | ||
| 14 | 15 | ||
| 15 | namespace FileSys { | 16 | namespace FileSys { |
| 17 | |||
| 18 | struct VfsFilesystem; | ||
| 16 | struct VfsFile; | 19 | struct VfsFile; |
| 17 | struct VfsDirectory; | 20 | struct VfsDirectory; |
| 18 | 21 | ||
| 19 | // Convenience typedefs to use VfsDirectory and VfsFile | 22 | // Convenience typedefs to use Vfs* interfaces |
| 20 | using VirtualDir = std::shared_ptr<FileSys::VfsDirectory>; | 23 | using VirtualFilesystem = std::shared_ptr<VfsFilesystem>; |
| 21 | using VirtualFile = std::shared_ptr<FileSys::VfsFile>; | 24 | using VirtualDir = std::shared_ptr<VfsDirectory>; |
| 25 | using VirtualFile = std::shared_ptr<VfsFile>; | ||
| 26 | |||
| 27 | // An enumeration representing what can be at the end of a path in a VfsFilesystem | ||
| 28 | enum class VfsEntryType { | ||
| 29 | None, | ||
| 30 | File, | ||
| 31 | Directory, | ||
| 32 | }; | ||
| 33 | |||
| 34 | // A class representing an abstract filesystem. A default implementation given the root VirtualDir | ||
| 35 | // is provided for convenience, but if the Vfs implementation has any additional state or | ||
| 36 | // functionality, they will need to override. | ||
| 37 | struct VfsFilesystem : NonCopyable { | ||
| 38 | VfsFilesystem(VirtualDir root); | ||
| 39 | virtual ~VfsFilesystem(); | ||
| 40 | |||
| 41 | // Gets the friendly name for the filesystem. | ||
| 42 | virtual std::string GetName() const; | ||
| 43 | |||
| 44 | // Return whether or not the user has read permissions on this filesystem. | ||
| 45 | virtual bool IsReadable() const; | ||
| 46 | // Return whether or not the user has write permission on this filesystem. | ||
| 47 | virtual bool IsWritable() const; | ||
| 48 | |||
| 49 | // Determine if the entry at path is non-existant, a file, or a directory. | ||
| 50 | virtual VfsEntryType GetEntryType(std::string_view path) const; | ||
| 51 | |||
| 52 | // Opens the file with path relative to root. If it doesn't exist, returns nullptr. | ||
| 53 | virtual VirtualFile OpenFile(std::string_view path, Mode perms); | ||
| 54 | // Creates a new, empty file at path | ||
| 55 | virtual VirtualFile CreateFile(std::string_view path, Mode perms); | ||
| 56 | // Copies the file from old_path to new_path, returning the new file on success and nullptr on | ||
| 57 | // failure. | ||
| 58 | virtual VirtualFile CopyFile(std::string_view old_path, std::string_view new_path); | ||
| 59 | // Moves the file from old_path to new_path, returning the moved file on success and nullptr on | ||
| 60 | // failure. | ||
| 61 | virtual VirtualFile MoveFile(std::string_view old_path, std::string_view new_path); | ||
| 62 | // Deletes the file with path relative to root, returing true on success. | ||
| 63 | virtual bool DeleteFile(std::string_view path); | ||
| 64 | |||
| 65 | // Opens the directory with path relative to root. If it doesn't exist, returns nullptr. | ||
| 66 | virtual VirtualDir OpenDirectory(std::string_view path, Mode perms); | ||
| 67 | // Creates a new, empty directory at path | ||
| 68 | virtual VirtualDir CreateDirectory(std::string_view path, Mode perms); | ||
| 69 | // Copies the directory from old_path to new_path, returning the new directory on success and | ||
| 70 | // nullptr on failure. | ||
| 71 | virtual VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path); | ||
| 72 | // Moves the directory from old_path to new_path, returning the moved directory on success and | ||
| 73 | // nullptr on failure. | ||
| 74 | virtual VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path); | ||
| 75 | // Deletes the directory with path relative to root, returing true on success. | ||
| 76 | virtual bool DeleteDirectory(std::string_view path); | ||
| 77 | |||
| 78 | protected: | ||
| 79 | // Root directory in default implementation. | ||
| 80 | VirtualDir root; | ||
| 81 | }; | ||
| 22 | 82 | ||
| 23 | // A class representing a file in an abstract filesystem. | 83 | // A class representing a file in an abstract filesystem. |
| 24 | struct VfsFile : NonCopyable { | 84 | struct VfsFile : NonCopyable { |
diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp index 82d54da4a..1b5919737 100644 --- a/src/core/file_sys/vfs_real.cpp +++ b/src/core/file_sys/vfs_real.cpp | |||
| @@ -6,7 +6,7 @@ | |||
| 6 | #include <cstddef> | 6 | #include <cstddef> |
| 7 | #include <iterator> | 7 | #include <iterator> |
| 8 | #include <utility> | 8 | #include <utility> |
| 9 | 9 | #include "common/assert.h" | |
| 10 | #include "common/common_paths.h" | 10 | #include "common/common_paths.h" |
| 11 | #include "common/logging/log.h" | 11 | #include "common/logging/log.h" |
| 12 | #include "core/file_sys/vfs_real.h" | 12 | #include "core/file_sys/vfs_real.h" |
| @@ -29,6 +29,8 @@ static std::string ModeFlagsToString(Mode mode) { | |||
| 29 | mode_str = "a"; | 29 | mode_str = "a"; |
| 30 | else if (mode & Mode::Write) | 30 | else if (mode & Mode::Write) |
| 31 | mode_str = "w"; | 31 | mode_str = "w"; |
| 32 | else | ||
| 33 | UNREACHABLE_MSG("Invalid file open mode: {:02X}", static_cast<u8>(mode)); | ||
| 32 | } | 34 | } |
| 33 | 35 | ||
| 34 | mode_str += "b"; | 36 | mode_str += "b"; |
| @@ -36,8 +38,174 @@ static std::string ModeFlagsToString(Mode mode) { | |||
| 36 | return mode_str; | 38 | return mode_str; |
| 37 | } | 39 | } |
| 38 | 40 | ||
| 39 | RealVfsFile::RealVfsFile(const std::string& path_, Mode perms_) | 41 | RealVfsFilesystem::RealVfsFilesystem() : VfsFilesystem(nullptr) {} |
| 40 | : backing(path_, ModeFlagsToString(perms_).c_str()), path(path_), | 42 | |
| 43 | std::string RealVfsFilesystem::GetName() const { | ||
| 44 | return "Real"; | ||
| 45 | } | ||
| 46 | |||
| 47 | bool RealVfsFilesystem::IsReadable() const { | ||
| 48 | return true; | ||
| 49 | } | ||
| 50 | |||
| 51 | bool RealVfsFilesystem::IsWritable() const { | ||
| 52 | return true; | ||
| 53 | } | ||
| 54 | |||
| 55 | VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const { | ||
| 56 | const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 57 | if (!FileUtil::Exists(path)) | ||
| 58 | return VfsEntryType::None; | ||
| 59 | if (FileUtil::IsDirectory(path)) | ||
| 60 | return VfsEntryType::Directory; | ||
| 61 | |||
| 62 | return VfsEntryType::File; | ||
| 63 | } | ||
| 64 | |||
| 65 | VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) { | ||
| 66 | const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 67 | if (cache.find(path) != cache.end()) { | ||
| 68 | auto weak = cache[path]; | ||
| 69 | if (!weak.expired()) { | ||
| 70 | return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, weak.lock(), path, perms)); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | if (!FileUtil::Exists(path) && (perms & Mode::WriteAppend) != 0) | ||
| 75 | FileUtil::CreateEmptyFile(path); | ||
| 76 | |||
| 77 | auto backing = std::make_shared<FileUtil::IOFile>(path, ModeFlagsToString(perms).c_str()); | ||
| 78 | cache[path] = backing; | ||
| 79 | |||
| 80 | // Cannot use make_shared as RealVfsFile constructor is private | ||
| 81 | return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, backing, path, perms)); | ||
| 82 | } | ||
| 83 | |||
| 84 | VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) { | ||
| 85 | const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 86 | if (!FileUtil::Exists(path) && !FileUtil::CreateEmptyFile(path)) | ||
| 87 | return nullptr; | ||
| 88 | return OpenFile(path, perms); | ||
| 89 | } | ||
| 90 | |||
| 91 | VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) { | ||
| 92 | const auto old_path = | ||
| 93 | FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 94 | const auto new_path = | ||
| 95 | FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 96 | |||
| 97 | if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || | ||
| 98 | FileUtil::IsDirectory(old_path) || !FileUtil::Copy(old_path, new_path)) | ||
| 99 | return nullptr; | ||
| 100 | return OpenFile(new_path, Mode::ReadWrite); | ||
| 101 | } | ||
| 102 | |||
| 103 | VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) { | ||
| 104 | const auto old_path = | ||
| 105 | FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 106 | const auto new_path = | ||
| 107 | FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 108 | |||
| 109 | if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || | ||
| 110 | FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path)) | ||
| 111 | return nullptr; | ||
| 112 | |||
| 113 | if (cache.find(old_path) != cache.end()) { | ||
| 114 | auto cached = cache[old_path]; | ||
| 115 | if (!cached.expired()) { | ||
| 116 | auto file = cached.lock(); | ||
| 117 | file->Open(new_path, "r+b"); | ||
| 118 | cache.erase(old_path); | ||
| 119 | cache[new_path] = file; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | return OpenFile(new_path, Mode::ReadWrite); | ||
| 123 | } | ||
| 124 | |||
| 125 | bool RealVfsFilesystem::DeleteFile(std::string_view path_) { | ||
| 126 | const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 127 | if (cache.find(path) != cache.end()) { | ||
| 128 | if (!cache[path].expired()) | ||
| 129 | cache[path].lock()->Close(); | ||
| 130 | cache.erase(path); | ||
| 131 | } | ||
| 132 | return FileUtil::Delete(path); | ||
| 133 | } | ||
| 134 | |||
| 135 | VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) { | ||
| 136 | const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 137 | // Cannot use make_shared as RealVfsDirectory constructor is private | ||
| 138 | return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms)); | ||
| 139 | } | ||
| 140 | |||
| 141 | VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) { | ||
| 142 | const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 143 | if (!FileUtil::Exists(path) && !FileUtil::CreateDir(path)) | ||
| 144 | return nullptr; | ||
| 145 | // Cannot use make_shared as RealVfsDirectory constructor is private | ||
| 146 | return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms)); | ||
| 147 | } | ||
| 148 | |||
| 149 | VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_, | ||
| 150 | std::string_view new_path_) { | ||
| 151 | const auto old_path = | ||
| 152 | FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 153 | const auto new_path = | ||
| 154 | FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 155 | if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || | ||
| 156 | !FileUtil::IsDirectory(old_path)) | ||
| 157 | return nullptr; | ||
| 158 | FileUtil::CopyDir(old_path, new_path); | ||
| 159 | return OpenDirectory(new_path, Mode::ReadWrite); | ||
| 160 | } | ||
| 161 | |||
| 162 | VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_, | ||
| 163 | std::string_view new_path_) { | ||
| 164 | const auto old_path = | ||
| 165 | FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 166 | const auto new_path = | ||
| 167 | FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 168 | if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || | ||
| 169 | FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path)) | ||
| 170 | return nullptr; | ||
| 171 | |||
| 172 | for (auto& kv : cache) { | ||
| 173 | // Path in cache starts with old_path | ||
| 174 | if (kv.first.rfind(old_path, 0) == 0) { | ||
| 175 | const auto file_old_path = | ||
| 176 | FileUtil::SanitizePath(kv.first, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 177 | const auto file_new_path = | ||
| 178 | FileUtil::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()), | ||
| 179 | FileUtil::DirectorySeparator::PlatformDefault); | ||
| 180 | auto cached = cache[file_old_path]; | ||
| 181 | if (!cached.expired()) { | ||
| 182 | auto file = cached.lock(); | ||
| 183 | file->Open(file_new_path, "r+b"); | ||
| 184 | cache.erase(file_old_path); | ||
| 185 | cache[file_new_path] = file; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | return OpenDirectory(new_path, Mode::ReadWrite); | ||
| 191 | } | ||
| 192 | |||
| 193 | bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) { | ||
| 194 | const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault); | ||
| 195 | for (auto& kv : cache) { | ||
| 196 | // Path in cache starts with old_path | ||
| 197 | if (kv.first.rfind(path, 0) == 0) { | ||
| 198 | if (!cache[kv.first].expired()) | ||
| 199 | cache[kv.first].lock()->Close(); | ||
| 200 | cache.erase(kv.first); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | return FileUtil::DeleteDirRecursively(path); | ||
| 204 | } | ||
| 205 | |||
| 206 | RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::shared_ptr<FileUtil::IOFile> backing_, | ||
| 207 | const std::string& path_, Mode perms_) | ||
| 208 | : base(base_), backing(std::move(backing_)), path(path_), | ||
| 41 | parent_path(FileUtil::GetParentPath(path_)), | 209 | parent_path(FileUtil::GetParentPath(path_)), |
| 42 | path_components(FileUtil::SplitPathComponents(path_)), | 210 | path_components(FileUtil::SplitPathComponents(path_)), |
| 43 | parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)), | 211 | parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)), |
| @@ -48,15 +216,15 @@ std::string RealVfsFile::GetName() const { | |||
| 48 | } | 216 | } |
| 49 | 217 | ||
| 50 | size_t RealVfsFile::GetSize() const { | 218 | size_t RealVfsFile::GetSize() const { |
| 51 | return backing.GetSize(); | 219 | return backing->GetSize(); |
| 52 | } | 220 | } |
| 53 | 221 | ||
| 54 | bool RealVfsFile::Resize(size_t new_size) { | 222 | bool RealVfsFile::Resize(size_t new_size) { |
| 55 | return backing.Resize(new_size); | 223 | return backing->Resize(new_size); |
| 56 | } | 224 | } |
| 57 | 225 | ||
| 58 | std::shared_ptr<VfsDirectory> RealVfsFile::GetContainingDirectory() const { | 226 | std::shared_ptr<VfsDirectory> RealVfsFile::GetContainingDirectory() const { |
| 59 | return std::make_shared<RealVfsDirectory>(parent_path, perms); | 227 | return base.OpenDirectory(parent_path, perms); |
| 60 | } | 228 | } |
| 61 | 229 | ||
| 62 | bool RealVfsFile::IsWritable() const { | 230 | bool RealVfsFile::IsWritable() const { |
| @@ -68,62 +236,118 @@ bool RealVfsFile::IsReadable() const { | |||
| 68 | } | 236 | } |
| 69 | 237 | ||
| 70 | size_t RealVfsFile::Read(u8* data, size_t length, size_t offset) const { | 238 | size_t RealVfsFile::Read(u8* data, size_t length, size_t offset) const { |
| 71 | if (!backing.Seek(offset, SEEK_SET)) | 239 | if (!backing->Seek(offset, SEEK_SET)) |
| 72 | return 0; | 240 | return 0; |
| 73 | return backing.ReadBytes(data, length); | 241 | return backing->ReadBytes(data, length); |
| 74 | } | 242 | } |
| 75 | 243 | ||
| 76 | size_t RealVfsFile::Write(const u8* data, size_t length, size_t offset) { | 244 | size_t RealVfsFile::Write(const u8* data, size_t length, size_t offset) { |
| 77 | if (!backing.Seek(offset, SEEK_SET)) | 245 | if (!backing->Seek(offset, SEEK_SET)) |
| 78 | return 0; | 246 | return 0; |
| 79 | return backing.WriteBytes(data, length); | 247 | return backing->WriteBytes(data, length); |
| 80 | } | 248 | } |
| 81 | 249 | ||
| 82 | bool RealVfsFile::Rename(std::string_view name) { | 250 | bool RealVfsFile::Rename(std::string_view name) { |
| 83 | std::string name_str(name.begin(), name.end()); | 251 | return base.MoveFile(path, parent_path + DIR_SEP + std::string(name)) != nullptr; |
| 84 | const auto out = FileUtil::Rename(GetName(), name_str); | 252 | } |
| 253 | |||
| 254 | bool RealVfsFile::Close() { | ||
| 255 | return backing->Close(); | ||
| 256 | } | ||
| 85 | 257 | ||
| 86 | path = (parent_path + DIR_SEP).append(name); | 258 | // TODO(DarkLordZach): MSVC would not let me combine the following two functions using 'if |
| 87 | path_components = parent_components; | 259 | // constexpr' because there is a compile error in the branch not used. |
| 88 | path_components.push_back(std::move(name_str)); | 260 | |
| 89 | backing = FileUtil::IOFile(path, ModeFlagsToString(perms).c_str()); | 261 | template <> |
| 262 | std::vector<VirtualFile> RealVfsDirectory::IterateEntries<RealVfsFile, VfsFile>() const { | ||
| 263 | if (perms == Mode::Append) | ||
| 264 | return {}; | ||
| 265 | |||
| 266 | std::vector<VirtualFile> out; | ||
| 267 | FileUtil::ForeachDirectoryEntry( | ||
| 268 | nullptr, path, | ||
| 269 | [&out, this](u64* entries_out, const std::string& directory, const std::string& filename) { | ||
| 270 | const std::string full_path = directory + DIR_SEP + filename; | ||
| 271 | if (!FileUtil::IsDirectory(full_path)) | ||
| 272 | out.emplace_back(base.OpenFile(full_path, perms)); | ||
| 273 | return true; | ||
| 274 | }); | ||
| 90 | 275 | ||
| 91 | return out; | 276 | return out; |
| 92 | } | 277 | } |
| 93 | 278 | ||
| 94 | bool RealVfsFile::Close() { | 279 | template <> |
| 95 | return backing.Close(); | 280 | std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDirectory>() const { |
| 281 | if (perms == Mode::Append) | ||
| 282 | return {}; | ||
| 283 | |||
| 284 | std::vector<VirtualDir> out; | ||
| 285 | FileUtil::ForeachDirectoryEntry( | ||
| 286 | nullptr, path, | ||
| 287 | [&out, this](u64* entries_out, const std::string& directory, const std::string& filename) { | ||
| 288 | const std::string full_path = directory + DIR_SEP + filename; | ||
| 289 | if (FileUtil::IsDirectory(full_path)) | ||
| 290 | out.emplace_back(base.OpenDirectory(full_path, perms)); | ||
| 291 | return true; | ||
| 292 | }); | ||
| 293 | |||
| 294 | return out; | ||
| 96 | } | 295 | } |
| 97 | 296 | ||
| 98 | RealVfsDirectory::RealVfsDirectory(const std::string& path_, Mode perms_) | 297 | RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_, Mode perms_) |
| 99 | : path(FileUtil::RemoveTrailingSlash(path_)), parent_path(FileUtil::GetParentPath(path)), | 298 | : base(base_), path(FileUtil::RemoveTrailingSlash(path_)), |
| 299 | parent_path(FileUtil::GetParentPath(path)), | ||
| 100 | path_components(FileUtil::SplitPathComponents(path)), | 300 | path_components(FileUtil::SplitPathComponents(path)), |
| 101 | parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)), | 301 | parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)), |
| 102 | perms(perms_) { | 302 | perms(perms_) { |
| 103 | if (!FileUtil::Exists(path) && perms & Mode::WriteAppend) | 303 | if (!FileUtil::Exists(path) && perms & Mode::WriteAppend) |
| 104 | FileUtil::CreateDir(path); | 304 | FileUtil::CreateDir(path); |
| 305 | } | ||
| 105 | 306 | ||
| 106 | if (perms == Mode::Append) | 307 | std::shared_ptr<VfsFile> RealVfsDirectory::GetFileRelative(std::string_view path) const { |
| 107 | return; | 308 | const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path)); |
| 309 | if (!FileUtil::Exists(full_path)) | ||
| 310 | return nullptr; | ||
| 311 | return base.OpenFile(full_path, perms); | ||
| 312 | } | ||
| 108 | 313 | ||
| 109 | FileUtil::ForeachDirectoryEntry( | 314 | std::shared_ptr<VfsDirectory> RealVfsDirectory::GetDirectoryRelative(std::string_view path) const { |
| 110 | nullptr, path, | 315 | const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path)); |
| 111 | [this](u64* entries_out, const std::string& directory, const std::string& filename) { | 316 | if (!FileUtil::Exists(full_path)) |
| 112 | std::string full_path = directory + DIR_SEP + filename; | 317 | return nullptr; |
| 113 | if (FileUtil::IsDirectory(full_path)) | 318 | return base.OpenDirectory(full_path, perms); |
| 114 | subdirectories.emplace_back(std::make_shared<RealVfsDirectory>(full_path, perms)); | 319 | } |
| 115 | else | 320 | |
| 116 | files.emplace_back(std::make_shared<RealVfsFile>(full_path, perms)); | 321 | std::shared_ptr<VfsFile> RealVfsDirectory::GetFile(std::string_view name) const { |
| 117 | return true; | 322 | return GetFileRelative(name); |
| 118 | }); | 323 | } |
| 324 | |||
| 325 | std::shared_ptr<VfsDirectory> RealVfsDirectory::GetSubdirectory(std::string_view name) const { | ||
| 326 | return GetDirectoryRelative(name); | ||
| 327 | } | ||
| 328 | |||
| 329 | std::shared_ptr<VfsFile> RealVfsDirectory::CreateFileRelative(std::string_view path) { | ||
| 330 | const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path)); | ||
| 331 | return base.CreateFile(full_path, perms); | ||
| 332 | } | ||
| 333 | |||
| 334 | std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateDirectoryRelative(std::string_view path) { | ||
| 335 | const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path)); | ||
| 336 | auto parent = std::string(FileUtil::GetParentPath(full_path)); | ||
| 337 | return base.CreateDirectory(full_path, perms); | ||
| 338 | } | ||
| 339 | |||
| 340 | bool RealVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) { | ||
| 341 | auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(name)); | ||
| 342 | return base.DeleteDirectory(full_path); | ||
| 119 | } | 343 | } |
| 120 | 344 | ||
| 121 | std::vector<std::shared_ptr<VfsFile>> RealVfsDirectory::GetFiles() const { | 345 | std::vector<std::shared_ptr<VfsFile>> RealVfsDirectory::GetFiles() const { |
| 122 | return files; | 346 | return IterateEntries<RealVfsFile, VfsFile>(); |
| 123 | } | 347 | } |
| 124 | 348 | ||
| 125 | std::vector<std::shared_ptr<VfsDirectory>> RealVfsDirectory::GetSubdirectories() const { | 349 | std::vector<std::shared_ptr<VfsDirectory>> RealVfsDirectory::GetSubdirectories() const { |
| 126 | return subdirectories; | 350 | return IterateEntries<RealVfsDirectory, VfsDirectory>(); |
| 127 | } | 351 | } |
| 128 | 352 | ||
| 129 | bool RealVfsDirectory::IsWritable() const { | 353 | bool RealVfsDirectory::IsWritable() const { |
| @@ -142,57 +366,32 @@ std::shared_ptr<VfsDirectory> RealVfsDirectory::GetParentDirectory() const { | |||
| 142 | if (path_components.size() <= 1) | 366 | if (path_components.size() <= 1) |
| 143 | return nullptr; | 367 | return nullptr; |
| 144 | 368 | ||
| 145 | return std::make_shared<RealVfsDirectory>(parent_path, perms); | 369 | return base.OpenDirectory(parent_path, perms); |
| 146 | } | 370 | } |
| 147 | 371 | ||
| 148 | std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateSubdirectory(std::string_view name) { | 372 | std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateSubdirectory(std::string_view name) { |
| 149 | const std::string subdir_path = (path + DIR_SEP).append(name); | 373 | const std::string subdir_path = (path + DIR_SEP).append(name); |
| 150 | 374 | return base.CreateDirectory(subdir_path, perms); | |
| 151 | if (!FileUtil::CreateDir(subdir_path)) { | ||
| 152 | return nullptr; | ||
| 153 | } | ||
| 154 | |||
| 155 | subdirectories.emplace_back(std::make_shared<RealVfsDirectory>(subdir_path, perms)); | ||
| 156 | return subdirectories.back(); | ||
| 157 | } | 375 | } |
| 158 | 376 | ||
| 159 | std::shared_ptr<VfsFile> RealVfsDirectory::CreateFile(std::string_view name) { | 377 | std::shared_ptr<VfsFile> RealVfsDirectory::CreateFile(std::string_view name) { |
| 160 | const std::string file_path = (path + DIR_SEP).append(name); | 378 | const std::string file_path = (path + DIR_SEP).append(name); |
| 161 | 379 | return base.CreateFile(file_path, perms); | |
| 162 | if (!FileUtil::CreateEmptyFile(file_path)) { | ||
| 163 | return nullptr; | ||
| 164 | } | ||
| 165 | |||
| 166 | files.emplace_back(std::make_shared<RealVfsFile>(file_path, perms)); | ||
| 167 | return files.back(); | ||
| 168 | } | 380 | } |
| 169 | 381 | ||
| 170 | bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) { | 382 | bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) { |
| 171 | const std::string subdir_path = (path + DIR_SEP).append(name); | 383 | const std::string subdir_path = (path + DIR_SEP).append(name); |
| 172 | 384 | return base.DeleteDirectory(subdir_path); | |
| 173 | return FileUtil::DeleteDirRecursively(subdir_path); | ||
| 174 | } | 385 | } |
| 175 | 386 | ||
| 176 | bool RealVfsDirectory::DeleteFile(std::string_view name) { | 387 | bool RealVfsDirectory::DeleteFile(std::string_view name) { |
| 177 | const auto file = GetFile(name); | ||
| 178 | |||
| 179 | if (file == nullptr) { | ||
| 180 | return false; | ||
| 181 | } | ||
| 182 | |||
| 183 | files.erase(std::find(files.begin(), files.end(), file)); | ||
| 184 | |||
| 185 | auto real_file = std::static_pointer_cast<RealVfsFile>(file); | ||
| 186 | real_file->Close(); | ||
| 187 | |||
| 188 | const std::string file_path = (path + DIR_SEP).append(name); | 388 | const std::string file_path = (path + DIR_SEP).append(name); |
| 189 | return FileUtil::Delete(file_path); | 389 | return base.DeleteFile(file_path); |
| 190 | } | 390 | } |
| 191 | 391 | ||
| 192 | bool RealVfsDirectory::Rename(std::string_view name) { | 392 | bool RealVfsDirectory::Rename(std::string_view name) { |
| 193 | const std::string new_name = (parent_path + DIR_SEP).append(name); | 393 | const std::string new_name = (parent_path + DIR_SEP).append(name); |
| 194 | 394 | return base.MoveFile(path, new_name) != nullptr; | |
| 195 | return FileUtil::Rename(path, new_name); | ||
| 196 | } | 395 | } |
| 197 | 396 | ||
| 198 | std::string RealVfsDirectory::GetFullPath() const { | 397 | std::string RealVfsDirectory::GetFullPath() const { |
| @@ -202,16 +401,6 @@ std::string RealVfsDirectory::GetFullPath() const { | |||
| 202 | } | 401 | } |
| 203 | 402 | ||
| 204 | bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { | 403 | bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { |
| 205 | const auto iter = std::find(files.begin(), files.end(), file); | 404 | return false; |
| 206 | if (iter == files.end()) | ||
| 207 | return false; | ||
| 208 | |||
| 209 | const std::ptrdiff_t offset = std::distance(files.begin(), iter); | ||
| 210 | files[offset] = files.back(); | ||
| 211 | files.pop_back(); | ||
| 212 | |||
| 213 | subdirectories.emplace_back(std::move(dir)); | ||
| 214 | |||
| 215 | return true; | ||
| 216 | } | 405 | } |
| 217 | } // namespace FileSys | 406 | } // namespace FileSys |
diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h index 243d58576..8a1e79ef6 100644 --- a/src/core/file_sys/vfs_real.h +++ b/src/core/file_sys/vfs_real.h | |||
| @@ -6,18 +6,45 @@ | |||
| 6 | 6 | ||
| 7 | #include <string_view> | 7 | #include <string_view> |
| 8 | 8 | ||
| 9 | #include <boost/container/flat_map.hpp> | ||
| 9 | #include "common/file_util.h" | 10 | #include "common/file_util.h" |
| 10 | #include "core/file_sys/mode.h" | 11 | #include "core/file_sys/mode.h" |
| 11 | #include "core/file_sys/vfs.h" | 12 | #include "core/file_sys/vfs.h" |
| 12 | 13 | ||
| 13 | namespace FileSys { | 14 | namespace FileSys { |
| 14 | 15 | ||
| 16 | class RealVfsFilesystem : public VfsFilesystem { | ||
| 17 | public: | ||
| 18 | RealVfsFilesystem(); | ||
| 19 | |||
| 20 | std::string GetName() const override; | ||
| 21 | bool IsReadable() const override; | ||
| 22 | bool IsWritable() const override; | ||
| 23 | VfsEntryType GetEntryType(std::string_view path) const override; | ||
| 24 | VirtualFile OpenFile(std::string_view path, Mode perms = Mode::Read) override; | ||
| 25 | VirtualFile CreateFile(std::string_view path, Mode perms = Mode::ReadWrite) override; | ||
| 26 | VirtualFile CopyFile(std::string_view old_path, std::string_view new_path) override; | ||
| 27 | VirtualFile MoveFile(std::string_view old_path, std::string_view new_path) override; | ||
| 28 | bool DeleteFile(std::string_view path) override; | ||
| 29 | VirtualDir OpenDirectory(std::string_view path, Mode perms = Mode::Read) override; | ||
| 30 | VirtualDir CreateDirectory(std::string_view path, Mode perms = Mode::ReadWrite) override; | ||
| 31 | VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path) override; | ||
| 32 | VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path) override; | ||
| 33 | bool DeleteDirectory(std::string_view path) override; | ||
| 34 | |||
| 35 | private: | ||
| 36 | boost::container::flat_map<std::string, std::weak_ptr<FileUtil::IOFile>> cache; | ||
| 37 | }; | ||
| 38 | |||
| 15 | // An implmentation of VfsFile that represents a file on the user's computer. | 39 | // An implmentation of VfsFile that represents a file on the user's computer. |
| 16 | struct RealVfsFile : public VfsFile { | 40 | class RealVfsFile : public VfsFile { |
| 17 | friend struct RealVfsDirectory; | 41 | friend class RealVfsDirectory; |
| 42 | friend class RealVfsFilesystem; | ||
| 18 | 43 | ||
| 19 | RealVfsFile(const std::string& name, Mode perms = Mode::Read); | 44 | RealVfsFile(RealVfsFilesystem& base, std::shared_ptr<FileUtil::IOFile> backing, |
| 45 | const std::string& path, Mode perms = Mode::Read); | ||
| 20 | 46 | ||
| 47 | public: | ||
| 21 | std::string GetName() const override; | 48 | std::string GetName() const override; |
| 22 | size_t GetSize() const override; | 49 | size_t GetSize() const override; |
| 23 | bool Resize(size_t new_size) override; | 50 | bool Resize(size_t new_size) override; |
| @@ -31,7 +58,8 @@ struct RealVfsFile : public VfsFile { | |||
| 31 | private: | 58 | private: |
| 32 | bool Close(); | 59 | bool Close(); |
| 33 | 60 | ||
| 34 | FileUtil::IOFile backing; | 61 | RealVfsFilesystem& base; |
| 62 | std::shared_ptr<FileUtil::IOFile> backing; | ||
| 35 | std::string path; | 63 | std::string path; |
| 36 | std::string parent_path; | 64 | std::string parent_path; |
| 37 | std::vector<std::string> path_components; | 65 | std::vector<std::string> path_components; |
| @@ -40,9 +68,19 @@ private: | |||
| 40 | }; | 68 | }; |
| 41 | 69 | ||
| 42 | // An implementation of VfsDirectory that represents a directory on the user's computer. | 70 | // An implementation of VfsDirectory that represents a directory on the user's computer. |
| 43 | struct RealVfsDirectory : public VfsDirectory { | 71 | class RealVfsDirectory : public VfsDirectory { |
| 44 | RealVfsDirectory(const std::string& path, Mode perms = Mode::Read); | 72 | friend class RealVfsFilesystem; |
| 45 | 73 | ||
| 74 | RealVfsDirectory(RealVfsFilesystem& base, const std::string& path, Mode perms = Mode::Read); | ||
| 75 | |||
| 76 | public: | ||
| 77 | std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const override; | ||
| 78 | std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const override; | ||
| 79 | std::shared_ptr<VfsFile> GetFile(std::string_view name) const override; | ||
| 80 | std::shared_ptr<VfsDirectory> GetSubdirectory(std::string_view name) const override; | ||
| 81 | std::shared_ptr<VfsFile> CreateFileRelative(std::string_view path) override; | ||
| 82 | std::shared_ptr<VfsDirectory> CreateDirectoryRelative(std::string_view path) override; | ||
| 83 | bool DeleteSubdirectoryRecursive(std::string_view name) override; | ||
| 46 | std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; | 84 | std::vector<std::shared_ptr<VfsFile>> GetFiles() const override; |
| 47 | std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override; | 85 | std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override; |
| 48 | bool IsWritable() const override; | 86 | bool IsWritable() const override; |
| @@ -60,13 +98,15 @@ protected: | |||
| 60 | bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; | 98 | bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; |
| 61 | 99 | ||
| 62 | private: | 100 | private: |
| 101 | template <typename T, typename R> | ||
| 102 | std::vector<std::shared_ptr<R>> IterateEntries() const; | ||
| 103 | |||
| 104 | RealVfsFilesystem& base; | ||
| 63 | std::string path; | 105 | std::string path; |
| 64 | std::string parent_path; | 106 | std::string parent_path; |
| 65 | std::vector<std::string> path_components; | 107 | std::vector<std::string> path_components; |
| 66 | std::vector<std::string> parent_components; | 108 | std::vector<std::string> parent_components; |
| 67 | Mode perms; | 109 | Mode perms; |
| 68 | std::vector<std::shared_ptr<VfsFile>> files; | ||
| 69 | std::vector<std::shared_ptr<VfsDirectory>> subdirectories; | ||
| 70 | }; | 110 | }; |
| 71 | 111 | ||
| 72 | } // namespace FileSys | 112 | } // namespace FileSys |