summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar condut2015-07-10 00:55:23 +0300
committerGravatar Yuri Kunde Schlesner2015-07-13 19:57:12 -0300
commitc385b7767d32eccabbfeaa12764310cfc3d113b9 (patch)
tree1868679b3e7c30c05eb76d5e1f09fd01c86e3e65 /src
parentMerge pull request #927 from yuriks/getopt-fix (diff)
downloadyuzu-c385b7767d32eccabbfeaa12764310cfc3d113b9.tar.gz
yuzu-c385b7767d32eccabbfeaa12764310cfc3d113b9.tar.xz
yuzu-c385b7767d32eccabbfeaa12764310cfc3d113b9.zip
FS: Stream RomFS from file instead of loading all of it to memory
Diffstat (limited to 'src')
-rw-r--r--src/core/file_sys/archive_romfs.cpp7
-rw-r--r--src/core/file_sys/archive_romfs.h4
-rw-r--r--src/core/file_sys/archive_savedatacheck.cpp11
-rw-r--r--src/core/file_sys/ivfc_archive.cpp13
-rw-r--r--src/core/file_sys/ivfc_archive.h15
-rw-r--r--src/core/loader/loader.cpp2
-rw-r--r--src/core/loader/loader.h7
-rw-r--r--src/core/loader/ncch.cpp13
-rw-r--r--src/core/loader/ncch.h7
9 files changed, 47 insertions, 32 deletions
diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp
index c1e45dfeb..b792b1c8c 100644
--- a/src/core/file_sys/archive_romfs.cpp
+++ b/src/core/file_sys/archive_romfs.cpp
@@ -17,16 +17,15 @@
17 17
18namespace FileSys { 18namespace FileSys {
19 19
20ArchiveFactory_RomFS::ArchiveFactory_RomFS(const Loader::AppLoader& app_loader) 20ArchiveFactory_RomFS::ArchiveFactory_RomFS(const Loader::AppLoader& app_loader) {
21 : romfs_data(std::make_shared<std::vector<u8>>()) {
22 // Load the RomFS from the app 21 // Load the RomFS from the app
23 if (Loader::ResultStatus::Success != app_loader.ReadRomFS(*romfs_data)) { 22 if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) {
24 LOG_ERROR(Service_FS, "Unable to read RomFS!"); 23 LOG_ERROR(Service_FS, "Unable to read RomFS!");
25 } 24 }
26} 25}
27 26
28ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_RomFS::Open(const Path& path) { 27ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_RomFS::Open(const Path& path) {
29 auto archive = Common::make_unique<IVFCArchive>(romfs_data); 28 auto archive = Common::make_unique<IVFCArchive>(romfs_file, data_offset, data_size);
30 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); 29 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
31} 30}
32 31
diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h
index c69ff91c3..0ef67c557 100644
--- a/src/core/file_sys/archive_romfs.h
+++ b/src/core/file_sys/archive_romfs.h
@@ -29,7 +29,9 @@ public:
29 ResultCode Format(const Path& path) override; 29 ResultCode Format(const Path& path) override;
30 30
31private: 31private:
32 std::shared_ptr<std::vector<u8>> romfs_data; 32 std::shared_ptr<FileUtil::IOFile> romfs_file;
33 u64 data_offset;
34 u64 data_size;
33}; 35};
34 36
35} // namespace FileSys 37} // namespace FileSys
diff --git a/src/core/file_sys/archive_savedatacheck.cpp b/src/core/file_sys/archive_savedatacheck.cpp
index dec838cae..ea1dfe2c7 100644
--- a/src/core/file_sys/archive_savedatacheck.cpp
+++ b/src/core/file_sys/archive_savedatacheck.cpp
@@ -37,17 +37,14 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveDataCheck::Open(co
37 auto vec = path.AsBinary(); 37 auto vec = path.AsBinary();
38 const u32* data = reinterpret_cast<u32*>(vec.data()); 38 const u32* data = reinterpret_cast<u32*>(vec.data());
39 std::string file_path = GetSaveDataCheckPath(mount_point, data[1], data[0]); 39 std::string file_path = GetSaveDataCheckPath(mount_point, data[1], data[0]);
40 FileUtil::IOFile file(file_path, "rb"); 40 auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb");
41 41
42 if (!file.IsOpen()) { 42 if (!file->IsOpen()) {
43 return ResultCode(-1); // TODO(Subv): Find the right error code 43 return ResultCode(-1); // TODO(Subv): Find the right error code
44 } 44 }
45 auto size = file.GetSize(); 45 auto size = file->GetSize();
46 auto raw_data = std::make_shared<std::vector<u8>>(size);
47 file.ReadBytes(raw_data->data(), size);
48 file.Close();
49 46
50 auto archive = Common::make_unique<IVFCArchive>(std::move(raw_data)); 47 auto archive = Common::make_unique<IVFCArchive>(file, 0, size);
51 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); 48 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
52} 49}
53 50
diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp
index c88b39bcd..2b88b1d5d 100644
--- a/src/core/file_sys/ivfc_archive.cpp
+++ b/src/core/file_sys/ivfc_archive.cpp
@@ -16,15 +16,12 @@
16 16
17namespace FileSys { 17namespace FileSys {
18 18
19IVFCArchive::IVFCArchive(std::shared_ptr<const std::vector<u8>> data) : data(data) {
20}
21
22std::string IVFCArchive::GetName() const { 19std::string IVFCArchive::GetName() const {
23 return "IVFC"; 20 return "IVFC";
24} 21}
25 22
26std::unique_ptr<FileBackend> IVFCArchive::OpenFile(const Path& path, const Mode mode) const { 23std::unique_ptr<FileBackend> IVFCArchive::OpenFile(const Path& path, const Mode mode) const {
27 return Common::make_unique<IVFCFile>(data); 24 return Common::make_unique<IVFCFile>(romfs_file, data_offset, data_size);
28} 25}
29 26
30bool IVFCArchive::DeleteFile(const Path& path) const { 27bool IVFCArchive::DeleteFile(const Path& path) const {
@@ -66,8 +63,10 @@ std::unique_ptr<DirectoryBackend> IVFCArchive::OpenDirectory(const Path& path) c
66 63
67size_t IVFCFile::Read(const u64 offset, const u32 length, u8* buffer) const { 64size_t IVFCFile::Read(const u64 offset, const u32 length, u8* buffer) const {
68 LOG_TRACE(Service_FS, "called offset=%llu, length=%d", offset, length); 65 LOG_TRACE(Service_FS, "called offset=%llu, length=%d", offset, length);
69 memcpy(buffer, data->data() + offset, length); 66 romfs_file->Seek(data_offset + offset, SEEK_SET);
70 return length; 67 u32 read_length = (u32)std::min((u64)length, data_size - offset);
68
69 return romfs_file->ReadBytes(buffer, read_length);
71} 70}
72 71
73size_t IVFCFile::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const { 72size_t IVFCFile::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const {
@@ -76,7 +75,7 @@ size_t IVFCFile::Write(const u64 offset, const u32 length, const u32 flush, cons
76} 75}
77 76
78size_t IVFCFile::GetSize() const { 77size_t IVFCFile::GetSize() const {
79 return sizeof(u8) * data->size(); 78 return data_size; // TODO: return value will overflow on 32-bit machines
80} 79}
81 80
82bool IVFCFile::SetSize(const u64 size) const { 81bool IVFCFile::SetSize(const u64 size) const {
diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h
index 1850b3b17..c25666223 100644
--- a/src/core/file_sys/ivfc_archive.h
+++ b/src/core/file_sys/ivfc_archive.h
@@ -10,6 +10,7 @@
10#include <vector> 10#include <vector>
11 11
12#include "common/common_types.h" 12#include "common/common_types.h"
13#include "common/file_util.h"
13 14
14#include "core/file_sys/archive_backend.h" 15#include "core/file_sys/archive_backend.h"
15#include "core/file_sys/directory_backend.h" 16#include "core/file_sys/directory_backend.h"
@@ -28,7 +29,8 @@ namespace FileSys {
28 */ 29 */
29class IVFCArchive : public ArchiveBackend { 30class IVFCArchive : public ArchiveBackend {
30public: 31public:
31 IVFCArchive(std::shared_ptr<const std::vector<u8>> data); 32 IVFCArchive(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
33 : romfs_file(file), data_offset(offset), data_size(size) {}
32 34
33 std::string GetName() const override; 35 std::string GetName() const override;
34 36
@@ -42,12 +44,15 @@ public:
42 std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override; 44 std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
43 45
44protected: 46protected:
45 std::shared_ptr<const std::vector<u8>> data; 47 std::shared_ptr<FileUtil::IOFile> romfs_file;
48 u64 data_offset;
49 u64 data_size;
46}; 50};
47 51
48class IVFCFile : public FileBackend { 52class IVFCFile : public FileBackend {
49public: 53public:
50 IVFCFile(std::shared_ptr<const std::vector<u8>> data) : data(data) {} 54 IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
55 : romfs_file(file), data_offset(offset), data_size(size) {}
51 56
52 bool Open() override { return true; } 57 bool Open() override { return true; }
53 size_t Read(const u64 offset, const u32 length, u8* buffer) const override; 58 size_t Read(const u64 offset, const u32 length, u8* buffer) const override;
@@ -58,7 +63,9 @@ public:
58 void Flush() const override { } 63 void Flush() const override { }
59 64
60private: 65private:
61 std::shared_ptr<const std::vector<u8>> data; 66 std::shared_ptr<FileUtil::IOFile> romfs_file;
67 u64 data_offset;
68 u64 data_size;
62}; 69};
63 70
64class IVFCDirectory : public DirectoryBackend { 71class IVFCDirectory : public DirectoryBackend {
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 2e450fce4..b6549daf2 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -124,7 +124,7 @@ ResultStatus LoadFile(const std::string& filename) {
124 case FileType::CXI: 124 case FileType::CXI:
125 case FileType::CCI: 125 case FileType::CCI:
126 { 126 {
127 AppLoader_NCCH app_loader(std::move(file)); 127 AppLoader_NCCH app_loader(std::move(file), filename);
128 128
129 // Load application and RomFS 129 // Load application and RomFS
130 if (ResultStatus::Success == app_loader.Load()) { 130 if (ResultStatus::Success == app_loader.Load()) {
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 52bbf35b8..ff298222b 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -99,10 +99,13 @@ public:
99 99
100 /** 100 /**
101 * Get the RomFS of the application 101 * Get the RomFS of the application
102 * @param buffer Reference to buffer to store data 102 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
103 * @param romfs_file The file containing the RomFS
104 * @param offset The offset the romfs begins on
105 * @param size The size of the romfs
103 * @return ResultStatus result of function 106 * @return ResultStatus result of function
104 */ 107 */
105 virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) const { 108 virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) const {
106 return ResultStatus::ErrorNotImplemented; 109 return ResultStatus::ErrorNotImplemented;
107 } 110 }
108 111
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 87603d198..2bf1a6a26 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -299,7 +299,7 @@ ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const {
299 return LoadSectionExeFS("logo", buffer); 299 return LoadSectionExeFS("logo", buffer);
300} 300}
301 301
302ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const { 302ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) const {
303 if (!file->IsOpen()) 303 if (!file->IsOpen())
304 return ResultStatus::Error; 304 return ResultStatus::Error;
305 305
@@ -311,12 +311,17 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
311 LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); 311 LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset);
312 LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); 312 LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size);
313 313
314 buffer.resize(romfs_size); 314 if (file->GetSize () < romfs_offset + romfs_size)
315 return ResultStatus::Error;
315 316
316 file->Seek(romfs_offset, SEEK_SET); 317 // We reopen the file, to allow its position to be independent from file's
317 if (file->ReadBytes(&buffer[0], romfs_size) != romfs_size) 318 romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb");
319 if (!romfs_file->IsOpen())
318 return ResultStatus::Error; 320 return ResultStatus::Error;
319 321
322 offset = romfs_offset;
323 size = romfs_size;
324
320 return ResultStatus::Success; 325 return ResultStatus::Success;
321 } 326 }
322 LOG_DEBUG(Loader, "NCCH has no RomFS"); 327 LOG_DEBUG(Loader, "NCCH has no RomFS");
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h
index 29e39d2c0..d180e77ed 100644
--- a/src/core/loader/ncch.h
+++ b/src/core/loader/ncch.h
@@ -163,7 +163,8 @@ namespace Loader {
163/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI) 163/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
164class AppLoader_NCCH final : public AppLoader { 164class AppLoader_NCCH final : public AppLoader {
165public: 165public:
166 AppLoader_NCCH(std::unique_ptr<FileUtil::IOFile>&& file) : AppLoader(std::move(file)) { } 166 AppLoader_NCCH(std::unique_ptr<FileUtil::IOFile>&& file, const std::string& filepath)
167 : AppLoader(std::move(file)), filepath(filepath) { }
167 168
168 /** 169 /**
169 * Returns the type of the file 170 * Returns the type of the file
@@ -211,7 +212,7 @@ public:
211 * @param buffer Reference to buffer to store data 212 * @param buffer Reference to buffer to store data
212 * @return ResultStatus result of function 213 * @return ResultStatus result of function
213 */ 214 */
214 ResultStatus ReadRomFS(std::vector<u8>& buffer) const override; 215 ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) const override;
215 216
216private: 217private:
217 218
@@ -244,6 +245,8 @@ private:
244 NCCH_Header ncch_header; 245 NCCH_Header ncch_header;
245 ExeFs_Header exefs_header; 246 ExeFs_Header exefs_header;
246 ExHeader_Header exheader_header; 247 ExHeader_Header exheader_header;
248
249 std::string filepath;
247}; 250};
248 251
249} // namespace Loader 252} // namespace Loader