From d9ae4c332d11466351dc231531bf2a4f424ae8c1 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Sun, 9 Feb 2020 21:48:42 +0800 Subject: [PATCH] layered_fs: Do not open all replacement files on load Instead open them when we want to read them. This is because the standard library has a limit on the number of opened files. --- src/core/file_sys/layered_fs.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/core/file_sys/layered_fs.cpp b/src/core/file_sys/layered_fs.cpp index 8cfe73846..a77ab38f1 100644 --- a/src/core/file_sys/layered_fs.cpp +++ b/src/core/file_sys/layered_fs.cpp @@ -18,7 +18,7 @@ namespace FileSys { struct FileRelocationInfo { int type; // 0 - none, 1 - replaced / created, 2 - patched, 3 - removed u64 original_offset; // Type 0. Offset is absolute - FileUtil::IOFile replace_file; // Type 1 + std::string replace_file_path; // Type 1 std::vector patched_file; // Type 2 u64 size; // Relocated file size }; @@ -175,14 +175,9 @@ void LayeredFS::LoadRelocations() { } auto* file = file_path_map.at(path); - file->relocation.replace_file = FileUtil::IOFile(directory + virtual_name, "rb"); - if (file->relocation.replace_file) { - file->relocation.type = 1; - file->relocation.size = file->relocation.replace_file.GetSize(); - LOG_INFO(Service_FS, "LayeredFS replacement file in use for {}", path); - } else { - LOG_ERROR(Service_FS, "Could not open replacement file for {}", path); - } + file->relocation.type = 1; + file->relocation.replace_file_path = directory + virtual_name; + LOG_INFO(Service_FS, "LayeredFS replacement file in use for {}", path); return true; }; @@ -524,8 +519,14 @@ std::size_t LayeredFS::ReadFile(std::size_t offset, std::size_t length, u8* buff romfs->ReadFile(relocation.original_offset + relative_offset, to_read, buffer + read_size); } else if (relocation.type == 1) { // replace - relocation.replace_file.Seek(relative_offset, SEEK_SET); - relocation.replace_file.ReadBytes(buffer + read_size, to_read); + FileUtil::IOFile replace_file(relocation.replace_file_path, "rb"); + if (replace_file) { + replace_file.Seek(relative_offset, SEEK_SET); + replace_file.ReadBytes(buffer + read_size, to_read); + } else { + LOG_ERROR(Service_FS, "Could not open replacement file for {}", + current->second->path); + } } else if (relocation.type == 2) { // patch std::memcpy(buffer + read_size, relocation.patched_file.data() + relative_offset, to_read);