summaryrefslogtreecommitdiff
path: root/src/core/file_sys
diff options
context:
space:
mode:
authorGravatar Zach Hilman2018-07-06 10:51:32 -0400
committerGravatar bunnei2018-07-06 10:51:32 -0400
commit77c684c1140f6bf3fb7d4560d06d2efb1a2ee5e2 (patch)
tree38ef6451732c5eecb0efdd198f3db4d33848453c /src/core/file_sys
parentMerge pull request #629 from Subv/depth_test (diff)
downloadyuzu-77c684c1140f6bf3fb7d4560d06d2efb1a2ee5e2.tar.gz
yuzu-77c684c1140f6bf3fb7d4560d06d2efb1a2ee5e2.tar.xz
yuzu-77c684c1140f6bf3fb7d4560d06d2efb1a2ee5e2.zip
Virtual Filesystem (#597)
* Add VfsFile and VfsDirectory classes * Finish abstract Vfs classes * Implement RealVfsFile (computer fs backend) * Finish RealVfsFile and RealVfsDirectory * Finished OffsetVfsFile * More changes * Fix import paths * Major refactor * Remove double const * Use experimental/filesystem or filesystem depending on compiler * Port partition_filesystem * More changes * More Overhaul * FSP_SRV fixes * Fixes and testing * Try to get filesystem to compile * Filesystem on linux * Remove std::filesystem and document/test * Compile fixes * Missing include * Bug fixes * Fixes * Rename v_file and v_dir * clang-format fix * Rename NGLOG_* to LOG_* * Most review changes * Fix TODO * Guess 'main' to be Directory by filename
Diffstat (limited to 'src/core/file_sys')
-rw-r--r--src/core/file_sys/content_archive.cpp164
-rw-r--r--src/core/file_sys/content_archive.h89
-rw-r--r--src/core/file_sys/disk_filesystem.cpp237
-rw-r--r--src/core/file_sys/disk_filesystem.h84
-rw-r--r--src/core/file_sys/filesystem.h132
-rw-r--r--src/core/file_sys/partition_filesystem.cpp136
-rw-r--r--src/core/file_sys/partition_filesystem.h29
-rw-r--r--src/core/file_sys/program_metadata.cpp43
-rw-r--r--src/core/file_sys/program_metadata.h6
-rw-r--r--src/core/file_sys/romfs_factory.cpp38
-rw-r--r--src/core/file_sys/romfs_factory.h35
-rw-r--r--src/core/file_sys/romfs_filesystem.cpp110
-rw-r--r--src/core/file_sys/romfs_filesystem.h85
-rw-r--r--src/core/file_sys/savedata_factory.cpp54
-rw-r--r--src/core/file_sys/savedata_factory.h33
-rw-r--r--src/core/file_sys/sdmc_factory.cpp39
-rw-r--r--src/core/file_sys/sdmc_factory.h31
-rw-r--r--src/core/file_sys/vfs.cpp187
-rw-r--r--src/core/file_sys/vfs.h220
-rw-r--r--src/core/file_sys/vfs_offset.cpp92
-rw-r--r--src/core/file_sys/vfs_offset.h46
-rw-r--r--src/core/file_sys/vfs_real.cpp168
-rw-r--r--src/core/file_sys/vfs_real.h65
23 files changed, 1127 insertions, 996 deletions
diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp
new file mode 100644
index 000000000..b45b83a26
--- /dev/null
+++ b/src/core/file_sys/content_archive.cpp
@@ -0,0 +1,164 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/logging/log.h"
6#include "core/file_sys/content_archive.h"
7#include "core/file_sys/vfs_offset.h"
8#include "core/loader/loader.h"
9
10namespace FileSys {
11
12// Media offsets in headers are stored divided by 512. Mult. by this to get real offset.
13constexpr u64 MEDIA_OFFSET_MULTIPLIER = 0x200;
14
15constexpr u64 SECTION_HEADER_SIZE = 0x200;
16constexpr u64 SECTION_HEADER_OFFSET = 0x400;
17
18constexpr u32 IVFC_MAX_LEVEL = 6;
19
20enum class NCASectionFilesystemType : u8 { PFS0 = 0x2, ROMFS = 0x3 };
21
22struct NCASectionHeaderBlock {
23 INSERT_PADDING_BYTES(3);
24 NCASectionFilesystemType filesystem_type;
25 u8 crypto_type;
26 INSERT_PADDING_BYTES(3);
27};
28static_assert(sizeof(NCASectionHeaderBlock) == 0x8, "NCASectionHeaderBlock has incorrect size.");
29
30struct PFS0Superblock {
31 NCASectionHeaderBlock header_block;
32 std::array<u8, 0x20> hash;
33 u32_le size;
34 INSERT_PADDING_BYTES(4);
35 u64_le hash_table_offset;
36 u64_le hash_table_size;
37 u64_le pfs0_header_offset;
38 u64_le pfs0_size;
39 INSERT_PADDING_BYTES(432);
40};
41static_assert(sizeof(PFS0Superblock) == 0x200, "PFS0Superblock has incorrect size.");
42
43struct IVFCLevel {
44 u64_le offset;
45 u64_le size;
46 u32_le block_size;
47 u32_le reserved;
48};
49static_assert(sizeof(IVFCLevel) == 0x18, "IVFCLevel has incorrect size.");
50
51struct RomFSSuperblock {
52 NCASectionHeaderBlock header_block;
53 u32_le magic;
54 u32_le magic_number;
55 INSERT_PADDING_BYTES(8);
56 std::array<IVFCLevel, 6> levels;
57 INSERT_PADDING_BYTES(64);
58};
59static_assert(sizeof(RomFSSuperblock) == 0xE8, "RomFSSuperblock has incorrect size.");
60
61NCA::NCA(VirtualFile file_) : file(file_) {
62 if (sizeof(NCAHeader) != file->ReadObject(&header))
63 LOG_CRITICAL(Loader, "File reader errored out during header read.");
64
65 if (!IsValidNCA(header)) {
66 status = Loader::ResultStatus::ErrorInvalidFormat;
67 return;
68 }
69
70 std::ptrdiff_t number_sections =
71 std::count_if(std::begin(header.section_tables), std::end(header.section_tables),
72 [](NCASectionTableEntry entry) { return entry.media_offset > 0; });
73
74 for (std::ptrdiff_t i = 0; i < number_sections; ++i) {
75 // Seek to beginning of this section.
76 NCASectionHeaderBlock block{};
77 if (sizeof(NCASectionHeaderBlock) !=
78 file->ReadObject(&block, SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE))
79 LOG_CRITICAL(Loader, "File reader errored out during header read.");
80
81 if (block.filesystem_type == NCASectionFilesystemType::ROMFS) {
82 RomFSSuperblock sb{};
83 if (sizeof(RomFSSuperblock) !=
84 file->ReadObject(&sb, SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE))
85 LOG_CRITICAL(Loader, "File reader errored out during header read.");
86
87 const size_t romfs_offset =
88 header.section_tables[i].media_offset * MEDIA_OFFSET_MULTIPLIER +
89 sb.levels[IVFC_MAX_LEVEL - 1].offset;
90 const size_t romfs_size = sb.levels[IVFC_MAX_LEVEL - 1].size;
91 files.emplace_back(std::make_shared<OffsetVfsFile>(file, romfs_size, romfs_offset));
92 romfs = files.back();
93 } else if (block.filesystem_type == NCASectionFilesystemType::PFS0) {
94 PFS0Superblock sb{};
95 // Seek back to beginning of this section.
96 if (sizeof(PFS0Superblock) !=
97 file->ReadObject(&sb, SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE))
98 LOG_CRITICAL(Loader, "File reader errored out during header read.");
99
100 u64 offset = (static_cast<u64>(header.section_tables[i].media_offset) *
101 MEDIA_OFFSET_MULTIPLIER) +
102 sb.pfs0_header_offset;
103 u64 size = MEDIA_OFFSET_MULTIPLIER * (header.section_tables[i].media_end_offset -
104 header.section_tables[i].media_offset);
105 auto npfs = std::make_shared<PartitionFilesystem>(
106 std::make_shared<OffsetVfsFile>(file, size, offset));
107
108 if (npfs->GetStatus() == Loader::ResultStatus::Success) {
109 dirs.emplace_back(npfs);
110 if (IsDirectoryExeFS(dirs.back()))
111 exefs = dirs.back();
112 }
113 }
114 }
115
116 status = Loader::ResultStatus::Success;
117}
118
119Loader::ResultStatus NCA::GetStatus() const {
120 return status;
121}
122
123std::vector<std::shared_ptr<VfsFile>> NCA::GetFiles() const {
124 if (status != Loader::ResultStatus::Success)
125 return {};
126 return files;
127}
128
129std::vector<std::shared_ptr<VfsDirectory>> NCA::GetSubdirectories() const {
130 if (status != Loader::ResultStatus::Success)
131 return {};
132 return dirs;
133}
134
135std::string NCA::GetName() const {
136 return file->GetName();
137}
138
139std::shared_ptr<VfsDirectory> NCA::GetParentDirectory() const {
140 return file->GetContainingDirectory();
141}
142
143NCAContentType NCA::GetType() const {
144 return header.content_type;
145}
146
147u64 NCA::GetTitleId() const {
148 if (status != Loader::ResultStatus::Success)
149 return {};
150 return header.title_id;
151}
152
153VirtualFile NCA::GetRomFS() const {
154 return romfs;
155}
156
157VirtualDir NCA::GetExeFS() const {
158 return exefs;
159}
160
161bool NCA::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
162 return false;
163}
164} // namespace FileSys
diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h
new file mode 100644
index 000000000..eb4ca1c18
--- /dev/null
+++ b/src/core/file_sys/content_archive.h
@@ -0,0 +1,89 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/common_funcs.h"
8#include "common/common_types.h"
9#include "common/swap.h"
10#include "core/file_sys/partition_filesystem.h"
11
12namespace FileSys {
13
14enum class NCAContentType : u8 { Program = 0, Meta = 1, Control = 2, Manual = 3, Data = 4 };
15
16struct NCASectionTableEntry {
17 u32_le media_offset;
18 u32_le media_end_offset;
19 INSERT_PADDING_BYTES(0x8);
20};
21static_assert(sizeof(NCASectionTableEntry) == 0x10, "NCASectionTableEntry has incorrect size.");
22
23struct NCAHeader {
24 std::array<u8, 0x100> rsa_signature_1;
25 std::array<u8, 0x100> rsa_signature_2;
26 u32_le magic;
27 u8 is_system;
28 NCAContentType content_type;
29 u8 crypto_type;
30 u8 key_index;
31 u64_le size;
32 u64_le title_id;
33 INSERT_PADDING_BYTES(0x4);
34 u32_le sdk_version;
35 u8 crypto_type_2;
36 INSERT_PADDING_BYTES(15);
37 std::array<u8, 0x10> rights_id;
38 std::array<NCASectionTableEntry, 0x4> section_tables;
39 std::array<std::array<u8, 0x20>, 0x4> hash_tables;
40 std::array<std::array<u8, 0x10>, 0x4> key_area;
41 INSERT_PADDING_BYTES(0xC0);
42};
43static_assert(sizeof(NCAHeader) == 0x400, "NCAHeader has incorrect size.");
44
45static bool IsDirectoryExeFS(std::shared_ptr<FileSys::VfsDirectory> pfs) {
46 // According to switchbrew, an exefs must only contain these two files:
47 return pfs->GetFile("main") != nullptr && pfs->GetFile("main.npdm") != nullptr;
48}
49
50static bool IsValidNCA(const NCAHeader& header) {
51 return header.magic == Common::MakeMagic('N', 'C', 'A', '2') ||
52 header.magic == Common::MakeMagic('N', 'C', 'A', '3');
53}
54
55// An implementation of VfsDirectory that represents a Nintendo Content Archive (NCA) conatiner.
56// After construction, use GetStatus to determine if the file is valid and ready to be used.
57class NCA : public ReadOnlyVfsDirectory {
58public:
59 explicit NCA(VirtualFile file);
60 Loader::ResultStatus GetStatus() const;
61
62 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
63 std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
64 std::string GetName() const override;
65 std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
66
67 NCAContentType GetType() const;
68 u64 GetTitleId() const;
69
70 VirtualFile GetRomFS() const;
71 VirtualDir GetExeFS() const;
72
73protected:
74 bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
75
76private:
77 std::vector<VirtualDir> dirs;
78 std::vector<VirtualFile> files;
79
80 VirtualFile romfs = nullptr;
81 VirtualDir exefs = nullptr;
82 VirtualFile file;
83
84 NCAHeader header{};
85
86 Loader::ResultStatus status{};
87};
88
89} // namespace FileSys
diff --git a/src/core/file_sys/disk_filesystem.cpp b/src/core/file_sys/disk_filesystem.cpp
deleted file mode 100644
index 8c6f15bb5..000000000
--- a/src/core/file_sys/disk_filesystem.cpp
+++ /dev/null
@@ -1,237 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <memory>
7#include "common/common_types.h"
8#include "common/logging/log.h"
9#include "core/file_sys/disk_filesystem.h"
10#include "core/file_sys/errors.h"
11
12namespace FileSys {
13
14static std::string ModeFlagsToString(Mode mode) {
15 std::string mode_str;
16 u32 mode_flags = static_cast<u32>(mode);
17
18 // Calculate the correct open mode for the file.
19 if ((mode_flags & static_cast<u32>(Mode::Read)) &&
20 (mode_flags & static_cast<u32>(Mode::Write))) {
21 if (mode_flags & static_cast<u32>(Mode::Append))
22 mode_str = "a+";
23 else
24 mode_str = "r+";
25 } else {
26 if (mode_flags & static_cast<u32>(Mode::Read))
27 mode_str = "r";
28 else if (mode_flags & static_cast<u32>(Mode::Append))
29 mode_str = "a";
30 else if (mode_flags & static_cast<u32>(Mode::Write))
31 mode_str = "w";
32 }
33
34 mode_str += "b";
35
36 return mode_str;
37}
38
39std::string Disk_FileSystem::GetName() const {
40 return "Disk";
41}
42
43ResultVal<std::unique_ptr<StorageBackend>> Disk_FileSystem::OpenFile(const std::string& path,
44 Mode mode) const {
45
46 // Calculate the correct open mode for the file.
47 std::string mode_str = ModeFlagsToString(mode);
48
49 std::string full_path = base_directory + path;
50 auto file = std::make_shared<FileUtil::IOFile>(full_path, mode_str.c_str());
51
52 if (!file->IsOpen()) {
53 return ERROR_PATH_NOT_FOUND;
54 }
55
56 return MakeResult<std::unique_ptr<StorageBackend>>(
57 std::make_unique<Disk_Storage>(std::move(file)));
58}
59
60ResultCode Disk_FileSystem::DeleteFile(const std::string& path) const {
61 if (!FileUtil::Exists(path)) {
62 return ERROR_PATH_NOT_FOUND;
63 }
64
65 FileUtil::Delete(path);
66
67 return RESULT_SUCCESS;
68}
69
70ResultCode Disk_FileSystem::RenameFile(const std::string& src_path,
71 const std::string& dest_path) const {
72 const std::string full_src_path = base_directory + src_path;
73 const std::string full_dest_path = base_directory + dest_path;
74
75 if (!FileUtil::Exists(full_src_path)) {
76 return ERROR_PATH_NOT_FOUND;
77 }
78 // TODO(wwylele): Use correct error code
79 return FileUtil::Rename(full_src_path, full_dest_path) ? RESULT_SUCCESS : ResultCode(-1);
80}
81
82ResultCode Disk_FileSystem::DeleteDirectory(const Path& path) const {
83 LOG_WARNING(Service_FS, "(STUBBED) called");
84 // TODO(wwylele): Use correct error code
85 return ResultCode(-1);
86}
87
88ResultCode Disk_FileSystem::DeleteDirectoryRecursively(const Path& path) const {
89 LOG_WARNING(Service_FS, "(STUBBED) called");
90 // TODO(wwylele): Use correct error code
91 return ResultCode(-1);
92}
93
94ResultCode Disk_FileSystem::CreateFile(const std::string& path, u64 size) const {
95 LOG_WARNING(Service_FS, "(STUBBED) called");
96
97 std::string full_path = base_directory + path;
98 if (size == 0) {
99 FileUtil::CreateEmptyFile(full_path);
100 return RESULT_SUCCESS;
101 }
102
103 FileUtil::IOFile file(full_path, "wb");
104 // Creates a sparse file (or a normal file on filesystems without the concept of sparse files)
105 // We do this by seeking to the right size, then writing a single null byte.
106 if (file.Seek(size - 1, SEEK_SET) && file.WriteBytes("", 1) == 1) {
107 return RESULT_SUCCESS;
108 }
109
110 LOG_ERROR(Service_FS, "Too large file");
111 // TODO(Subv): Find out the correct error code
112 return ResultCode(-1);
113}
114
115ResultCode Disk_FileSystem::CreateDirectory(const std::string& path) const {
116 // TODO(Subv): Perform path validation to prevent escaping the emulator sandbox.
117 std::string full_path = base_directory + path;
118
119 if (FileUtil::CreateDir(full_path)) {
120 return RESULT_SUCCESS;
121 }
122
123 LOG_CRITICAL(Service_FS, "(unreachable) Unknown error creating {}", full_path);
124 // TODO(wwylele): Use correct error code
125 return ResultCode(-1);
126}
127
128ResultCode Disk_FileSystem::RenameDirectory(const Path& src_path, const Path& dest_path) const {
129 LOG_WARNING(Service_FS, "(STUBBED) called");
130 // TODO(wwylele): Use correct error code
131 return ResultCode(-1);
132}
133
134ResultVal<std::unique_ptr<DirectoryBackend>> Disk_FileSystem::OpenDirectory(
135 const std::string& path) const {
136
137 std::string full_path = base_directory + path;
138
139 if (!FileUtil::IsDirectory(full_path)) {
140 // TODO(Subv): Find the correct error code for this.
141 return ResultCode(-1);
142 }
143
144 auto directory = std::make_unique<Disk_Directory>(full_path);
145 return MakeResult<std::unique_ptr<DirectoryBackend>>(std::move(directory));
146}
147
148u64 Disk_FileSystem::GetFreeSpaceSize() const {
149 LOG_WARNING(Service_FS, "(STUBBED) called");
150 return 0;
151}
152
153ResultVal<FileSys::EntryType> Disk_FileSystem::GetEntryType(const std::string& path) const {
154 std::string full_path = base_directory + path;
155 if (!FileUtil::Exists(full_path)) {
156 return ERROR_PATH_NOT_FOUND;
157 }
158
159 if (FileUtil::IsDirectory(full_path))
160 return MakeResult(EntryType::Directory);
161
162 return MakeResult(EntryType::File);
163}
164
165ResultVal<size_t> Disk_Storage::Read(const u64 offset, const size_t length, u8* buffer) const {
166 LOG_TRACE(Service_FS, "called offset={}, length={}", offset, length);
167 file->Seek(offset, SEEK_SET);
168 return MakeResult<size_t>(file->ReadBytes(buffer, length));
169}
170
171ResultVal<size_t> Disk_Storage::Write(const u64 offset, const size_t length, const bool flush,
172 const u8* buffer) const {
173 LOG_WARNING(Service_FS, "(STUBBED) called");
174 file->Seek(offset, SEEK_SET);
175 size_t written = file->WriteBytes(buffer, length);
176 if (flush) {
177 file->Flush();
178 }
179 return MakeResult<size_t>(written);
180}
181
182u64 Disk_Storage::GetSize() const {
183 return file->GetSize();
184}
185
186bool Disk_Storage::SetSize(const u64 size) const {
187 file->Resize(size);
188 file->Flush();
189 return true;
190}
191
192Disk_Directory::Disk_Directory(const std::string& path) {
193 unsigned size = FileUtil::ScanDirectoryTree(path, directory);
194 directory.size = size;
195 directory.isDirectory = true;
196 children_iterator = directory.children.begin();
197}
198
199u64 Disk_Directory::Read(const u64 count, Entry* entries) {
200 u64 entries_read = 0;
201
202 while (entries_read < count && children_iterator != directory.children.cend()) {
203 const FileUtil::FSTEntry& file = *children_iterator;
204 const std::string& filename = file.virtualName;
205 Entry& entry = entries[entries_read];
206
207 LOG_TRACE(Service_FS, "File {}: size={} dir={}", filename, file.size, file.isDirectory);
208
209 // TODO(Link Mauve): use a proper conversion to UTF-16.
210 for (size_t j = 0; j < FILENAME_LENGTH; ++j) {
211 entry.filename[j] = filename[j];
212 if (!filename[j])
213 break;
214 }
215
216 if (file.isDirectory) {
217 entry.file_size = 0;
218 entry.type = EntryType::Directory;
219 } else {
220 entry.file_size = file.size;
221 entry.type = EntryType::File;
222 }
223
224 ++entries_read;
225 ++children_iterator;
226 }
227 return entries_read;
228}
229
230u64 Disk_Directory::GetEntryCount() const {
231 // We convert the children iterator into a const_iterator to allow template argument deduction
232 // in std::distance.
233 std::vector<FileUtil::FSTEntry>::const_iterator current = children_iterator;
234 return std::distance(current, directory.children.end());
235}
236
237} // namespace FileSys
diff --git a/src/core/file_sys/disk_filesystem.h b/src/core/file_sys/disk_filesystem.h
deleted file mode 100644
index 591e39fda..000000000
--- a/src/core/file_sys/disk_filesystem.h
+++ /dev/null
@@ -1,84 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <cstddef>
8#include <memory>
9#include <string>
10#include "common/common_types.h"
11#include "common/file_util.h"
12#include "core/file_sys/directory.h"
13#include "core/file_sys/filesystem.h"
14#include "core/file_sys/storage.h"
15#include "core/hle/result.h"
16
17namespace FileSys {
18
19class Disk_FileSystem : public FileSystemBackend {
20public:
21 explicit Disk_FileSystem(std::string base_directory)
22 : base_directory(std::move(base_directory)) {}
23
24 std::string GetName() const override;
25
26 ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const std::string& path,
27 Mode mode) const override;
28 ResultCode DeleteFile(const std::string& path) const override;
29 ResultCode RenameFile(const std::string& src_path, const std::string& dest_path) const override;
30 ResultCode DeleteDirectory(const Path& path) const override;
31 ResultCode DeleteDirectoryRecursively(const Path& path) const override;
32 ResultCode CreateFile(const std::string& path, u64 size) const override;
33 ResultCode CreateDirectory(const std::string& path) const override;
34 ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override;
35 ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(
36 const std::string& path) const override;
37 u64 GetFreeSpaceSize() const override;
38 ResultVal<EntryType> GetEntryType(const std::string& path) const override;
39
40protected:
41 std::string base_directory;
42};
43
44class Disk_Storage : public StorageBackend {
45public:
46 explicit Disk_Storage(std::shared_ptr<FileUtil::IOFile> file) : file(std::move(file)) {}
47
48 ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
49 ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
50 u64 GetSize() const override;
51 bool SetSize(u64 size) const override;
52 bool Close() const override {
53 return false;
54 }
55 void Flush() const override {}
56
57private:
58 std::shared_ptr<FileUtil::IOFile> file;
59};
60
61class Disk_Directory : public DirectoryBackend {
62public:
63 explicit Disk_Directory(const std::string& path);
64
65 ~Disk_Directory() override {
66 Close();
67 }
68
69 u64 Read(const u64 count, Entry* entries) override;
70 u64 GetEntryCount() const override;
71
72 bool Close() const override {
73 return true;
74 }
75
76protected:
77 FileUtil::FSTEntry directory;
78
79 // We need to remember the last entry we returned, so a subsequent call to Read will continue
80 // from the next one. This iterator will always point to the next unread entry.
81 std::vector<FileUtil::FSTEntry>::iterator children_iterator;
82};
83
84} // namespace FileSys
diff --git a/src/core/file_sys/filesystem.h b/src/core/file_sys/filesystem.h
index 295a3133e..2d925d9e4 100644
--- a/src/core/file_sys/filesystem.h
+++ b/src/core/file_sys/filesystem.h
@@ -66,136 +66,4 @@ private:
66 std::u16string u16str; 66 std::u16string u16str;
67}; 67};
68 68
69/// Parameters of the archive, as specified in the Create or Format call.
70struct ArchiveFormatInfo {
71 u32_le total_size; ///< The pre-defined size of the archive.
72 u32_le number_directories; ///< The pre-defined number of directories in the archive.
73 u32_le number_files; ///< The pre-defined number of files in the archive.
74 u8 duplicate_data; ///< Whether the archive should duplicate the data.
75};
76static_assert(std::is_pod<ArchiveFormatInfo>::value, "ArchiveFormatInfo is not POD");
77
78class FileSystemBackend : NonCopyable {
79public:
80 virtual ~FileSystemBackend() {}
81
82 /**
83 * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
84 */
85 virtual std::string GetName() const = 0;
86
87 /**
88 * Create a file specified by its path
89 * @param path Path relative to the Archive
90 * @param size The size of the new file, filled with zeroes
91 * @return Result of the operation
92 */
93 virtual ResultCode CreateFile(const std::string& path, u64 size) const = 0;
94
95 /**
96 * Delete a file specified by its path
97 * @param path Path relative to the archive
98 * @return Result of the operation
99 */
100 virtual ResultCode DeleteFile(const std::string& path) const = 0;
101
102 /**
103 * Create a directory specified by its path
104 * @param path Path relative to the archive
105 * @return Result of the operation
106 */
107 virtual ResultCode CreateDirectory(const std::string& path) const = 0;
108
109 /**
110 * Delete a directory specified by its path
111 * @param path Path relative to the archive
112 * @return Result of the operation
113 */
114 virtual ResultCode DeleteDirectory(const Path& path) const = 0;
115
116 /**
117 * Delete a directory specified by its path and anything under it
118 * @param path Path relative to the archive
119 * @return Result of the operation
120 */
121 virtual ResultCode DeleteDirectoryRecursively(const Path& path) const = 0;
122
123 /**
124 * Rename a File specified by its path
125 * @param src_path Source path relative to the archive
126 * @param dest_path Destination path relative to the archive
127 * @return Result of the operation
128 */
129 virtual ResultCode RenameFile(const std::string& src_path,
130 const std::string& dest_path) const = 0;
131
132 /**
133 * Rename a Directory specified by its path
134 * @param src_path Source path relative to the archive
135 * @param dest_path Destination path relative to the archive
136 * @return Result of the operation
137 */
138 virtual ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const = 0;
139
140 /**
141 * Open a file specified by its path, using the specified mode
142 * @param path Path relative to the archive
143 * @param mode Mode to open the file with
144 * @return Opened file, or error code
145 */
146 virtual ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const std::string& path,
147 Mode mode) const = 0;
148
149 /**
150 * Open a directory specified by its path
151 * @param path Path relative to the archive
152 * @return Opened directory, or error code
153 */
154 virtual ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(
155 const std::string& path) const = 0;
156
157 /**
158 * Get the free space
159 * @return The number of free bytes in the archive
160 */
161 virtual u64 GetFreeSpaceSize() const = 0;
162
163 /**
164 * Get the type of the specified path
165 * @return The type of the specified path or error code
166 */
167 virtual ResultVal<EntryType> GetEntryType(const std::string& path) const = 0;
168};
169
170class FileSystemFactory : NonCopyable {
171public:
172 virtual ~FileSystemFactory() {}
173
174 /**
175 * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
176 */
177 virtual std::string GetName() const = 0;
178
179 /**
180 * Tries to open the archive of this type with the specified path
181 * @param path Path to the archive
182 * @return An ArchiveBackend corresponding operating specified archive path.
183 */
184 virtual ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) = 0;
185
186 /**
187 * Deletes the archive contents and then re-creates the base folder
188 * @param path Path to the archive
189 * @return ResultCode of the operation, 0 on success
190 */
191 virtual ResultCode Format(const Path& path) = 0;
192
193 /**
194 * Retrieves the format info about the archive with the specified path
195 * @param path Path to the archive
196 * @return Format information about the archive or error code
197 */
198 virtual ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const = 0;
199};
200
201} // namespace FileSys 69} // namespace FileSys
diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp
index 46d438aca..15b1fb946 100644
--- a/src/core/file_sys/partition_filesystem.cpp
+++ b/src/core/file_sys/partition_filesystem.cpp
@@ -6,29 +6,30 @@
6#include "common/file_util.h" 6#include "common/file_util.h"
7#include "common/logging/log.h" 7#include "common/logging/log.h"
8#include "core/file_sys/partition_filesystem.h" 8#include "core/file_sys/partition_filesystem.h"
9#include "core/file_sys/vfs_offset.h"
9#include "core/loader/loader.h" 10#include "core/loader/loader.h"
10 11
11namespace FileSys { 12namespace FileSys {
12 13
13Loader::ResultStatus PartitionFilesystem::Load(const std::string& file_path, size_t offset) { 14PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) {
14 FileUtil::IOFile file(file_path, "rb");
15 if (!file.IsOpen())
16 return Loader::ResultStatus::Error;
17
18 // At least be as large as the header 15 // At least be as large as the header
19 if (file.GetSize() < sizeof(Header)) 16 if (file->GetSize() < sizeof(Header)) {
20 return Loader::ResultStatus::Error; 17 status = Loader::ResultStatus::Error;
18 return;
19 }
21 20
22 file.Seek(offset, SEEK_SET);
23 // For cartridges, HFSs can get very large, so we need to calculate the size up to 21 // For cartridges, HFSs can get very large, so we need to calculate the size up to
24 // the actual content itself instead of just blindly reading in the entire file. 22 // the actual content itself instead of just blindly reading in the entire file.
25 Header pfs_header; 23 Header pfs_header;
26 if (!file.ReadBytes(&pfs_header, sizeof(Header))) 24 if (sizeof(Header) != file->ReadObject(&pfs_header)) {
27 return Loader::ResultStatus::Error; 25 status = Loader::ResultStatus::Error;
26 return;
27 }
28 28
29 if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && 29 if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') &&
30 pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { 30 pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) {
31 return Loader::ResultStatus::ErrorInvalidFormat; 31 status = Loader::ResultStatus::ErrorInvalidFormat;
32 return;
32 } 33 }
33 34
34 bool is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); 35 bool is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0');
@@ -38,99 +39,86 @@ Loader::ResultStatus PartitionFilesystem::Load(const std::string& file_path, siz
38 sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size; 39 sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size;
39 40
40 // Actually read in now... 41 // Actually read in now...
41 file.Seek(offset, SEEK_SET); 42 std::vector<u8> file_data = file->ReadBytes(metadata_size);
42 std::vector<u8> file_data(metadata_size);
43
44 if (!file.ReadBytes(file_data.data(), metadata_size))
45 return Loader::ResultStatus::Error;
46 43
47 Loader::ResultStatus result = Load(file_data); 44 if (file_data.size() != metadata_size) {
48 if (result != Loader::ResultStatus::Success) 45 status = Loader::ResultStatus::Error;
49 LOG_ERROR(Service_FS, "Failed to load PFS from file {}!", file_path); 46 return;
50 47 }
51 return result;
52}
53 48
54Loader::ResultStatus PartitionFilesystem::Load(const std::vector<u8>& file_data, size_t offset) { 49 size_t total_size = file_data.size();
55 size_t total_size = file_data.size() - offset; 50 if (total_size < sizeof(Header)) {
56 if (total_size < sizeof(Header)) 51 status = Loader::ResultStatus::Error;
57 return Loader::ResultStatus::Error; 52 return;
53 }
58 54
59 memcpy(&pfs_header, &file_data[offset], sizeof(Header)); 55 memcpy(&pfs_header, file_data.data(), sizeof(Header));
60 if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && 56 if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') &&
61 pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { 57 pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) {
62 return Loader::ResultStatus::ErrorInvalidFormat; 58 status = Loader::ResultStatus::ErrorInvalidFormat;
59 return;
63 } 60 }
64 61
65 is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); 62 is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0');
66 63
67 size_t entries_offset = offset + sizeof(Header); 64 size_t entries_offset = sizeof(Header);
68 size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
69 size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size); 65 size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size);
66 content_offset = strtab_offset + pfs_header.strtab_size;
70 for (u16 i = 0; i < pfs_header.num_entries; i++) { 67 for (u16 i = 0; i < pfs_header.num_entries; i++) {
71 FileEntry entry; 68 FSEntry entry;
72 69
73 memcpy(&entry.fs_entry, &file_data[entries_offset + (i * entry_size)], sizeof(FSEntry)); 70 memcpy(&entry, &file_data[entries_offset + (i * entry_size)], sizeof(FSEntry));
74 entry.name = std::string(reinterpret_cast<const char*>( 71 std::string name(
75 &file_data[strtab_offset + entry.fs_entry.strtab_offset])); 72 reinterpret_cast<const char*>(&file_data[strtab_offset + entry.strtab_offset]));
76 pfs_entries.push_back(std::move(entry));
77 }
78 73
79 content_offset = strtab_offset + pfs_header.strtab_size; 74 pfs_files.emplace_back(
75 std::make_shared<OffsetVfsFile>(file, entry.size, content_offset + entry.offset, name));
76 }
80 77
81 return Loader::ResultStatus::Success; 78 status = Loader::ResultStatus::Success;
82} 79}
83 80
84u32 PartitionFilesystem::GetNumEntries() const { 81Loader::ResultStatus PartitionFilesystem::GetStatus() const {
85 return pfs_header.num_entries; 82 return status;
86} 83}
87 84
88u64 PartitionFilesystem::GetEntryOffset(u32 index) const { 85std::vector<std::shared_ptr<VfsFile>> PartitionFilesystem::GetFiles() const {
89 if (index > GetNumEntries()) 86 return pfs_files;
90 return 0;
91
92 return content_offset + pfs_entries[index].fs_entry.offset;
93} 87}
94 88
95u64 PartitionFilesystem::GetEntrySize(u32 index) const { 89std::vector<std::shared_ptr<VfsDirectory>> PartitionFilesystem::GetSubdirectories() const {
96 if (index > GetNumEntries()) 90 return {};
97 return 0;
98
99 return pfs_entries[index].fs_entry.size;
100} 91}
101 92
102std::string PartitionFilesystem::GetEntryName(u32 index) const { 93std::string PartitionFilesystem::GetName() const {
103 if (index > GetNumEntries()) 94 return is_hfs ? "HFS0" : "PFS0";
104 return ""; 95}
105 96
106 return pfs_entries[index].name; 97std::shared_ptr<VfsDirectory> PartitionFilesystem::GetParentDirectory() const {
98 // TODO(DarkLordZach): Add support for nested containers.
99 return nullptr;
107} 100}
108 101
109u64 PartitionFilesystem::GetFileOffset(const std::string& name) const { 102void PartitionFilesystem::PrintDebugInfo() const {
103 LOG_DEBUG(Service_FS, "Magic: {:.4}", pfs_header.magic);
104 LOG_DEBUG(Service_FS, "Files: {}", pfs_header.num_entries);
110 for (u32 i = 0; i < pfs_header.num_entries; i++) { 105 for (u32 i = 0; i < pfs_header.num_entries; i++) {
111 if (pfs_entries[i].name == name) 106 LOG_DEBUG(Service_FS, " > File {}: {} (0x{:X} bytes, at 0x{:X})", i,
112 return content_offset + pfs_entries[i].fs_entry.offset; 107 pfs_files[i]->GetName(), pfs_files[i]->GetSize(),
108 dynamic_cast<OffsetVfsFile*>(pfs_files[i].get())->GetOffset());
113 } 109 }
114
115 return 0;
116} 110}
117 111
118u64 PartitionFilesystem::GetFileSize(const std::string& name) const { 112bool PartitionFilesystem::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
119 for (u32 i = 0; i < pfs_header.num_entries; i++) { 113 auto iter = std::find(pfs_files.begin(), pfs_files.end(), file);
120 if (pfs_entries[i].name == name) 114 if (iter == pfs_files.end())
121 return pfs_entries[i].fs_entry.size; 115 return false;
122 }
123 116
124 return 0; 117 pfs_files[iter - pfs_files.begin()] = pfs_files.back();
125} 118 pfs_files.pop_back();
126 119
127void PartitionFilesystem::Print() const { 120 pfs_dirs.emplace_back(dir);
128 LOG_DEBUG(Service_FS, "Magic: {}", pfs_header.magic); 121
129 LOG_DEBUG(Service_FS, "Files: {}", pfs_header.num_entries); 122 return true;
130 for (u32 i = 0; i < pfs_header.num_entries; i++) {
131 LOG_DEBUG(Service_FS, " > File {}: {} (0x{:X} bytes, at 0x{:X})", i,
132 pfs_entries[i].name.c_str(), pfs_entries[i].fs_entry.size,
133 GetFileOffset(pfs_entries[i].name));
134 }
135} 123}
136} // namespace FileSys 124} // namespace FileSys
diff --git a/src/core/file_sys/partition_filesystem.h b/src/core/file_sys/partition_filesystem.h
index 9c5810cf1..9656b40bf 100644
--- a/src/core/file_sys/partition_filesystem.h
+++ b/src/core/file_sys/partition_filesystem.h
@@ -10,6 +10,7 @@
10#include "common/common_funcs.h" 10#include "common/common_funcs.h"
11#include "common/common_types.h" 11#include "common/common_types.h"
12#include "common/swap.h" 12#include "common/swap.h"
13#include "core/file_sys/vfs.h"
13 14
14namespace Loader { 15namespace Loader {
15enum class ResultStatus; 16enum class ResultStatus;
@@ -21,19 +22,19 @@ namespace FileSys {
21 * Helper which implements an interface to parse PFS/HFS filesystems. 22 * Helper which implements an interface to parse PFS/HFS filesystems.
22 * Data can either be loaded from a file path or data with an offset into it. 23 * Data can either be loaded from a file path or data with an offset into it.
23 */ 24 */
24class PartitionFilesystem { 25class PartitionFilesystem : public ReadOnlyVfsDirectory {
25public: 26public:
26 Loader::ResultStatus Load(const std::string& file_path, size_t offset = 0); 27 explicit PartitionFilesystem(std::shared_ptr<VfsFile> file);
27 Loader::ResultStatus Load(const std::vector<u8>& file_data, size_t offset = 0); 28 Loader::ResultStatus GetStatus() const;
28 29
29 u32 GetNumEntries() const; 30 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
30 u64 GetEntryOffset(u32 index) const; 31 std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
31 u64 GetEntrySize(u32 index) const; 32 std::string GetName() const override;
32 std::string GetEntryName(u32 index) const; 33 std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
33 u64 GetFileOffset(const std::string& name) const; 34 void PrintDebugInfo() const;
34 u64 GetFileSize(const std::string& name) const;
35 35
36 void Print() const; 36protected:
37 bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
37 38
38private: 39private:
39 struct Header { 40 struct Header {
@@ -72,16 +73,14 @@ private:
72 73
73#pragma pack(pop) 74#pragma pack(pop)
74 75
75 struct FileEntry { 76 Loader::ResultStatus status;
76 FSEntry fs_entry;
77 std::string name;
78 };
79 77
80 Header pfs_header; 78 Header pfs_header;
81 bool is_hfs; 79 bool is_hfs;
82 size_t content_offset; 80 size_t content_offset;
83 81
84 std::vector<FileEntry> pfs_entries; 82 std::vector<VirtualFile> pfs_files;
83 std::vector<VirtualDir> pfs_dirs;
85}; 84};
86 85
87} // namespace FileSys 86} // namespace FileSys
diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp
index 226811115..63d4b6e4f 100644
--- a/src/core/file_sys/program_metadata.cpp
+++ b/src/core/file_sys/program_metadata.cpp
@@ -9,40 +9,29 @@
9 9
10namespace FileSys { 10namespace FileSys {
11 11
12Loader::ResultStatus ProgramMetadata::Load(const std::string& file_path) { 12Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) {
13 FileUtil::IOFile file(file_path, "rb"); 13 size_t total_size = static_cast<size_t>(file->GetSize());
14 if (!file.IsOpen()) 14 if (total_size < sizeof(Header))
15 return Loader::ResultStatus::Error; 15 return Loader::ResultStatus::Error;
16 16
17 std::vector<u8> file_data(file.GetSize()); 17 // TODO(DarkLordZach): Use ReadObject when Header/AcidHeader becomes trivially copyable.
18 18 std::vector<u8> npdm_header_data = file->ReadBytes(sizeof(Header));
19 if (!file.ReadBytes(file_data.data(), file_data.size())) 19 if (sizeof(Header) != npdm_header_data.size())
20 return Loader::ResultStatus::Error; 20 return Loader::ResultStatus::Error;
21 std::memcpy(&npdm_header, npdm_header_data.data(), sizeof(Header));
21 22
22 Loader::ResultStatus result = Load(file_data); 23 std::vector<u8> acid_header_data = file->ReadBytes(sizeof(AcidHeader), npdm_header.acid_offset);
23 if (result != Loader::ResultStatus::Success) 24 if (sizeof(AcidHeader) != acid_header_data.size())
24 LOG_ERROR(Service_FS, "Failed to load NPDM from file {}!", file_path);
25
26 return result;
27}
28
29Loader::ResultStatus ProgramMetadata::Load(const std::vector<u8> file_data, size_t offset) {
30 size_t total_size = static_cast<size_t>(file_data.size() - offset);
31 if (total_size < sizeof(Header))
32 return Loader::ResultStatus::Error; 25 return Loader::ResultStatus::Error;
26 std::memcpy(&acid_header, acid_header_data.data(), sizeof(AcidHeader));
33 27
34 size_t header_offset = offset; 28 if (sizeof(AciHeader) != file->ReadObject(&aci_header, npdm_header.aci_offset))
35 memcpy(&npdm_header, &file_data[offset], sizeof(Header)); 29 return Loader::ResultStatus::Error;
36
37 size_t aci_offset = header_offset + npdm_header.aci_offset;
38 size_t acid_offset = header_offset + npdm_header.acid_offset;
39 memcpy(&aci_header, &file_data[aci_offset], sizeof(AciHeader));
40 memcpy(&acid_header, &file_data[acid_offset], sizeof(AcidHeader));
41 30
42 size_t fac_offset = acid_offset + acid_header.fac_offset; 31 if (sizeof(FileAccessControl) != file->ReadObject(&acid_file_access, acid_header.fac_offset))
43 size_t fah_offset = aci_offset + aci_header.fah_offset; 32 return Loader::ResultStatus::Error;
44 memcpy(&acid_file_access, &file_data[fac_offset], sizeof(FileAccessControl)); 33 if (sizeof(FileAccessHeader) != file->ReadObject(&aci_file_access, aci_header.fah_offset))
45 memcpy(&aci_file_access, &file_data[fah_offset], sizeof(FileAccessHeader)); 34 return Loader::ResultStatus::Error;
46 35
47 return Loader::ResultStatus::Success; 36 return Loader::ResultStatus::Success;
48} 37}
diff --git a/src/core/file_sys/program_metadata.h b/src/core/file_sys/program_metadata.h
index b80a08485..06a7315db 100644
--- a/src/core/file_sys/program_metadata.h
+++ b/src/core/file_sys/program_metadata.h
@@ -10,6 +10,7 @@
10#include "common/bit_field.h" 10#include "common/bit_field.h"
11#include "common/common_types.h" 11#include "common/common_types.h"
12#include "common/swap.h" 12#include "common/swap.h"
13#include "partition_filesystem.h"
13 14
14namespace Loader { 15namespace Loader {
15enum class ResultStatus; 16enum class ResultStatus;
@@ -37,8 +38,7 @@ enum class ProgramFilePermission : u64 {
37 */ 38 */
38class ProgramMetadata { 39class ProgramMetadata {
39public: 40public:
40 Loader::ResultStatus Load(const std::string& file_path); 41 Loader::ResultStatus Load(VirtualFile file);
41 Loader::ResultStatus Load(const std::vector<u8> file_data, size_t offset = 0);
42 42
43 bool Is64BitProgram() const; 43 bool Is64BitProgram() const;
44 ProgramAddressSpaceType GetAddressSpaceType() const; 44 ProgramAddressSpaceType GetAddressSpaceType() const;
@@ -51,6 +51,7 @@ public:
51 void Print() const; 51 void Print() const;
52 52
53private: 53private:
54 // TODO(DarkLordZach): BitField is not trivially copyable.
54 struct Header { 55 struct Header {
55 std::array<char, 4> magic; 56 std::array<char, 4> magic;
56 std::array<u8, 8> reserved; 57 std::array<u8, 8> reserved;
@@ -77,6 +78,7 @@ private:
77 78
78 static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong"); 79 static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong");
79 80
81 // TODO(DarkLordZach): BitField is not trivially copyable.
80 struct AcidHeader { 82 struct AcidHeader {
81 std::array<u8, 0x100> signature; 83 std::array<u8, 0x100> signature;
82 std::array<u8, 0x100> nca_modulus; 84 std::array<u8, 0x100> nca_modulus;
diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp
deleted file mode 100644
index 84ae0d99b..000000000
--- a/src/core/file_sys/romfs_factory.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <memory>
7#include "common/common_types.h"
8#include "common/logging/log.h"
9#include "core/file_sys/romfs_factory.h"
10#include "core/file_sys/romfs_filesystem.h"
11
12namespace FileSys {
13
14RomFS_Factory::RomFS_Factory(Loader::AppLoader& app_loader) {
15 // Load the RomFS from the app
16 if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) {
17 LOG_ERROR(Service_FS, "Unable to read RomFS!");
18 }
19}
20
21ResultVal<std::unique_ptr<FileSystemBackend>> RomFS_Factory::Open(const Path& path) {
22 auto archive = std::make_unique<RomFS_FileSystem>(romfs_file, data_offset, data_size);
23 return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
24}
25
26ResultCode RomFS_Factory::Format(const Path& path) {
27 LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName());
28 // TODO(bunnei): Find the right error code for this
29 return ResultCode(-1);
30}
31
32ResultVal<ArchiveFormatInfo> RomFS_Factory::GetFormatInfo(const Path& path) const {
33 LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName());
34 // TODO(bunnei): Find the right error code for this
35 return ResultCode(-1);
36}
37
38} // namespace FileSys
diff --git a/src/core/file_sys/romfs_factory.h b/src/core/file_sys/romfs_factory.h
deleted file mode 100644
index e0698e642..000000000
--- a/src/core/file_sys/romfs_factory.h
+++ /dev/null
@@ -1,35 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include <vector>
10#include "common/common_types.h"
11#include "core/file_sys/filesystem.h"
12#include "core/hle/result.h"
13#include "core/loader/loader.h"
14
15namespace FileSys {
16
17/// File system interface to the RomFS archive
18class RomFS_Factory final : public FileSystemFactory {
19public:
20 explicit RomFS_Factory(Loader::AppLoader& app_loader);
21
22 std::string GetName() const override {
23 return "ArchiveFactory_RomFS";
24 }
25 ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) override;
26 ResultCode Format(const Path& path) override;
27 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
28
29private:
30 std::shared_ptr<FileUtil::IOFile> romfs_file;
31 u64 data_offset;
32 u64 data_size;
33};
34
35} // namespace FileSys
diff --git a/src/core/file_sys/romfs_filesystem.cpp b/src/core/file_sys/romfs_filesystem.cpp
deleted file mode 100644
index 83162622b..000000000
--- a/src/core/file_sys/romfs_filesystem.cpp
+++ /dev/null
@@ -1,110 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <memory>
7#include "common/common_types.h"
8#include "common/logging/log.h"
9#include "core/file_sys/romfs_filesystem.h"
10
11namespace FileSys {
12
13std::string RomFS_FileSystem::GetName() const {
14 return "RomFS";
15}
16
17ResultVal<std::unique_ptr<StorageBackend>> RomFS_FileSystem::OpenFile(const std::string& path,
18 Mode mode) const {
19 return MakeResult<std::unique_ptr<StorageBackend>>(
20 std::make_unique<RomFS_Storage>(romfs_file, data_offset, data_size));
21}
22
23ResultCode RomFS_FileSystem::DeleteFile(const std::string& path) const {
24 LOG_CRITICAL(Service_FS, "Attempted to delete a file from an ROMFS archive ({}).", GetName());
25 // TODO(bunnei): Use correct error code
26 return ResultCode(-1);
27}
28
29ResultCode RomFS_FileSystem::RenameFile(const std::string& src_path,
30 const std::string& dest_path) const {
31 LOG_CRITICAL(Service_FS, "Attempted to rename a file within an ROMFS archive ({}).", GetName());
32 // TODO(wwylele): Use correct error code
33 return ResultCode(-1);
34}
35
36ResultCode RomFS_FileSystem::DeleteDirectory(const Path& path) const {
37 LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an ROMFS archive ({}).",
38 GetName());
39 // TODO(wwylele): Use correct error code
40 return ResultCode(-1);
41}
42
43ResultCode RomFS_FileSystem::DeleteDirectoryRecursively(const Path& path) const {
44 LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an ROMFS archive ({}).",
45 GetName());
46 // TODO(wwylele): Use correct error code
47 return ResultCode(-1);
48}
49
50ResultCode RomFS_FileSystem::CreateFile(const std::string& path, u64 size) const {
51 LOG_CRITICAL(Service_FS, "Attempted to create a file in an ROMFS archive ({}).", GetName());
52 // TODO(bunnei): Use correct error code
53 return ResultCode(-1);
54}
55
56ResultCode RomFS_FileSystem::CreateDirectory(const std::string& path) const {
57 LOG_CRITICAL(Service_FS, "Attempted to create a directory in an ROMFS archive ({}).",
58 GetName());
59 // TODO(wwylele): Use correct error code
60 return ResultCode(-1);
61}
62
63ResultCode RomFS_FileSystem::RenameDirectory(const Path& src_path, const Path& dest_path) const {
64 LOG_CRITICAL(Service_FS, "Attempted to rename a file within an ROMFS archive ({}).", GetName());
65 // TODO(wwylele): Use correct error code
66 return ResultCode(-1);
67}
68
69ResultVal<std::unique_ptr<DirectoryBackend>> RomFS_FileSystem::OpenDirectory(
70 const std::string& path) const {
71 LOG_WARNING(Service_FS, "Opening Directory in a ROMFS archive");
72 return MakeResult<std::unique_ptr<DirectoryBackend>>(std::make_unique<ROMFSDirectory>());
73}
74
75u64 RomFS_FileSystem::GetFreeSpaceSize() const {
76 LOG_WARNING(Service_FS, "Attempted to get the free space in an ROMFS archive");
77 return 0;
78}
79
80ResultVal<FileSys::EntryType> RomFS_FileSystem::GetEntryType(const std::string& path) const {
81 LOG_CRITICAL(Service_FS, "Called within an ROMFS archive (path {}).", path);
82 // TODO(wwylele): Use correct error code
83 return ResultCode(-1);
84}
85
86ResultVal<size_t> RomFS_Storage::Read(const u64 offset, const size_t length, u8* buffer) const {
87 LOG_TRACE(Service_FS, "called offset={}, length={}", offset, length);
88 romfs_file->Seek(data_offset + offset, SEEK_SET);
89 size_t read_length = (size_t)std::min((u64)length, data_size - offset);
90
91 return MakeResult<size_t>(romfs_file->ReadBytes(buffer, read_length));
92}
93
94ResultVal<size_t> RomFS_Storage::Write(const u64 offset, const size_t length, const bool flush,
95 const u8* buffer) const {
96 LOG_ERROR(Service_FS, "Attempted to write to ROMFS file");
97 // TODO(Subv): Find error code
98 return MakeResult<size_t>(0);
99}
100
101u64 RomFS_Storage::GetSize() const {
102 return data_size;
103}
104
105bool RomFS_Storage::SetSize(const u64 size) const {
106 LOG_ERROR(Service_FS, "Attempted to set the size of an ROMFS file");
107 return false;
108}
109
110} // namespace FileSys
diff --git a/src/core/file_sys/romfs_filesystem.h b/src/core/file_sys/romfs_filesystem.h
deleted file mode 100644
index ba9d85823..000000000
--- a/src/core/file_sys/romfs_filesystem.h
+++ /dev/null
@@ -1,85 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <cstddef>
8#include <memory>
9#include <string>
10#include <vector>
11#include "common/common_types.h"
12#include "common/file_util.h"
13#include "core/file_sys/directory.h"
14#include "core/file_sys/filesystem.h"
15#include "core/file_sys/storage.h"
16#include "core/hle/result.h"
17
18namespace FileSys {
19
20/**
21 * Helper which implements an interface to deal with Switch .istorage ROMFS images used in some
22 * archives This should be subclassed by concrete archive types, which will provide the input data
23 * (load the raw ROMFS archive) and override any required methods
24 */
25class RomFS_FileSystem : public FileSystemBackend {
26public:
27 RomFS_FileSystem(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
28 : romfs_file(file), data_offset(offset), data_size(size) {}
29
30 std::string GetName() const override;
31
32 ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const std::string& path,
33 Mode mode) const override;
34 ResultCode DeleteFile(const std::string& path) const override;
35 ResultCode RenameFile(const std::string& src_path, const std::string& dest_path) const override;
36 ResultCode DeleteDirectory(const Path& path) const override;
37 ResultCode DeleteDirectoryRecursively(const Path& path) const override;
38 ResultCode CreateFile(const std::string& path, u64 size) const override;
39 ResultCode CreateDirectory(const std::string& path) const override;
40 ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override;
41 ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(
42 const std::string& path) const override;
43 u64 GetFreeSpaceSize() const override;
44 ResultVal<EntryType> GetEntryType(const std::string& path) const override;
45
46protected:
47 std::shared_ptr<FileUtil::IOFile> romfs_file;
48 u64 data_offset;
49 u64 data_size;
50};
51
52class RomFS_Storage : public StorageBackend {
53public:
54 RomFS_Storage(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
55 : romfs_file(file), data_offset(offset), data_size(size) {}
56
57 ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
58 ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
59 u64 GetSize() const override;
60 bool SetSize(u64 size) const override;
61 bool Close() const override {
62 return false;
63 }
64 void Flush() const override {}
65
66private:
67 std::shared_ptr<FileUtil::IOFile> romfs_file;
68 u64 data_offset;
69 u64 data_size;
70};
71
72class ROMFSDirectory : public DirectoryBackend {
73public:
74 u64 Read(const u64 count, Entry* entries) override {
75 return 0;
76 }
77 u64 GetEntryCount() const override {
78 return 0;
79 }
80 bool Close() const override {
81 return false;
82 }
83};
84
85} // namespace FileSys
diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp
deleted file mode 100644
index d78baf9c3..000000000
--- a/src/core/file_sys/savedata_factory.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <memory>
6#include "common/common_types.h"
7#include "common/logging/log.h"
8#include "core/core.h"
9#include "core/file_sys/disk_filesystem.h"
10#include "core/file_sys/savedata_factory.h"
11#include "core/hle/kernel/process.h"
12
13namespace FileSys {
14
15SaveData_Factory::SaveData_Factory(std::string nand_directory)
16 : nand_directory(std::move(nand_directory)) {}
17
18ResultVal<std::unique_ptr<FileSystemBackend>> SaveData_Factory::Open(const Path& path) {
19 std::string save_directory = GetFullPath();
20 // Return an error if the save data doesn't actually exist.
21 if (!FileUtil::IsDirectory(save_directory)) {
22 // TODO(Subv): Find out correct error code.
23 return ResultCode(-1);
24 }
25
26 auto archive = std::make_unique<Disk_FileSystem>(save_directory);
27 return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
28}
29
30ResultCode SaveData_Factory::Format(const Path& path) {
31 LOG_WARNING(Service_FS, "Format archive {}", GetName());
32 // Create the save data directory.
33 if (!FileUtil::CreateFullPath(GetFullPath())) {
34 // TODO(Subv): Find the correct error code.
35 return ResultCode(-1);
36 }
37
38 return RESULT_SUCCESS;
39}
40
41ResultVal<ArchiveFormatInfo> SaveData_Factory::GetFormatInfo(const Path& path) const {
42 LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName());
43 // TODO(bunnei): Find the right error code for this
44 return ResultCode(-1);
45}
46
47std::string SaveData_Factory::GetFullPath() const {
48 u64 title_id = Core::CurrentProcess()->program_id;
49 // TODO(Subv): Somehow obtain this value.
50 u32 user = 0;
51 return fmt::format("{}save/{:016X}/{:08X}/", nand_directory, title_id, user);
52}
53
54} // namespace FileSys
diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h
deleted file mode 100644
index 73a42aab6..000000000
--- a/src/core/file_sys/savedata_factory.h
+++ /dev/null
@@ -1,33 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "common/common_types.h"
10#include "core/file_sys/filesystem.h"
11#include "core/hle/result.h"
12
13namespace FileSys {
14
15/// File system interface to the SaveData archive
16class SaveData_Factory final : public FileSystemFactory {
17public:
18 explicit SaveData_Factory(std::string nand_directory);
19
20 std::string GetName() const override {
21 return "SaveData_Factory";
22 }
23 ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) override;
24 ResultCode Format(const Path& path) override;
25 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
26
27private:
28 std::string nand_directory;
29
30 std::string GetFullPath() const;
31};
32
33} // namespace FileSys
diff --git a/src/core/file_sys/sdmc_factory.cpp b/src/core/file_sys/sdmc_factory.cpp
deleted file mode 100644
index 2e5ffb764..000000000
--- a/src/core/file_sys/sdmc_factory.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <memory>
6#include "common/common_types.h"
7#include "common/logging/log.h"
8#include "common/string_util.h"
9#include "core/core.h"
10#include "core/file_sys/disk_filesystem.h"
11#include "core/file_sys/sdmc_factory.h"
12
13namespace FileSys {
14
15SDMC_Factory::SDMC_Factory(std::string sd_directory) : sd_directory(std::move(sd_directory)) {}
16
17ResultVal<std::unique_ptr<FileSystemBackend>> SDMC_Factory::Open(const Path& path) {
18 // Create the SD Card directory if it doesn't already exist.
19 if (!FileUtil::IsDirectory(sd_directory)) {
20 FileUtil::CreateFullPath(sd_directory);
21 }
22
23 auto archive = std::make_unique<Disk_FileSystem>(sd_directory);
24 return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
25}
26
27ResultCode SDMC_Factory::Format(const Path& path) {
28 LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName());
29 // TODO(Subv): Find the right error code for this
30 return ResultCode(-1);
31}
32
33ResultVal<ArchiveFormatInfo> SDMC_Factory::GetFormatInfo(const Path& path) const {
34 LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName());
35 // TODO(bunnei): Find the right error code for this
36 return ResultCode(-1);
37}
38
39} // namespace FileSys
diff --git a/src/core/file_sys/sdmc_factory.h b/src/core/file_sys/sdmc_factory.h
deleted file mode 100644
index 93becda25..000000000
--- a/src/core/file_sys/sdmc_factory.h
+++ /dev/null
@@ -1,31 +0,0 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "common/common_types.h"
10#include "core/file_sys/filesystem.h"
11#include "core/hle/result.h"
12
13namespace FileSys {
14
15/// File system interface to the SDCard archive
16class SDMC_Factory final : public FileSystemFactory {
17public:
18 explicit SDMC_Factory(std::string sd_directory);
19
20 std::string GetName() const override {
21 return "SDMC_Factory";
22 }
23 ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) override;
24 ResultCode Format(const Path& path) override;
25 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
26
27private:
28 std::string sd_directory;
29};
30
31} // namespace FileSys
diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp
new file mode 100644
index 000000000..c99071d6a
--- /dev/null
+++ b/src/core/file_sys/vfs.cpp
@@ -0,0 +1,187 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <numeric>
7#include "common/file_util.h"
8#include "core/file_sys/vfs.h"
9
10namespace FileSys {
11
12VfsFile::~VfsFile() = default;
13
14std::string VfsFile::GetExtension() const {
15 return FileUtil::GetExtensionFromFilename(GetName());
16}
17
18VfsDirectory::~VfsDirectory() = default;
19
20boost::optional<u8> VfsFile::ReadByte(size_t offset) const {
21 u8 out{};
22 size_t size = Read(&out, 1, offset);
23 if (size == 1)
24 return out;
25
26 return boost::none;
27}
28
29std::vector<u8> VfsFile::ReadBytes(size_t size, size_t offset) const {
30 std::vector<u8> out(size);
31 size_t read_size = Read(out.data(), size, offset);
32 out.resize(read_size);
33 return out;
34}
35
36std::vector<u8> VfsFile::ReadAllBytes() const {
37 return ReadBytes(GetSize());
38}
39
40bool VfsFile::WriteByte(u8 data, size_t offset) {
41 return Write(&data, 1, offset) == 1;
42}
43
44size_t VfsFile::WriteBytes(std::vector<u8> data, size_t offset) {
45 return Write(data.data(), data.size(), offset);
46}
47
48std::shared_ptr<VfsFile> VfsDirectory::GetFileRelative(const std::string& path) const {
49 auto vec = FileUtil::SplitPathComponents(path);
50 vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }),
51 vec.end());
52 if (vec.empty())
53 return nullptr;
54 if (vec.size() == 1)
55 return GetFile(vec[0]);
56 auto dir = GetSubdirectory(vec[0]);
57 for (size_t i = 1; i < vec.size() - 1; ++i) {
58 if (dir == nullptr)
59 return nullptr;
60 dir = dir->GetSubdirectory(vec[i]);
61 }
62 if (dir == nullptr)
63 return nullptr;
64 return dir->GetFile(vec.back());
65}
66
67std::shared_ptr<VfsFile> VfsDirectory::GetFileAbsolute(const std::string& path) const {
68 if (IsRoot())
69 return GetFileRelative(path);
70
71 return GetParentDirectory()->GetFileAbsolute(path);
72}
73
74std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryRelative(const std::string& path) const {
75 auto vec = FileUtil::SplitPathComponents(path);
76 vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }),
77 vec.end());
78 if (vec.empty())
79 // return std::shared_ptr<VfsDirectory>(this);
80 return nullptr;
81 auto dir = GetSubdirectory(vec[0]);
82 for (size_t i = 1; i < vec.size(); ++i) {
83 dir = dir->GetSubdirectory(vec[i]);
84 }
85 return dir;
86}
87
88std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryAbsolute(const std::string& path) const {
89 if (IsRoot())
90 return GetDirectoryRelative(path);
91
92 return GetParentDirectory()->GetDirectoryAbsolute(path);
93}
94
95std::shared_ptr<VfsFile> VfsDirectory::GetFile(const std::string& name) const {
96 const auto& files = GetFiles();
97 const auto iter = std::find_if(files.begin(), files.end(),
98 [&name](const auto& file1) { return name == file1->GetName(); });
99 return iter == files.end() ? nullptr : *iter;
100}
101
102std::shared_ptr<VfsDirectory> VfsDirectory::GetSubdirectory(const std::string& name) const {
103 const auto& subs = GetSubdirectories();
104 const auto iter = std::find_if(subs.begin(), subs.end(),
105 [&name](const auto& file1) { return name == file1->GetName(); });
106 return iter == subs.end() ? nullptr : *iter;
107}
108
109bool VfsDirectory::IsRoot() const {
110 return GetParentDirectory() == nullptr;
111}
112
113size_t VfsDirectory::GetSize() const {
114 const auto& files = GetFiles();
115 const auto file_total =
116 std::accumulate(files.begin(), files.end(), 0ull,
117 [](const auto& f1, const auto& f2) { return f1 + f2->GetSize(); });
118
119 const auto& sub_dir = GetSubdirectories();
120 const auto subdir_total =
121 std::accumulate(sub_dir.begin(), sub_dir.end(), 0ull,
122 [](const auto& f1, const auto& f2) { return f1 + f2->GetSize(); });
123
124 return file_total + subdir_total;
125}
126
127bool VfsDirectory::DeleteSubdirectoryRecursive(const std::string& name) {
128 auto dir = GetSubdirectory(name);
129 if (dir == nullptr)
130 return false;
131
132 bool success = true;
133 for (const auto& file : dir->GetFiles()) {
134 if (!DeleteFile(file->GetName()))
135 success = false;
136 }
137
138 for (const auto& sdir : dir->GetSubdirectories()) {
139 if (!dir->DeleteSubdirectoryRecursive(sdir->GetName()))
140 success = false;
141 }
142
143 return success;
144}
145
146bool VfsDirectory::Copy(const std::string& src, const std::string& dest) {
147 const auto f1 = GetFile(src);
148 auto f2 = CreateFile(dest);
149 if (f1 == nullptr || f2 == nullptr)
150 return false;
151
152 if (!f2->Resize(f1->GetSize())) {
153 DeleteFile(dest);
154 return false;
155 }
156
157 return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize();
158}
159
160bool ReadOnlyVfsDirectory::IsWritable() const {
161 return false;
162}
163
164bool ReadOnlyVfsDirectory::IsReadable() const {
165 return true;
166}
167
168std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectory::CreateSubdirectory(const std::string& name) {
169 return nullptr;
170}
171
172std::shared_ptr<VfsFile> ReadOnlyVfsDirectory::CreateFile(const std::string& name) {
173 return nullptr;
174}
175
176bool ReadOnlyVfsDirectory::DeleteSubdirectory(const std::string& name) {
177 return false;
178}
179
180bool ReadOnlyVfsDirectory::DeleteFile(const std::string& name) {
181 return false;
182}
183
184bool ReadOnlyVfsDirectory::Rename(const std::string& name) {
185 return false;
186}
187} // namespace FileSys
diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h
new file mode 100644
index 000000000..0e30991b4
--- /dev/null
+++ b/src/core/file_sys/vfs.h
@@ -0,0 +1,220 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include <type_traits>
10#include <vector>
11#include "boost/optional.hpp"
12#include "common/common_types.h"
13#include "common/file_util.h"
14
15namespace FileSys {
16struct VfsFile;
17struct VfsDirectory;
18
19// Convenience typedefs to use VfsDirectory and VfsFile
20using VirtualDir = std::shared_ptr<FileSys::VfsDirectory>;
21using VirtualFile = std::shared_ptr<FileSys::VfsFile>;
22
23// A class representing a file in an abstract filesystem.
24struct VfsFile : NonCopyable {
25 virtual ~VfsFile();
26
27 // Retrieves the file name.
28 virtual std::string GetName() const = 0;
29 // Retrieves the extension of the file name.
30 virtual std::string GetExtension() const;
31 // Retrieves the size of the file.
32 virtual size_t GetSize() const = 0;
33 // Resizes the file to new_size. Returns whether or not the operation was successful.
34 virtual bool Resize(size_t new_size) = 0;
35 // Gets a pointer to the directory containing this file, returning nullptr if there is none.
36 virtual std::shared_ptr<VfsDirectory> GetContainingDirectory() const = 0;
37
38 // Returns whether or not the file can be written to.
39 virtual bool IsWritable() const = 0;
40 // Returns whether or not the file can be read from.
41 virtual bool IsReadable() const = 0;
42
43 // The primary method of reading from the file. Reads length bytes into data starting at offset
44 // into file. Returns number of bytes successfully read.
45 virtual size_t Read(u8* data, size_t length, size_t offset = 0) const = 0;
46 // The primary method of writing to the file. Writes length bytes from data starting at offset
47 // into file. Returns number of bytes successfully written.
48 virtual size_t Write(const u8* data, size_t length, size_t offset = 0) = 0;
49
50 // Reads exactly one byte at the offset provided, returning boost::none on error.
51 virtual boost::optional<u8> ReadByte(size_t offset = 0) const;
52 // Reads size bytes starting at offset in file into a vector.
53 virtual std::vector<u8> ReadBytes(size_t size, size_t offset = 0) const;
54 // Reads all the bytes from the file into a vector. Equivalent to 'file->Read(file->GetSize(),
55 // 0)'
56 virtual std::vector<u8> ReadAllBytes() const;
57
58 // Reads an array of type T, size number_elements starting at offset.
59 // Returns the number of bytes (sizeof(T)*number_elements) read successfully.
60 template <typename T>
61 size_t ReadArray(T* data, size_t number_elements, size_t offset = 0) const {
62 static_assert(std::is_trivially_copyable<T>::value,
63 "Data type must be trivially copyable.");
64
65 return Read(reinterpret_cast<u8*>(data), number_elements * sizeof(T), offset);
66 }
67
68 // Reads size bytes into the memory starting at data starting at offset into the file.
69 // Returns the number of bytes read successfully.
70 template <typename T>
71 size_t ReadBytes(T* data, size_t size, size_t offset = 0) const {
72 static_assert(std::is_trivially_copyable<T>::value,
73 "Data type must be trivially copyable.");
74 return Read(reinterpret_cast<u8*>(data), size, offset);
75 }
76
77 // Reads one object of type T starting at offset in file.
78 // Returns the number of bytes read successfully (sizeof(T)).
79 template <typename T>
80 size_t ReadObject(T* data, size_t offset = 0) const {
81 static_assert(std::is_trivially_copyable<T>::value,
82 "Data type must be trivially copyable.");
83 return Read(reinterpret_cast<u8*>(data), sizeof(T), offset);
84 }
85
86 // Writes exactly one byte to offset in file and retuns whether or not the byte was written
87 // successfully.
88 virtual bool WriteByte(u8 data, size_t offset = 0);
89 // Writes a vector of bytes to offset in file and returns the number of bytes successfully
90 // written.
91 virtual size_t WriteBytes(std::vector<u8> data, size_t offset = 0);
92
93 // Writes an array of type T, size number_elements to offset in file.
94 // Returns the number of bytes (sizeof(T)*number_elements) written successfully.
95 template <typename T>
96 size_t WriteArray(T* data, size_t number_elements, size_t offset = 0) {
97 static_assert(std::is_trivially_copyable<T>::value,
98 "Data type must be trivially copyable.");
99
100 return Write(data, number_elements * sizeof(T), offset);
101 }
102
103 // Writes size bytes starting at memory location data to offset in file.
104 // Returns the number of bytes written successfully.
105 template <typename T>
106 size_t WriteBytes(T* data, size_t size, size_t offset = 0) {
107 static_assert(std::is_trivially_copyable<T>::value,
108 "Data type must be trivially copyable.");
109 return Write(reinterpret_cast<u8*>(data), size, offset);
110 }
111
112 // Writes one object of type T to offset in file.
113 // Returns the number of bytes written successfully (sizeof(T)).
114 template <typename T>
115 size_t WriteObject(const T& data, size_t offset = 0) {
116 static_assert(std::is_trivially_copyable<T>::value,
117 "Data type must be trivially copyable.");
118 return Write(&data, sizeof(T), offset);
119 }
120
121 // Renames the file to name. Returns whether or not the operation was successsful.
122 virtual bool Rename(const std::string& name) = 0;
123};
124
125// A class representing a directory in an abstract filesystem.
126struct VfsDirectory : NonCopyable {
127 virtual ~VfsDirectory();
128
129 // Retrives the file located at path as if the current directory was root. Returns nullptr if
130 // not found.
131 virtual std::shared_ptr<VfsFile> GetFileRelative(const std::string& path) const;
132 // Calls GetFileRelative(path) on the root of the current directory.
133 virtual std::shared_ptr<VfsFile> GetFileAbsolute(const std::string& path) const;
134
135 // Retrives the directory located at path as if the current directory was root. Returns nullptr
136 // if not found.
137 virtual std::shared_ptr<VfsDirectory> GetDirectoryRelative(const std::string& path) const;
138 // Calls GetDirectoryRelative(path) on the root of the current directory.
139 virtual std::shared_ptr<VfsDirectory> GetDirectoryAbsolute(const std::string& path) const;
140
141 // Returns a vector containing all of the files in this directory.
142 virtual std::vector<std::shared_ptr<VfsFile>> GetFiles() const = 0;
143 // Returns the file with filename matching name. Returns nullptr if directory dosen't have a
144 // file with name.
145 virtual std::shared_ptr<VfsFile> GetFile(const std::string& name) const;
146
147 // Returns a vector containing all of the subdirectories in this directory.
148 virtual std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const = 0;
149 // Returns the directory with name matching name. Returns nullptr if directory dosen't have a
150 // directory with name.
151 virtual std::shared_ptr<VfsDirectory> GetSubdirectory(const std::string& name) const;
152
153 // Returns whether or not the directory can be written to.
154 virtual bool IsWritable() const = 0;
155 // Returns whether of not the directory can be read from.
156 virtual bool IsReadable() const = 0;
157
158 // Returns whether or not the directory is the root of the current file tree.
159 virtual bool IsRoot() const;
160
161 // Returns the name of the directory.
162 virtual std::string GetName() const = 0;
163 // Returns the total size of all files and subdirectories in this directory.
164 virtual size_t GetSize() const;
165 // Returns the parent directory of this directory. Returns nullptr if this directory is root or
166 // has no parent.
167 virtual std::shared_ptr<VfsDirectory> GetParentDirectory() const = 0;
168
169 // Creates a new subdirectory with name name. Returns a pointer to the new directory or nullptr
170 // if the operation failed.
171 virtual std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) = 0;
172 // Creates a new file with name name. Returns a pointer to the new file or nullptr if the
173 // operation failed.
174 virtual std::shared_ptr<VfsFile> CreateFile(const std::string& name) = 0;
175
176 // Deletes the subdirectory with name and returns true on success.
177 virtual bool DeleteSubdirectory(const std::string& name) = 0;
178 // Deletes all subdirectories and files of subdirectory with name recirsively and then deletes
179 // the subdirectory. Returns true on success.
180 virtual bool DeleteSubdirectoryRecursive(const std::string& name);
181 // Returnes whether or not the file with name name was deleted successfully.
182 virtual bool DeleteFile(const std::string& name) = 0;
183
184 // Returns whether or not this directory was renamed to name.
185 virtual bool Rename(const std::string& name) = 0;
186
187 // Returns whether or not the file with name src was successfully copied to a new file with name
188 // dest.
189 virtual bool Copy(const std::string& src, const std::string& dest);
190
191 // Interprets the file with name file instead as a directory of type directory.
192 // The directory must have a constructor that takes a single argument of type
193 // std::shared_ptr<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a
194 // subdirectory in one call.
195 template <typename Directory>
196 bool InterpretAsDirectory(const std::string& file) {
197 auto file_p = GetFile(file);
198 if (file_p == nullptr)
199 return false;
200 return ReplaceFileWithSubdirectory(file, std::make_shared<Directory>(file_p));
201 }
202
203protected:
204 // Backend for InterpretAsDirectory.
205 // Removes all references to file and adds a reference to dir in the directory's implementation.
206 virtual bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) = 0;
207};
208
209// A convenience partial-implementation of VfsDirectory that stubs out methods that should only work
210// if writable. This is to avoid redundant empty methods everywhere.
211struct ReadOnlyVfsDirectory : public VfsDirectory {
212 bool IsWritable() const override;
213 bool IsReadable() const override;
214 std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) override;
215 std::shared_ptr<VfsFile> CreateFile(const std::string& name) override;
216 bool DeleteSubdirectory(const std::string& name) override;
217 bool DeleteFile(const std::string& name) override;
218 bool Rename(const std::string& name) override;
219};
220} // namespace FileSys
diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp
new file mode 100644
index 000000000..288499cb5
--- /dev/null
+++ b/src/core/file_sys/vfs_offset.cpp
@@ -0,0 +1,92 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/file_sys/vfs_offset.h"
6
7namespace FileSys {
8
9OffsetVfsFile::OffsetVfsFile(std::shared_ptr<VfsFile> file_, size_t size_, size_t offset_,
10 const std::string& name_)
11 : file(file_), offset(offset_), size(size_), name(name_) {}
12
13std::string OffsetVfsFile::GetName() const {
14 return name.empty() ? file->GetName() : name;
15}
16
17size_t OffsetVfsFile::GetSize() const {
18 return size;
19}
20
21bool OffsetVfsFile::Resize(size_t new_size) {
22 if (offset + new_size < file->GetSize()) {
23 size = new_size;
24 } else {
25 auto res = file->Resize(offset + new_size);
26 if (!res)
27 return false;
28 size = new_size;
29 }
30
31 return true;
32}
33
34std::shared_ptr<VfsDirectory> OffsetVfsFile::GetContainingDirectory() const {
35 return file->GetContainingDirectory();
36}
37
38bool OffsetVfsFile::IsWritable() const {
39 return file->IsWritable();
40}
41
42bool OffsetVfsFile::IsReadable() const {
43 return file->IsReadable();
44}
45
46size_t OffsetVfsFile::Read(u8* data, size_t length, size_t r_offset) const {
47 return file->Read(data, TrimToFit(length, r_offset), offset + r_offset);
48}
49
50size_t OffsetVfsFile::Write(const u8* data, size_t length, size_t r_offset) {
51 return file->Write(data, TrimToFit(length, r_offset), offset + r_offset);
52}
53
54boost::optional<u8> OffsetVfsFile::ReadByte(size_t r_offset) const {
55 if (r_offset < size)
56 return file->ReadByte(offset + r_offset);
57
58 return boost::none;
59}
60
61std::vector<u8> OffsetVfsFile::ReadBytes(size_t r_size, size_t r_offset) const {
62 return file->ReadBytes(TrimToFit(r_size, r_offset), offset + r_offset);
63}
64
65std::vector<u8> OffsetVfsFile::ReadAllBytes() const {
66 return file->ReadBytes(size, offset);
67}
68
69bool OffsetVfsFile::WriteByte(u8 data, size_t r_offset) {
70 if (r_offset < size)
71 return file->WriteByte(data, offset + r_offset);
72
73 return false;
74}
75
76size_t OffsetVfsFile::WriteBytes(std::vector<u8> data, size_t r_offset) {
77 return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset);
78}
79
80bool OffsetVfsFile::Rename(const std::string& name) {
81 return file->Rename(name);
82}
83
84size_t OffsetVfsFile::GetOffset() const {
85 return offset;
86}
87
88size_t OffsetVfsFile::TrimToFit(size_t r_size, size_t r_offset) const {
89 return std::max<size_t>(std::min<size_t>(size - r_offset, r_size), 0);
90}
91
92} // namespace FileSys
diff --git a/src/core/file_sys/vfs_offset.h b/src/core/file_sys/vfs_offset.h
new file mode 100644
index 000000000..adc615b38
--- /dev/null
+++ b/src/core/file_sys/vfs_offset.h
@@ -0,0 +1,46 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/file_sys/vfs.h"
8
9namespace FileSys {
10
11// An implementation of VfsFile that wraps around another VfsFile at a certain offset.
12// Similar to seeking to an offset.
13// If the file is writable, operations that would write past the end of the offset file will expand
14// the size of this wrapper.
15struct OffsetVfsFile : public VfsFile {
16 OffsetVfsFile(std::shared_ptr<VfsFile> file, size_t size, size_t offset = 0,
17 const std::string& new_name = "");
18
19 std::string GetName() const override;
20 size_t GetSize() const override;
21 bool Resize(size_t new_size) override;
22 std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
23 bool IsWritable() const override;
24 bool IsReadable() const override;
25 size_t Read(u8* data, size_t length, size_t offset) const override;
26 size_t Write(const u8* data, size_t length, size_t offset) override;
27 boost::optional<u8> ReadByte(size_t offset) const override;
28 std::vector<u8> ReadBytes(size_t size, size_t offset) const override;
29 std::vector<u8> ReadAllBytes() const override;
30 bool WriteByte(u8 data, size_t offset) override;
31 size_t WriteBytes(std::vector<u8> data, size_t offset) override;
32
33 bool Rename(const std::string& name) override;
34
35 size_t GetOffset() const;
36
37private:
38 size_t TrimToFit(size_t r_size, size_t r_offset) const;
39
40 std::shared_ptr<VfsFile> file;
41 size_t offset;
42 size_t size;
43 std::string name;
44};
45
46} // namespace FileSys
diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp
new file mode 100644
index 000000000..8b95e8c72
--- /dev/null
+++ b/src/core/file_sys/vfs_real.cpp
@@ -0,0 +1,168 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/common_paths.h"
6#include "common/logging/log.h"
7#include "core/file_sys/vfs_real.h"
8
9namespace FileSys {
10
11static std::string PermissionsToCharArray(Mode perms) {
12 std::string out;
13 switch (perms) {
14 case Mode::Read:
15 out += "r";
16 break;
17 case Mode::Write:
18 out += "r+";
19 break;
20 case Mode::Append:
21 out += "a";
22 break;
23 }
24 return out + "b";
25}
26
27RealVfsFile::RealVfsFile(const std::string& path_, Mode perms_)
28 : backing(path_, PermissionsToCharArray(perms_).c_str()), path(path_),
29 parent_path(FileUtil::GetParentPath(path_)),
30 path_components(FileUtil::SplitPathComponents(path_)),
31 parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
32 perms(perms_) {}
33
34std::string RealVfsFile::GetName() const {
35 return path_components.back();
36}
37
38size_t RealVfsFile::GetSize() const {
39 return backing.GetSize();
40}
41
42bool RealVfsFile::Resize(size_t new_size) {
43 return backing.Resize(new_size);
44}
45
46std::shared_ptr<VfsDirectory> RealVfsFile::GetContainingDirectory() const {
47 return std::make_shared<RealVfsDirectory>(parent_path, perms);
48}
49
50bool RealVfsFile::IsWritable() const {
51 return perms == Mode::Append || perms == Mode::Write;
52}
53
54bool RealVfsFile::IsReadable() const {
55 return perms == Mode::Read || perms == Mode::Write;
56}
57
58size_t RealVfsFile::Read(u8* data, size_t length, size_t offset) const {
59 if (!backing.Seek(offset, SEEK_SET))
60 return 0;
61 return backing.ReadBytes(data, length);
62}
63
64size_t RealVfsFile::Write(const u8* data, size_t length, size_t offset) {
65 if (!backing.Seek(offset, SEEK_SET))
66 return 0;
67 return backing.WriteBytes(data, length);
68}
69
70bool RealVfsFile::Rename(const std::string& name) {
71 const auto out = FileUtil::Rename(GetName(), name);
72 path = parent_path + DIR_SEP + name;
73 path_components = parent_components;
74 path_components.push_back(name);
75 backing = FileUtil::IOFile(path, PermissionsToCharArray(perms).c_str());
76 return out;
77}
78
79RealVfsDirectory::RealVfsDirectory(const std::string& path_, Mode perms_)
80 : path(FileUtil::RemoveTrailingSlash(path_)), parent_path(FileUtil::GetParentPath(path)),
81 path_components(FileUtil::SplitPathComponents(path)),
82 parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
83 perms(perms_) {
84 if (!FileUtil::Exists(path) && (perms == Mode::Write || perms == Mode::Append))
85 FileUtil::CreateDir(path);
86 unsigned size;
87 if (perms != Mode::Append) {
88 FileUtil::ForeachDirectoryEntry(
89 &size, path,
90 [this](unsigned* entries_out, const std::string& directory,
91 const std::string& filename) {
92 std::string full_path = directory + DIR_SEP + filename;
93 if (FileUtil::IsDirectory(full_path))
94 subdirectories.emplace_back(
95 std::make_shared<RealVfsDirectory>(full_path, perms));
96 else
97 files.emplace_back(std::make_shared<RealVfsFile>(full_path, perms));
98 return true;
99 });
100 }
101}
102
103std::vector<std::shared_ptr<VfsFile>> RealVfsDirectory::GetFiles() const {
104 return std::vector<std::shared_ptr<VfsFile>>(files);
105}
106
107std::vector<std::shared_ptr<VfsDirectory>> RealVfsDirectory::GetSubdirectories() const {
108 return std::vector<std::shared_ptr<VfsDirectory>>(subdirectories);
109}
110
111bool RealVfsDirectory::IsWritable() const {
112 return perms == Mode::Write || perms == Mode::Append;
113}
114
115bool RealVfsDirectory::IsReadable() const {
116 return perms == Mode::Read || perms == Mode::Write;
117}
118
119std::string RealVfsDirectory::GetName() const {
120 return path_components.back();
121}
122
123std::shared_ptr<VfsDirectory> RealVfsDirectory::GetParentDirectory() const {
124 if (path_components.size() <= 1)
125 return nullptr;
126
127 return std::make_shared<RealVfsDirectory>(parent_path, perms);
128}
129
130std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateSubdirectory(const std::string& name) {
131 if (!FileUtil::CreateDir(path + DIR_SEP + name))
132 return nullptr;
133 subdirectories.emplace_back(std::make_shared<RealVfsDirectory>(path + DIR_SEP + name, perms));
134 return subdirectories.back();
135}
136
137std::shared_ptr<VfsFile> RealVfsDirectory::CreateFile(const std::string& name) {
138 if (!FileUtil::CreateEmptyFile(path + DIR_SEP + name))
139 return nullptr;
140 files.emplace_back(std::make_shared<RealVfsFile>(path + DIR_SEP + name, perms));
141 return files.back();
142}
143
144bool RealVfsDirectory::DeleteSubdirectory(const std::string& name) {
145 return FileUtil::DeleteDirRecursively(path + DIR_SEP + name);
146}
147
148bool RealVfsDirectory::DeleteFile(const std::string& name) {
149 return FileUtil::Delete(path + DIR_SEP + name);
150}
151
152bool RealVfsDirectory::Rename(const std::string& name) {
153 return FileUtil::Rename(path, parent_path + DIR_SEP + name);
154}
155
156bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
157 auto iter = std::find(files.begin(), files.end(), file);
158 if (iter == files.end())
159 return false;
160
161 files[iter - files.begin()] = files.back();
162 files.pop_back();
163
164 subdirectories.emplace_back(dir);
165
166 return true;
167}
168} // namespace FileSys
diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h
new file mode 100644
index 000000000..01717f485
--- /dev/null
+++ b/src/core/file_sys/vfs_real.h
@@ -0,0 +1,65 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/file_util.h"
8#include "core/file_sys/filesystem.h"
9#include "core/file_sys/vfs.h"
10
11namespace FileSys {
12
13// An implmentation of VfsFile that represents a file on the user's computer.
14struct RealVfsFile : public VfsFile {
15 RealVfsFile(const std::string& name, Mode perms = Mode::Read);
16
17 std::string GetName() const override;
18 size_t GetSize() const override;
19 bool Resize(size_t new_size) override;
20 std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
21 bool IsWritable() const override;
22 bool IsReadable() const override;
23 size_t Read(u8* data, size_t length, size_t offset) const override;
24 size_t Write(const u8* data, size_t length, size_t offset) override;
25 bool Rename(const std::string& name) override;
26
27private:
28 FileUtil::IOFile backing;
29 std::string path;
30 std::string parent_path;
31 std::vector<std::string> path_components;
32 std::vector<std::string> parent_components;
33 Mode perms;
34};
35
36// An implementation of VfsDirectory that represents a directory on the user's computer.
37struct RealVfsDirectory : public VfsDirectory {
38 RealVfsDirectory(const std::string& path, Mode perms);
39
40 std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
41 std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
42 bool IsWritable() const override;
43 bool IsReadable() const override;
44 std::string GetName() const override;
45 std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
46 std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) override;
47 std::shared_ptr<VfsFile> CreateFile(const std::string& name) override;
48 bool DeleteSubdirectory(const std::string& name) override;
49 bool DeleteFile(const std::string& name) override;
50 bool Rename(const std::string& name) override;
51
52protected:
53 bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
54
55private:
56 std::string path;
57 std::string parent_path;
58 std::vector<std::string> path_components;
59 std::vector<std::string> parent_components;
60 Mode perms;
61 std::vector<std::shared_ptr<VfsFile>> files;
62 std::vector<std::shared_ptr<VfsDirectory>> subdirectories;
63};
64
65} // namespace FileSys