summaryrefslogtreecommitdiff
path: root/src/core/file_sys
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/core/file_sys
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/core/file_sys')
-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
5 files changed, 27 insertions, 23 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 {