FS: Stream RomFS from file instead of loading all of it to memory

This commit is contained in:
condut 2015-07-10 00:55:23 +03:00 committed by Yuri Kunde Schlesner
parent ecdfd0643a
commit c385b7767d
9 changed files with 48 additions and 33 deletions

View file

@ -17,16 +17,15 @@
namespace FileSys {
ArchiveFactory_RomFS::ArchiveFactory_RomFS(const Loader::AppLoader& app_loader)
: romfs_data(std::make_shared<std::vector<u8>>()) {
ArchiveFactory_RomFS::ArchiveFactory_RomFS(const Loader::AppLoader& app_loader) {
// Load the RomFS from the app
if (Loader::ResultStatus::Success != app_loader.ReadRomFS(*romfs_data)) {
if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) {
LOG_ERROR(Service_FS, "Unable to read RomFS!");
}
}
ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_RomFS::Open(const Path& path) {
auto archive = Common::make_unique<IVFCArchive>(romfs_data);
auto archive = Common::make_unique<IVFCArchive>(romfs_file, data_offset, data_size);
return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
}

View file

@ -29,7 +29,9 @@ public:
ResultCode Format(const Path& path) override;
private:
std::shared_ptr<std::vector<u8>> romfs_data;
std::shared_ptr<FileUtil::IOFile> romfs_file;
u64 data_offset;
u64 data_size;
};
} // namespace FileSys

View file

@ -37,17 +37,14 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveDataCheck::Open(co
auto vec = path.AsBinary();
const u32* data = reinterpret_cast<u32*>(vec.data());
std::string file_path = GetSaveDataCheckPath(mount_point, data[1], data[0]);
FileUtil::IOFile file(file_path, "rb");
auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb");
if (!file.IsOpen()) {
if (!file->IsOpen()) {
return ResultCode(-1); // TODO(Subv): Find the right error code
}
auto size = file.GetSize();
auto raw_data = std::make_shared<std::vector<u8>>(size);
file.ReadBytes(raw_data->data(), size);
file.Close();
auto size = file->GetSize();
auto archive = Common::make_unique<IVFCArchive>(std::move(raw_data));
auto archive = Common::make_unique<IVFCArchive>(file, 0, size);
return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
}

View file

@ -16,15 +16,12 @@
namespace FileSys {
IVFCArchive::IVFCArchive(std::shared_ptr<const std::vector<u8>> data) : data(data) {
}
std::string IVFCArchive::GetName() const {
return "IVFC";
}
std::unique_ptr<FileBackend> IVFCArchive::OpenFile(const Path& path, const Mode mode) const {
return Common::make_unique<IVFCFile>(data);
return Common::make_unique<IVFCFile>(romfs_file, data_offset, data_size);
}
bool IVFCArchive::DeleteFile(const Path& path) const {
@ -66,8 +63,10 @@ std::unique_ptr<DirectoryBackend> IVFCArchive::OpenDirectory(const Path& path) c
size_t IVFCFile::Read(const u64 offset, const u32 length, u8* buffer) const {
LOG_TRACE(Service_FS, "called offset=%llu, length=%d", offset, length);
memcpy(buffer, data->data() + offset, length);
return length;
romfs_file->Seek(data_offset + offset, SEEK_SET);
u32 read_length = (u32)std::min((u64)length, data_size - offset);
return romfs_file->ReadBytes(buffer, read_length);
}
size_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
}
size_t IVFCFile::GetSize() const {
return sizeof(u8) * data->size();
return data_size; // TODO: return value will overflow on 32-bit machines
}
bool IVFCFile::SetSize(const u64 size) const {

View file

@ -10,6 +10,7 @@
#include <vector>
#include "common/common_types.h"
#include "common/file_util.h"
#include "core/file_sys/archive_backend.h"
#include "core/file_sys/directory_backend.h"
@ -28,7 +29,8 @@ namespace FileSys {
*/
class IVFCArchive : public ArchiveBackend {
public:
IVFCArchive(std::shared_ptr<const std::vector<u8>> data);
IVFCArchive(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
: romfs_file(file), data_offset(offset), data_size(size) {}
std::string GetName() const override;
@ -42,12 +44,15 @@ public:
std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
protected:
std::shared_ptr<const std::vector<u8>> data;
std::shared_ptr<FileUtil::IOFile> romfs_file;
u64 data_offset;
u64 data_size;
};
class IVFCFile : public FileBackend {
public:
IVFCFile(std::shared_ptr<const std::vector<u8>> data) : data(data) {}
IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
: romfs_file(file), data_offset(offset), data_size(size) {}
bool Open() override { return true; }
size_t Read(const u64 offset, const u32 length, u8* buffer) const override;
@ -58,7 +63,9 @@ public:
void Flush() const override { }
private:
std::shared_ptr<const std::vector<u8>> data;
std::shared_ptr<FileUtil::IOFile> romfs_file;
u64 data_offset;
u64 data_size;
};
class IVFCDirectory : public DirectoryBackend {