From 5021b4a567fad9a258f2b0fc397912f499e4cb1d Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 28 Jul 2015 16:56:25 +0200 Subject: Filesystem: Replace FileInfo struct with interface GC/Wii filesystem internals shouldn't be exposed to other classes. This change isn't especially useful by itself, but it opens up the way for some neat stuff in the following commits. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 53 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 21 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index b809047679..32f93f5eb6 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -22,8 +22,17 @@ namespace DiscIO { +FileInfoGCWii::FileInfoGCWii(u64 name_offset, u64 offset, u64 file_size) + : m_NameOffset(name_offset), m_Offset(offset), m_FileSize(file_size) +{ +} + +FileInfoGCWii::~FileInfoGCWii() +{ +} + FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partition) - : FileSystem(_rVolume, partition), m_Initialized(false), m_Valid(false), m_offset_shift(0) + : FileSystem(_rVolume, partition), m_Initialized(false), m_Valid(false), m_offset_shift(0) { m_Valid = DetectFileSystem(); } @@ -38,10 +47,10 @@ u64 FileSystemGCWii::GetFileSize(const std::string& _rFullPath) if (!m_Initialized) InitFileSystem(); - const FileInfo* pFileInfo = FindFileInfo(_rFullPath); + const FileInfoGCWii* pFileInfo = FindFileInfo(_rFullPath); if (pFileInfo != nullptr && !pFileInfo->IsDirectory()) - return pFileInfo->m_FileSize; + return pFileInfo->GetSize(); return 0; } @@ -53,7 +62,8 @@ std::string FileSystemGCWii::GetFileName(u64 _Address) for (auto& fileInfo : m_FileInfoVector) { - if ((fileInfo.m_Offset <= _Address) && ((fileInfo.m_Offset + fileInfo.m_FileSize) > _Address)) + if ((fileInfo.GetOffset() <= _Address) && + ((fileInfo.GetOffset() + fileInfo.GetSize()) > _Address)) { return fileInfo.m_FullPath; } @@ -68,21 +78,21 @@ u64 FileSystemGCWii::ReadFile(const std::string& _rFullPath, u8* _pBuffer, u64 _ if (!m_Initialized) InitFileSystem(); - const FileInfo* pFileInfo = FindFileInfo(_rFullPath); + const FileInfoGCWii* pFileInfo = FindFileInfo(_rFullPath); if (pFileInfo == nullptr) return 0; - if (_OffsetInFile >= pFileInfo->m_FileSize) + if (_OffsetInFile >= pFileInfo->GetSize()) return 0; - u64 read_length = std::min(_MaxBufferSize, pFileInfo->m_FileSize - _OffsetInFile); + u64 read_length = std::min(_MaxBufferSize, pFileInfo->GetSize() - _OffsetInFile); DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64 " Size: %" PRIx64, - read_length, _OffsetInFile, _rFullPath.c_str(), pFileInfo->m_Offset, - pFileInfo->m_FileSize); + read_length, _OffsetInFile, _rFullPath.c_str(), pFileInfo->GetOffset(), + pFileInfo->GetSize()); - m_rVolume->Read(pFileInfo->m_Offset + _OffsetInFile, read_length, _pBuffer, m_partition); + m_rVolume->Read(pFileInfo->GetOffset() + _OffsetInFile, read_length, _pBuffer, m_partition); return read_length; } @@ -91,13 +101,13 @@ bool FileSystemGCWii::ExportFile(const std::string& _rFullPath, const std::strin if (!m_Initialized) InitFileSystem(); - const FileInfo* pFileInfo = FindFileInfo(_rFullPath); + const FileInfoGCWii* pFileInfo = FindFileInfo(_rFullPath); if (!pFileInfo) return false; - u64 remainingSize = pFileInfo->m_FileSize; - u64 fileOffset = pFileInfo->m_Offset; + u64 remainingSize = pFileInfo->GetSize(); + u64 fileOffset = pFileInfo->GetOffset(); File::IOFile f(_rExportFilename, "wb"); if (!f) @@ -225,7 +235,7 @@ std::string FileSystemGCWii::GetStringFromOffset(u64 _Offset) const return SHIFTJISToUTF8(data); } -const std::vector& FileSystemGCWii::GetFileList() +const std::vector& FileSystemGCWii::GetFileList() { if (!m_Initialized) InitFileSystem(); @@ -233,7 +243,7 @@ const std::vector& FileSystemGCWii::GetFileList() return m_FileInfoVector; } -const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& _rFullPath) +const FileInfoGCWii* FileSystemGCWii::FindFileInfo(const std::string& _rFullPath) { if (!m_Initialized) InitFileSystem(); @@ -279,7 +289,8 @@ void FileSystemGCWii::InitFileSystem() const std::optional root_size = m_rVolume->ReadSwapped(FSTOffset + 0x8, m_partition); if (!root_name_offset || !root_offset || !root_size) return; - FileInfo root = {*root_name_offset, static_cast(*root_offset) << m_offset_shift, *root_size}; + FileInfoGCWii root(*root_name_offset, static_cast(*root_offset) << m_offset_shift, + *root_size); if (!root.IsDirectory()) return; @@ -287,7 +298,7 @@ void FileSystemGCWii::InitFileSystem() // 12 bytes (the size of a file entry) times 10 * 1024 * 1024 is 120 MiB, // more than total RAM in a Wii. No file system should use anywhere near that much. static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 10 * 1024 * 1024; - if (root.m_FileSize > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT) + if (root.GetSize() > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT) { // Without this check, Dolphin can crash by trying to allocate too much // memory when loading the file systems of certain malformed disc images. @@ -300,8 +311,8 @@ void FileSystemGCWii::InitFileSystem() PanicAlert("Wtf?"); u64 NameTableOffset = FSTOffset; - m_FileInfoVector.reserve((size_t)root.m_FileSize); - for (u32 i = 0; i < root.m_FileSize; i++) + m_FileInfoVector.reserve((size_t)root.GetSize()); + for (u32 i = 0; i < root.GetSize(); i++) { const u64 read_offset = FSTOffset + (i * 0xC); const std::optional name_offset = m_rVolume->ReadSwapped(read_offset, m_partition); @@ -323,7 +334,7 @@ size_t FileSystemGCWii::BuildFilenames(const size_t _FirstIndex, const size_t _L while (CurrentIndex < _LastIndex) { - FileInfo& rFileInfo = m_FileInfoVector[CurrentIndex]; + FileInfoGCWii& rFileInfo = m_FileInfoVector[CurrentIndex]; u64 const uOffset = _NameTableOffset + (rFileInfo.m_NameOffset & 0xFFFFFF); std::string const offset_str{GetStringFromOffset(uOffset)}; bool const is_dir = rFileInfo.IsDirectory(); @@ -340,7 +351,7 @@ size_t FileSystemGCWii::BuildFilenames(const size_t _FirstIndex, const size_t _L } // check next index - CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t)rFileInfo.m_FileSize, + CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t)rFileInfo.GetSize(), rFileInfo.m_FullPath, _NameTableOffset); } -- cgit v1.2.3 From 07d3a39aeb06204e13cfdf1d5f7dbe4f3aa66f89 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Wed, 29 Jul 2015 16:42:29 +0200 Subject: Filesystem: Replace file info's full path with name Some callers (i.e. ISOProperties) don't want the full path, so giving them it is unnecessary. Those that do want it can use GetPathFromFSTOffset. Not storing full paths everywhere also saves a small bit of RAM and is necessary for a later commit. The code isn't especially pretty right now (callers need to use FST offsets...) but it'll become better later. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 112 ++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 52 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 32f93f5eb6..fc0e40f055 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -22,8 +22,8 @@ namespace DiscIO { -FileInfoGCWii::FileInfoGCWii(u64 name_offset, u64 offset, u64 file_size) - : m_NameOffset(name_offset), m_Offset(offset), m_FileSize(file_size) +FileInfoGCWii::FileInfoGCWii(u64 name_offset, u64 offset, u64 file_size, std::string name) + : m_NameOffset(name_offset), m_Offset(offset), m_FileSize(file_size), m_Name(name) { } @@ -55,23 +55,64 @@ u64 FileSystemGCWii::GetFileSize(const std::string& _rFullPath) return 0; } -std::string FileSystemGCWii::GetFileName(u64 _Address) +std::string FileSystemGCWii::GetPath(u64 _Address) { if (!m_Initialized) InitFileSystem(); - for (auto& fileInfo : m_FileInfoVector) + for (size_t i = 0; i < m_FileInfoVector.size(); ++i) { - if ((fileInfo.GetOffset() <= _Address) && - ((fileInfo.GetOffset() + fileInfo.GetSize()) > _Address)) + const FileInfoGCWii& file_info = m_FileInfoVector[i]; + if ((file_info.GetOffset() <= _Address) && + ((file_info.GetOffset() + file_info.GetSize()) > _Address)) { - return fileInfo.m_FullPath; + return GetPathFromFSTOffset(i); } } return ""; } +std::string FileSystemGCWii::GetPathFromFSTOffset(size_t file_info_offset) +{ + if (!m_Initialized) + InitFileSystem(); + + // Root entry doesn't have a name + if (file_info_offset == 0) + return ""; + + const FileInfoGCWii& file_info = m_FileInfoVector[file_info_offset]; + if (file_info.IsDirectory()) + { + // The offset of the parent directory is stored in the current directory. + + if (file_info.GetOffset() >= file_info_offset) + { + // The offset of the parent directory is supposed to be smaller than + // the current offset. If an FST is malformed and breaks that rule, + // there's a risk that parent directory pointers form a loop. + // To avoid stack overflows, this method returns. + ERROR_LOG(DISCIO, "Invalid parent offset in file system"); + return ""; + } + return GetPathFromFSTOffset(file_info.GetOffset()) + file_info.GetName() + "/"; + } + else + { + // The parent directory can be found by searching backwards + // for a directory that contains this file. + + size_t parent_offset = file_info_offset - 1; + while (!(m_FileInfoVector[parent_offset].IsDirectory() && + m_FileInfoVector[parent_offset].GetSize() > file_info_offset)) + { + parent_offset--; + } + return GetPathFromFSTOffset(parent_offset) + file_info.GetName(); + } +} + u64 FileSystemGCWii::ReadFile(const std::string& _rFullPath, u8* _pBuffer, u64 _MaxBufferSize, u64 _OffsetInFile) { @@ -248,10 +289,10 @@ const FileInfoGCWii* FileSystemGCWii::FindFileInfo(const std::string& _rFullPath if (!m_Initialized) InitFileSystem(); - for (auto& fileInfo : m_FileInfoVector) + for (size_t i = 0; i < m_FileInfoVector.size(); ++i) { - if (!strcasecmp(fileInfo.m_FullPath.c_str(), _rFullPath.c_str())) - return &fileInfo; + if (!strcasecmp(GetPathFromFSTOffset(i).c_str(), _rFullPath.c_str())) + return &m_FileInfoVector[i]; } return nullptr; @@ -290,7 +331,7 @@ void FileSystemGCWii::InitFileSystem() if (!root_name_offset || !root_offset || !root_size) return; FileInfoGCWii root(*root_name_offset, static_cast(*root_offset) << m_offset_shift, - *root_size); + *root_size, ""); if (!root.IsDirectory()) return; @@ -309,53 +350,20 @@ void FileSystemGCWii::InitFileSystem() if (m_FileInfoVector.size()) PanicAlert("Wtf?"); - u64 NameTableOffset = FSTOffset; + u64 NameTableOffset = FSTOffset + (root.GetSize() * 0xC); m_FileInfoVector.reserve((size_t)root.GetSize()); for (u32 i = 0; i < root.GetSize(); i++) { const u64 read_offset = FSTOffset + (i * 0xC); - const std::optional name_offset = m_rVolume->ReadSwapped(read_offset, m_partition); - const std::optional offset = m_rVolume->ReadSwapped(read_offset + 0x4, m_partition); - const std::optional size = m_rVolume->ReadSwapped(read_offset + 0x8, m_partition); - m_FileInfoVector.emplace_back(name_offset.value_or(0), - static_cast(offset.value_or(0)) << m_offset_shift, - size.value_or(0)); - NameTableOffset += 0xC; + const u32 name_offset = m_rVolume->ReadSwapped(read_offset, m_partition).value_or(0); + const u32 offset = m_rVolume->ReadSwapped(read_offset + 0x4, m_partition).value_or(0); + const u32 size = m_rVolume->ReadSwapped(read_offset + 0x8, m_partition).value_or(0); + const std::string name = GetStringFromOffset(NameTableOffset + (name_offset & 0xFFFFFF)); + m_FileInfoVector.emplace_back( + name_offset, static_cast(offset) << (m_offset_shift * !(name_offset & 0xFF000000)), + size, name); } - - BuildFilenames(1, m_FileInfoVector.size(), "", NameTableOffset); -} - -size_t FileSystemGCWii::BuildFilenames(const size_t _FirstIndex, const size_t _LastIndex, - const std::string& _szDirectory, u64 _NameTableOffset) -{ - size_t CurrentIndex = _FirstIndex; - - while (CurrentIndex < _LastIndex) - { - FileInfoGCWii& rFileInfo = m_FileInfoVector[CurrentIndex]; - u64 const uOffset = _NameTableOffset + (rFileInfo.m_NameOffset & 0xFFFFFF); - std::string const offset_str{GetStringFromOffset(uOffset)}; - bool const is_dir = rFileInfo.IsDirectory(); - rFileInfo.m_FullPath.reserve(_szDirectory.size() + offset_str.size()); - - rFileInfo.m_FullPath.append(_szDirectory.data(), _szDirectory.size()) - .append(offset_str.data(), offset_str.size()) - .append("/", size_t(is_dir)); - - if (!is_dir) - { - ++CurrentIndex; - continue; - } - - // check next index - CurrentIndex = BuildFilenames(CurrentIndex + 1, (size_t)rFileInfo.GetSize(), - rFileInfo.m_FullPath, _NameTableOffset); - } - - return CurrentIndex; } } // namespace -- cgit v1.2.3 From 3d5ef948d0d3984e4ef7c26fe56acf469868e0f3 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Wed, 29 Jul 2015 17:27:43 +0200 Subject: Filesystem: Make FindFileInfo public --- Source/Core/DiscIO/FileSystemGCWii.cpp | 50 +++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index fc0e40f055..f8ddab28fe 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -42,12 +42,34 @@ FileSystemGCWii::~FileSystemGCWii() m_FileInfoVector.clear(); } +const std::vector& FileSystemGCWii::GetFileList() +{ + if (!m_Initialized) + InitFileSystem(); + + return m_FileInfoVector; +} + +const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& _rFullPath) +{ + if (!m_Initialized) + InitFileSystem(); + + for (size_t i = 0; i < m_FileInfoVector.size(); ++i) + { + if (!strcasecmp(GetPathFromFSTOffset(i).c_str(), _rFullPath.c_str())) + return &m_FileInfoVector[i]; + } + + return nullptr; +} + u64 FileSystemGCWii::GetFileSize(const std::string& _rFullPath) { if (!m_Initialized) InitFileSystem(); - const FileInfoGCWii* pFileInfo = FindFileInfo(_rFullPath); + const FileInfo* pFileInfo = FindFileInfo(_rFullPath); if (pFileInfo != nullptr && !pFileInfo->IsDirectory()) return pFileInfo->GetSize(); @@ -119,7 +141,7 @@ u64 FileSystemGCWii::ReadFile(const std::string& _rFullPath, u8* _pBuffer, u64 _ if (!m_Initialized) InitFileSystem(); - const FileInfoGCWii* pFileInfo = FindFileInfo(_rFullPath); + const FileInfo* pFileInfo = FindFileInfo(_rFullPath); if (pFileInfo == nullptr) return 0; @@ -142,7 +164,7 @@ bool FileSystemGCWii::ExportFile(const std::string& _rFullPath, const std::strin if (!m_Initialized) InitFileSystem(); - const FileInfoGCWii* pFileInfo = FindFileInfo(_rFullPath); + const FileInfo* pFileInfo = FindFileInfo(_rFullPath); if (!pFileInfo) return false; @@ -276,28 +298,6 @@ std::string FileSystemGCWii::GetStringFromOffset(u64 _Offset) const return SHIFTJISToUTF8(data); } -const std::vector& FileSystemGCWii::GetFileList() -{ - if (!m_Initialized) - InitFileSystem(); - - return m_FileInfoVector; -} - -const FileInfoGCWii* FileSystemGCWii::FindFileInfo(const std::string& _rFullPath) -{ - if (!m_Initialized) - InitFileSystem(); - - for (size_t i = 0; i < m_FileInfoVector.size(); ++i) - { - if (!strcasecmp(GetPathFromFSTOffset(i).c_str(), _rFullPath.c_str())) - return &m_FileInfoVector[i]; - } - - return nullptr; -} - bool FileSystemGCWii::DetectFileSystem() { if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) -- cgit v1.2.3 From 7c45afecb2a157088591a94f8c008d74419243d1 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 30 Jul 2015 15:06:23 +0200 Subject: Filesystem: Use file info in arguments instead of path Some callers already have the file info, making the relatively slow FindFileInfo calls unnecessary. Callers that didn't have the file info will now need to call FindFileInfo on their own. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 45 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index f8ddab28fe..d799caf435 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -50,31 +50,35 @@ const std::vector& FileSystemGCWii::GetFileList() return m_FileInfoVector; } -const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& _rFullPath) +const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path) { if (!m_Initialized) InitFileSystem(); for (size_t i = 0; i < m_FileInfoVector.size(); ++i) { - if (!strcasecmp(GetPathFromFSTOffset(i).c_str(), _rFullPath.c_str())) + if (!strcasecmp(GetPathFromFSTOffset(i).c_str(), path.c_str())) return &m_FileInfoVector[i]; } return nullptr; } -u64 FileSystemGCWii::GetFileSize(const std::string& _rFullPath) +const FileInfo* FileSystemGCWii::FindFileInfo(u64 disc_offset) { if (!m_Initialized) InitFileSystem(); - const FileInfo* pFileInfo = FindFileInfo(_rFullPath); - - if (pFileInfo != nullptr && !pFileInfo->IsDirectory()) - return pFileInfo->GetSize(); + for (auto& file_info : m_FileInfoVector) + { + if ((file_info.GetOffset() <= disc_offset) && + ((file_info.GetOffset() + file_info.GetSize()) > disc_offset)) + { + return &file_info; + } + } - return 0; + return nullptr; } std::string FileSystemGCWii::GetPath(u64 _Address) @@ -135,42 +139,39 @@ std::string FileSystemGCWii::GetPathFromFSTOffset(size_t file_info_offset) } } -u64 FileSystemGCWii::ReadFile(const std::string& _rFullPath, u8* _pBuffer, u64 _MaxBufferSize, +u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxBufferSize, u64 _OffsetInFile) { if (!m_Initialized) InitFileSystem(); - const FileInfo* pFileInfo = FindFileInfo(_rFullPath); - if (pFileInfo == nullptr) + if (!file_info || file_info->IsDirectory()) return 0; - if (_OffsetInFile >= pFileInfo->GetSize()) + if (_OffsetInFile >= file_info->GetSize()) return 0; - u64 read_length = std::min(_MaxBufferSize, pFileInfo->GetSize() - _OffsetInFile); + u64 read_length = std::min(_MaxBufferSize, file_info->GetSize() - _OffsetInFile); DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64 " Size: %" PRIx64, - read_length, _OffsetInFile, _rFullPath.c_str(), pFileInfo->GetOffset(), - pFileInfo->GetSize()); + read_length, _OffsetInFile, GetPath(file_info->GetOffset()).c_str(), + file_info->GetOffset(), file_info->GetSize()); - m_rVolume->Read(pFileInfo->GetOffset() + _OffsetInFile, read_length, _pBuffer, m_partition); + m_rVolume->Read(file_info->GetOffset() + _OffsetInFile, read_length, _pBuffer, m_partition); return read_length; } -bool FileSystemGCWii::ExportFile(const std::string& _rFullPath, const std::string& _rExportFilename) +bool FileSystemGCWii::ExportFile(const FileInfo* file_info, const std::string& _rExportFilename) { if (!m_Initialized) InitFileSystem(); - const FileInfo* pFileInfo = FindFileInfo(_rFullPath); - - if (!pFileInfo) + if (!file_info || file_info->IsDirectory()) return false; - u64 remainingSize = pFileInfo->GetSize(); - u64 fileOffset = pFileInfo->GetOffset(); + u64 remainingSize = file_info->GetSize(); + u64 fileOffset = file_info->GetOffset(); File::IOFile f(_rExportFilename, "wb"); if (!f) -- cgit v1.2.3 From f49b64caff2ebfeeaa80bc97d85de8b22f56e588 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 30 Jul 2015 17:12:44 +0200 Subject: Filesystem: Rewrite finding file info by path for performance Instead of calling GetPathFromFSTOffset for every file info, FindFileInfo now only looks at names in directories that are included in the path. For the common case of searching for "opening.bnr", this means that only root-level files and directories have to be searched through. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 61 ++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index d799caf435..5f3dd425ab 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -55,10 +55,65 @@ const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path) if (!m_Initialized) InitFileSystem(); - for (size_t i = 0; i < m_FileInfoVector.size(); ++i) + return FindFileInfo(path, 0); +} + +const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path, + size_t search_start_offset) const +{ + // Given a path like "directory1/directory2/fileA.bin", this function will + // find directory1 and then call itself to search for "directory2/fileA.bin". + + if (path.empty() || path == "/") + return &m_FileInfoVector[search_start_offset]; + + // It's only possible to search in directories. Searching in a file is an error + if (!m_FileInfoVector[search_start_offset].IsDirectory()) + return nullptr; + + size_t first_dir_separator = path.find('/'); + const std::string searching_for = path.substr(0, first_dir_separator); + const std::string rest_of_path = + (first_dir_separator != std::string::npos) ? path.substr(first_dir_separator + 1) : ""; + + size_t search_end_offset = m_FileInfoVector[search_start_offset].GetSize(); + search_start_offset++; + while (search_start_offset < search_end_offset) { - if (!strcasecmp(GetPathFromFSTOffset(i).c_str(), path.c_str())) - return &m_FileInfoVector[i]; + const FileInfoGCWii& file_info = m_FileInfoVector[search_start_offset]; + + if (file_info.GetName() == searching_for) + { + // A match is found. The rest of the path is passed on to finish the search. + const FileInfo* result = FindFileInfo(rest_of_path, search_start_offset); + + // If the search wasn't successful, the loop continues, just in case there's a second + // file info that matches searching_for (which probably won't happen in practice) + if (result) + return result; + } + + if (file_info.IsDirectory()) + { + // Skip a directory and everything that it contains + + if (file_info.GetSize() <= search_start_offset) + { + // The next offset (obtained by GetSize) is supposed to be larger than + // the current offset. If an FST is malformed and breaks that rule, + // there's a risk that next offset pointers form a loop. + // To avoid infinite loops, this method returns. + ERROR_LOG(DISCIO, "Invalid next offset in file system"); + return nullptr; + } + + search_start_offset = file_info.GetSize(); + } + else + { + // Skip a single file + search_start_offset++; + } } return nullptr; -- cgit v1.2.3 From d6ee7ec32c22871ea05a3fc383ceb8139c9227f9 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 30 Jul 2015 22:18:20 +0200 Subject: Filesystem: Read the entire FST in one go Instead of using lots of small scattered reads to read the FST, only one big read is used, which is more efficient. This also means that the FST only allocates memory once and stores all strings close to each other - good for the CPU cache. The file info objects use pointers to this FST memory of containing data themselves. Keeping around the big m_FileInfoVector containing objects with only pointers is a bit unnecessary, but that will be fixed soon. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 107 ++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 49 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 5f3dd425ab..a6e7ee0cd1 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -22,8 +22,8 @@ namespace DiscIO { -FileInfoGCWii::FileInfoGCWii(u64 name_offset, u64 offset, u64 file_size, std::string name) - : m_NameOffset(name_offset), m_Offset(offset), m_FileSize(file_size), m_Name(name) +FileInfoGCWii::FileInfoGCWii(u8 offset_shift, const u8* fst_entry, const u8* name_table_start) + : m_offset_shift(offset_shift), m_fst_entry(fst_entry), m_name_table_start(name_table_start) { } @@ -31,8 +31,36 @@ FileInfoGCWii::~FileInfoGCWii() { } +u32 FileInfoGCWii::Get(EntryProperty entry_property) const +{ + return Common::swap32(m_fst_entry + sizeof(u32) * static_cast(entry_property)); +} + +u32 FileInfoGCWii::GetSize() const +{ + return Get(EntryProperty::FILE_SIZE); +} + +u64 FileInfoGCWii::GetOffset() const +{ + return static_cast(Get(EntryProperty::FILE_OFFSET)) << (IsDirectory() ? 0 : m_offset_shift); +} + +bool FileInfoGCWii::IsDirectory() const +{ + return (Get(EntryProperty::NAME_OFFSET) & 0xFF000000) != 0; +} + +std::string FileInfoGCWii::GetName() const +{ + // TODO: Should we really always use SHIFT-JIS? + // Some names in Pikmin (NTSC-U) don't make sense without it, but is it correct? + const u8* name = m_name_table_start + (Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF); + return SHIFTJISToUTF8(reinterpret_cast(name)); +} + FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partition) - : FileSystem(_rVolume, partition), m_Initialized(false), m_Valid(false), m_offset_shift(0) + : FileSystem(_rVolume, partition), m_Initialized(false), m_Valid(false), m_offset_shift(0) { m_Valid = DetectFileSystem(); } @@ -55,6 +83,9 @@ const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path) if (!m_Initialized) InitFileSystem(); + if (m_FileInfoVector.empty()) + return nullptr; + return FindFileInfo(path, 0); } @@ -209,7 +240,7 @@ u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxB u64 read_length = std::min(_MaxBufferSize, file_info->GetSize() - _OffsetInFile); DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64 - " Size: %" PRIx64, + " Size: %" PRIx32, read_length, _OffsetInFile, GetPath(file_info->GetOffset()).c_str(), file_info->GetOffset(), file_info->GetSize()); @@ -343,17 +374,6 @@ bool FileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const return false; } -std::string FileSystemGCWii::GetStringFromOffset(u64 _Offset) const -{ - std::string data(255, 0x00); - m_rVolume->Read(_Offset, data.size(), (u8*)&data[0], m_partition); - data.erase(std::find(data.begin(), data.end(), 0x00), data.end()); - - // TODO: Should we really always use SHIFT-JIS? - // It makes some filenames in Pikmin (NTSC-U) sane, but is it correct? - return SHIFTJISToUTF8(data); -} - bool FileSystemGCWii::DetectFileSystem() { if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) @@ -374,52 +394,41 @@ void FileSystemGCWii::InitFileSystem() { m_Initialized = true; - // read the whole FST const std::optional fst_offset_unshifted = m_rVolume->ReadSwapped(0x424, m_partition); - if (!fst_offset_unshifted) + const std::optional fst_size_unshifted = m_rVolume->ReadSwapped(0x428, m_partition); + if (!fst_offset_unshifted || !fst_size_unshifted) return; - const u64 FSTOffset = static_cast(*fst_offset_unshifted) << m_offset_shift; - - // read all fileinfos - const std::optional root_name_offset = m_rVolume->ReadSwapped(FSTOffset, m_partition); - const std::optional root_offset = m_rVolume->ReadSwapped(FSTOffset + 0x4, m_partition); - const std::optional root_size = m_rVolume->ReadSwapped(FSTOffset + 0x8, m_partition); - if (!root_name_offset || !root_offset || !root_size) - return; - FileInfoGCWii root(*root_name_offset, static_cast(*root_offset) << m_offset_shift, - *root_size, ""); - - if (!root.IsDirectory()) + const u64 fst_offset = static_cast(*fst_offset_unshifted) << m_offset_shift; + const u64 fst_size = static_cast(*fst_size_unshifted) << m_offset_shift; + if (fst_size < 0xC) return; - // 12 bytes (the size of a file entry) times 10 * 1024 * 1024 is 120 MiB, - // more than total RAM in a Wii. No file system should use anywhere near that much. - static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 10 * 1024 * 1024; - if (root.GetSize() > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT) + // 128 MiB is more than the total amount of RAM in a Wii. + // No file system should use anywhere near that much. + static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 128 * 1024 * 1024; + if (fst_size > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT) { // Without this check, Dolphin can crash by trying to allocate too much - // memory when loading the file systems of certain malformed disc images. + // memory when loading a disc image with an incorrect FST size. ERROR_LOG(DISCIO, "File system is abnormally large! Aborting loading"); return; } - if (m_FileInfoVector.size()) - PanicAlert("Wtf?"); - u64 NameTableOffset = FSTOffset + (root.GetSize() * 0xC); + // Read the whole FST + m_file_system_table.resize(fst_size); + if (!m_rVolume->Read(fst_offset, fst_size, m_file_system_table.data(), m_partition)) + return; - m_FileInfoVector.reserve((size_t)root.GetSize()); - for (u32 i = 0; i < root.GetSize(); i++) - { - const u64 read_offset = FSTOffset + (i * 0xC); - const u32 name_offset = m_rVolume->ReadSwapped(read_offset, m_partition).value_or(0); - const u32 offset = m_rVolume->ReadSwapped(read_offset + 0x4, m_partition).value_or(0); - const u32 size = m_rVolume->ReadSwapped(read_offset + 0x8, m_partition).value_or(0); - const std::string name = GetStringFromOffset(NameTableOffset + (name_offset & 0xFFFFFF)); - m_FileInfoVector.emplace_back( - name_offset, static_cast(offset) << (m_offset_shift * !(name_offset & 0xFF000000)), - size, name); - } + // Create all file info objects + u32 number_of_file_infos = Common::swap32(*((u32*)m_file_system_table.data() + 2)); + const u8* fst_start = m_file_system_table.data(); + const u8* name_table_start = fst_start + (number_of_file_infos * 0xC); + const u8* name_table_end = fst_start + fst_size; + if (name_table_end < name_table_start) + return; + for (u32 i = 0; i < number_of_file_infos; i++) + m_FileInfoVector.emplace_back(m_offset_shift, fst_start + (i * 0xC), name_table_start); } } // namespace -- cgit v1.2.3 From afe2bc60f634cf1d417680cefc51b3caeb3cfbf2 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Fri, 31 Jul 2015 13:36:28 +0200 Subject: Filesystem: Initialize everything in constructor Not initializing until the filesystem is used is good when a filesystem is constructed and then never used, but nobody does that. This simplifies the code a little and lets all methods be const. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 146 +++++++++++++-------------------- 1 file changed, 59 insertions(+), 87 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index a6e7ee0cd1..813201b7a0 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -60,9 +60,58 @@ std::string FileInfoGCWii::GetName() const } FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partition) - : FileSystem(_rVolume, partition), m_Initialized(false), m_Valid(false), m_offset_shift(0) + : FileSystem(_rVolume, partition), m_Valid(false), m_offset_shift(0) { - m_Valid = DetectFileSystem(); + // Check if this is a GameCube or Wii disc + if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) + { + m_offset_shift = 2; // Wii file system + m_Valid = true; + } + else if (m_rVolume->ReadSwapped(0x1c, m_partition) == u32(0xC2339F3D)) + { + m_offset_shift = 0; // GameCube file system + m_Valid = true; + } + + if (!m_Valid) + return; + + const std::optional fst_offset_unshifted = m_rVolume->ReadSwapped(0x424, m_partition); + const std::optional fst_size_unshifted = m_rVolume->ReadSwapped(0x428, m_partition); + if (!fst_offset_unshifted || !fst_size_unshifted) + return; + const u64 fst_offset = static_cast(*fst_offset_unshifted) << m_offset_shift; + const u64 fst_size = static_cast(*fst_size_unshifted) << m_offset_shift; + if (fst_size < 0xC) + return; + + // 128 MiB is more than the total amount of RAM in a Wii. + // No file system should use anywhere near that much. + static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 128 * 1024 * 1024; + if (fst_size > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT) + { + // Without this check, Dolphin can crash by trying to allocate too much + // memory when loading a disc image with an incorrect FST size. + + ERROR_LOG(DISCIO, "File system is abnormally large! Aborting loading"); + return; + } + + // Read the whole FST + m_file_system_table.resize(fst_size); + if (!m_rVolume->Read(fst_offset, fst_size, m_file_system_table.data(), m_partition)) + return; + + // Create all file info objects + u32 number_of_file_infos = Common::swap32(*((u32*)m_file_system_table.data() + 2)); + const u8* fst_start = m_file_system_table.data(); + const u8* name_table_start = fst_start + (number_of_file_infos * 0xC); + const u8* name_table_end = fst_start + fst_size; + if (name_table_end < name_table_start) + return; + for (u32 i = 0; i < number_of_file_infos; i++) + m_FileInfoVector.emplace_back(m_offset_shift, fst_start + (i * 0xC), name_table_start); } FileSystemGCWii::~FileSystemGCWii() @@ -70,19 +119,13 @@ FileSystemGCWii::~FileSystemGCWii() m_FileInfoVector.clear(); } -const std::vector& FileSystemGCWii::GetFileList() +const std::vector& FileSystemGCWii::GetFileList() const { - if (!m_Initialized) - InitFileSystem(); - return m_FileInfoVector; } -const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path) +const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path) const { - if (!m_Initialized) - InitFileSystem(); - if (m_FileInfoVector.empty()) return nullptr; @@ -150,11 +193,8 @@ const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path, return nullptr; } -const FileInfo* FileSystemGCWii::FindFileInfo(u64 disc_offset) +const FileInfo* FileSystemGCWii::FindFileInfo(u64 disc_offset) const { - if (!m_Initialized) - InitFileSystem(); - for (auto& file_info : m_FileInfoVector) { if ((file_info.GetOffset() <= disc_offset) && @@ -167,11 +207,8 @@ const FileInfo* FileSystemGCWii::FindFileInfo(u64 disc_offset) return nullptr; } -std::string FileSystemGCWii::GetPath(u64 _Address) +std::string FileSystemGCWii::GetPath(u64 _Address) const { - if (!m_Initialized) - InitFileSystem(); - for (size_t i = 0; i < m_FileInfoVector.size(); ++i) { const FileInfoGCWii& file_info = m_FileInfoVector[i]; @@ -185,11 +222,8 @@ std::string FileSystemGCWii::GetPath(u64 _Address) return ""; } -std::string FileSystemGCWii::GetPathFromFSTOffset(size_t file_info_offset) +std::string FileSystemGCWii::GetPathFromFSTOffset(size_t file_info_offset) const { - if (!m_Initialized) - InitFileSystem(); - // Root entry doesn't have a name if (file_info_offset == 0) return ""; @@ -226,11 +260,8 @@ std::string FileSystemGCWii::GetPathFromFSTOffset(size_t file_info_offset) } u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxBufferSize, - u64 _OffsetInFile) + u64 _OffsetInFile) const { - if (!m_Initialized) - InitFileSystem(); - if (!file_info || file_info->IsDirectory()) return 0; @@ -248,11 +279,9 @@ u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxB return read_length; } -bool FileSystemGCWii::ExportFile(const FileInfo* file_info, const std::string& _rExportFilename) +bool FileSystemGCWii::ExportFile(const FileInfo* file_info, + const std::string& _rExportFilename) const { - if (!m_Initialized) - InitFileSystem(); - if (!file_info || file_info->IsDirectory()) return false; @@ -374,61 +403,4 @@ bool FileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const return false; } -bool FileSystemGCWii::DetectFileSystem() -{ - if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) - { - m_offset_shift = 2; // Wii file system - return true; - } - else if (m_rVolume->ReadSwapped(0x1c, m_partition) == u32(0xC2339F3D)) - { - m_offset_shift = 0; // GameCube file system - return true; - } - - return false; -} - -void FileSystemGCWii::InitFileSystem() -{ - m_Initialized = true; - - const std::optional fst_offset_unshifted = m_rVolume->ReadSwapped(0x424, m_partition); - const std::optional fst_size_unshifted = m_rVolume->ReadSwapped(0x428, m_partition); - if (!fst_offset_unshifted || !fst_size_unshifted) - return; - const u64 fst_offset = static_cast(*fst_offset_unshifted) << m_offset_shift; - const u64 fst_size = static_cast(*fst_size_unshifted) << m_offset_shift; - if (fst_size < 0xC) - return; - - // 128 MiB is more than the total amount of RAM in a Wii. - // No file system should use anywhere near that much. - static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 128 * 1024 * 1024; - if (fst_size > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT) - { - // Without this check, Dolphin can crash by trying to allocate too much - // memory when loading a disc image with an incorrect FST size. - - ERROR_LOG(DISCIO, "File system is abnormally large! Aborting loading"); - return; - } - - // Read the whole FST - m_file_system_table.resize(fst_size); - if (!m_rVolume->Read(fst_offset, fst_size, m_file_system_table.data(), m_partition)) - return; - - // Create all file info objects - u32 number_of_file_infos = Common::swap32(*((u32*)m_file_system_table.data() + 2)); - const u8* fst_start = m_file_system_table.data(); - const u8* name_table_start = fst_start + (number_of_file_infos * 0xC); - const u8* name_table_end = fst_start + fst_size; - if (name_table_end < name_table_start) - return; - for (u32 i = 0; i < number_of_file_infos; i++) - m_FileInfoVector.emplace_back(m_offset_shift, fst_start + (i * 0xC), name_table_start); -} - } // namespace -- cgit v1.2.3 From 1262f08ac119e5d12ec48688f66e8b12e52b6d29 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Fri, 31 Jul 2015 13:56:29 +0200 Subject: Filesystem: Better validity checking Now that the FST in read in the constructor, m_Valid can be set to false when there are errors in the FST. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 813201b7a0..8e8254e21c 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -64,17 +64,10 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit { // Check if this is a GameCube or Wii disc if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) - { m_offset_shift = 2; // Wii file system - m_Valid = true; - } else if (m_rVolume->ReadSwapped(0x1c, m_partition) == u32(0xC2339F3D)) - { m_offset_shift = 0; // GameCube file system - m_Valid = true; - } - - if (!m_Valid) + else return; const std::optional fst_offset_unshifted = m_rVolume->ReadSwapped(0x424, m_partition); @@ -112,6 +105,9 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit return; for (u32 i = 0; i < number_of_file_infos; i++) m_FileInfoVector.emplace_back(m_offset_shift, fst_start + (i * 0xC), name_table_start); + + // If we haven't returned yet, everything succeeded + m_Valid = true; } FileSystemGCWii::~FileSystemGCWii() -- cgit v1.2.3 From ee2b88ebb627869a6153fe3135df232793f02e87 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 2 Aug 2015 17:39:03 +0200 Subject: Filesystem: Store pointer to beginning of FST in file infos Needed for the next commit. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 8e8254e21c..9280dbab16 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -22,8 +22,10 @@ namespace DiscIO { -FileInfoGCWii::FileInfoGCWii(u8 offset_shift, const u8* fst_entry, const u8* name_table_start) - : m_offset_shift(offset_shift), m_fst_entry(fst_entry), m_name_table_start(name_table_start) +constexpr u32 FST_ENTRY_SIZE = 4 * 3; // An FST entry consists of three 32-bit integers + +FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift, u32 index, u32 total_file_infos) + : m_fst(fst), m_offset_shift(offset_shift), m_index(index), m_total_file_infos(total_file_infos) { } @@ -33,7 +35,8 @@ FileInfoGCWii::~FileInfoGCWii() u32 FileInfoGCWii::Get(EntryProperty entry_property) const { - return Common::swap32(m_fst_entry + sizeof(u32) * static_cast(entry_property)); + return Common::swap32(m_fst + FST_ENTRY_SIZE * m_index + + sizeof(u32) * static_cast(entry_property)); } u32 FileInfoGCWii::GetSize() const @@ -55,7 +58,8 @@ std::string FileInfoGCWii::GetName() const { // TODO: Should we really always use SHIFT-JIS? // Some names in Pikmin (NTSC-U) don't make sense without it, but is it correct? - const u8* name = m_name_table_start + (Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF); + u32 name_offset = Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF; + const u8* name = m_fst + FST_ENTRY_SIZE * m_total_file_infos + name_offset; return SHIFTJISToUTF8(reinterpret_cast(name)); } @@ -76,7 +80,7 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit return; const u64 fst_offset = static_cast(*fst_offset_unshifted) << m_offset_shift; const u64 fst_size = static_cast(*fst_size_unshifted) << m_offset_shift; - if (fst_size < 0xC) + if (fst_size < FST_ENTRY_SIZE) return; // 128 MiB is more than the total amount of RAM in a Wii. @@ -99,12 +103,12 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit // Create all file info objects u32 number_of_file_infos = Common::swap32(*((u32*)m_file_system_table.data() + 2)); const u8* fst_start = m_file_system_table.data(); - const u8* name_table_start = fst_start + (number_of_file_infos * 0xC); + const u8* name_table_start = fst_start + (FST_ENTRY_SIZE * number_of_file_infos); const u8* name_table_end = fst_start + fst_size; if (name_table_end < name_table_start) return; for (u32 i = 0; i < number_of_file_infos; i++) - m_FileInfoVector.emplace_back(m_offset_shift, fst_start + (i * 0xC), name_table_start); + m_FileInfoVector.emplace_back(fst_start, m_offset_shift, i, number_of_file_infos); // If we haven't returned yet, everything succeeded m_Valid = true; -- cgit v1.2.3 From 87916fe09979eea276438deef83ba85fcace91e0 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 8 Aug 2015 19:59:33 +0200 Subject: Filesystem: Replace GetFileList() Instead of expecting callers to know how the size of directory file infos relates to which files are in which directories, filesystems now offer a GetRoot() method, and file infos offer a way to get their children. As a bonus, m_FileInfoVector no longer has to be created and kept around in RAM. Only the file info objects that actually are used are created. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 250 +++++++++++++++++++-------------- 1 file changed, 146 insertions(+), 104 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 9280dbab16..54b7f6a0e2 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -24,15 +25,60 @@ namespace DiscIO { constexpr u32 FST_ENTRY_SIZE = 4 * 3; // An FST entry consists of three 32-bit integers +// Set everything manually. FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift, u32 index, u32 total_file_infos) : m_fst(fst), m_offset_shift(offset_shift), m_index(index), m_total_file_infos(total_file_infos) { } +// For the root object only. +// m_fst and m_index must be correctly set before GetSize() is called! +FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift) + : m_fst(fst), m_offset_shift(offset_shift), m_index(0), m_total_file_infos(GetSize()) +{ +} + +// Copy data that is common to the whole file system. +FileInfoGCWii::FileInfoGCWii(const FileInfoGCWii& file_info, u32 index) + : FileInfoGCWii(file_info.m_fst, file_info.m_offset_shift, index, file_info.m_total_file_infos) +{ +} + FileInfoGCWii::~FileInfoGCWii() { } +uintptr_t FileInfoGCWii::GetAddress() const +{ + return reinterpret_cast(m_fst + FST_ENTRY_SIZE * m_index); +} + +u32 FileInfoGCWii::GetNextIndex() const +{ + return IsDirectory() ? GetSize() : m_index + 1; +} + +FileInfo& FileInfoGCWii::operator++() +{ + m_index = GetNextIndex(); + return *this; +} + +std::unique_ptr FileInfoGCWii::clone() const +{ + return std::make_unique(*this); +} + +FileInfo::const_iterator FileInfoGCWii::begin() const +{ + return const_iterator(std::make_unique(*this, m_index + 1)); +} + +FileInfo::const_iterator FileInfoGCWii::end() const +{ + return const_iterator(std::make_unique(*this, GetNextIndex())); +} + u32 FileInfoGCWii::Get(EntryProperty entry_property) const { return Common::swap32(m_fst + FST_ENTRY_SIZE * m_index + @@ -41,12 +87,23 @@ u32 FileInfoGCWii::Get(EntryProperty entry_property) const u32 FileInfoGCWii::GetSize() const { - return Get(EntryProperty::FILE_SIZE); + u32 result = Get(EntryProperty::FILE_SIZE); + + if (IsDirectory() && result <= m_index) + { + // For directories, GetSize is supposed to return the index of the next entry. + // If a file system is malformed and instead has an index that isn't after this one, + // we act as if the directory is empty to avoid strange behavior. + ERROR_LOG(DISCIO, "Invalid folder end in file system"); + return m_index + 1; + } + + return result; } u64 FileInfoGCWii::GetOffset() const { - return static_cast(Get(EntryProperty::FILE_OFFSET)) << (IsDirectory() ? 0 : m_offset_shift); + return static_cast(Get(EntryProperty::FILE_OFFSET)) << m_offset_shift; } bool FileInfoGCWii::IsDirectory() const @@ -54,6 +111,11 @@ bool FileInfoGCWii::IsDirectory() const return (Get(EntryProperty::NAME_OFFSET) & 0xFF000000) != 0; } +u32 FileInfoGCWii::GetTotalChildren() const +{ + return GetSize() - (m_index + 1); +} + std::string FileInfoGCWii::GetName() const { // TODO: Should we really always use SHIFT-JIS? @@ -63,8 +125,48 @@ std::string FileInfoGCWii::GetName() const return SHIFTJISToUTF8(reinterpret_cast(name)); } +std::string FileInfoGCWii::GetPath() const +{ + // The root entry doesn't have a name + if (m_index == 0) + return ""; + + if (IsDirectory()) + { + u32 parent_directory_index = Get(EntryProperty::FILE_OFFSET); + if (parent_directory_index >= m_index) + { + // The index of the parent directory is supposed to be smaller than + // the current index. If an FST is malformed and breaks that rule, + // there's a risk that parent directory pointers form a loop. + // To avoid stack overflows, this method returns. + ERROR_LOG(DISCIO, "Invalid parent offset in file system"); + return ""; + } + return FileInfoGCWii(*this, parent_directory_index).GetPath() + GetName() + "/"; + } + else + { + // The parent directory can be found by searching backwards + // for a directory that contains this file. + + FileInfoGCWii potential_parent(*this, m_index - 1); + while (!(potential_parent.IsDirectory() && potential_parent.GetSize() > m_index)) + { + if (potential_parent.m_index == 0) + { + // This can happen if an FST has a root with a size that's too small + ERROR_LOG(DISCIO, "The parent of %s couldn't be found", GetName().c_str()); + return ""; + } + potential_parent = FileInfoGCWii(*this, potential_parent.m_index - 1); + } + return potential_parent.GetPath() + GetName(); + } +} + FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partition) - : FileSystem(_rVolume, partition), m_Valid(false), m_offset_shift(0) + : FileSystem(_rVolume, partition), m_Valid(false), m_offset_shift(0), m_root(nullptr, 0, 0, 0) { // Check if this is a GameCube or Wii disc if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) @@ -81,7 +183,10 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit const u64 fst_offset = static_cast(*fst_offset_unshifted) << m_offset_shift; const u64 fst_size = static_cast(*fst_size_unshifted) << m_offset_shift; if (fst_size < FST_ENTRY_SIZE) + { + ERROR_LOG(DISCIO, "File system is too small"); return; + } // 128 MiB is more than the total amount of RAM in a Wii. // No file system should use anywhere near that much. @@ -98,17 +203,18 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit // Read the whole FST m_file_system_table.resize(fst_size); if (!m_rVolume->Read(fst_offset, fst_size, m_file_system_table.data(), m_partition)) + { + ERROR_LOG(DISCIO, "Couldn't read file system table"); return; + } - // Create all file info objects - u32 number_of_file_infos = Common::swap32(*((u32*)m_file_system_table.data() + 2)); - const u8* fst_start = m_file_system_table.data(); - const u8* name_table_start = fst_start + (FST_ENTRY_SIZE * number_of_file_infos); - const u8* name_table_end = fst_start + fst_size; - if (name_table_end < name_table_start) + // Create the root object + m_root = FileInfoGCWii(m_file_system_table.data(), m_offset_shift); + if (!m_root.IsDirectory()) + { + ERROR_LOG(DISCIO, "File system root is not a directory"); return; - for (u32 i = 0; i < number_of_file_infos; i++) - m_FileInfoVector.emplace_back(fst_start, m_offset_shift, i, number_of_file_infos); + } // If we haven't returned yet, everything succeeded m_Valid = true; @@ -116,33 +222,32 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit FileSystemGCWii::~FileSystemGCWii() { - m_FileInfoVector.clear(); } -const std::vector& FileSystemGCWii::GetFileList() const +const FileInfo& FileSystemGCWii::GetRoot() const { - return m_FileInfoVector; + return m_root; } -const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path) const +std::unique_ptr FileSystemGCWii::FindFileInfo(const std::string& path) const { - if (m_FileInfoVector.empty()) + if (!IsValid()) return nullptr; - return FindFileInfo(path, 0); + return FindFileInfo(path, m_root); } -const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path, - size_t search_start_offset) const +std::unique_ptr FileSystemGCWii::FindFileInfo(const std::string& path, + const FileInfo& file_info) const { // Given a path like "directory1/directory2/fileA.bin", this function will // find directory1 and then call itself to search for "directory2/fileA.bin". if (path.empty() || path == "/") - return &m_FileInfoVector[search_start_offset]; + return file_info.clone(); // It's only possible to search in directories. Searching in a file is an error - if (!m_FileInfoVector[search_start_offset].IsDirectory()) + if (!file_info.IsDirectory()) return nullptr; size_t first_dir_separator = path.find('/'); @@ -150,113 +255,50 @@ const FileInfo* FileSystemGCWii::FindFileInfo(const std::string& path, const std::string rest_of_path = (first_dir_separator != std::string::npos) ? path.substr(first_dir_separator + 1) : ""; - size_t search_end_offset = m_FileInfoVector[search_start_offset].GetSize(); - search_start_offset++; - while (search_start_offset < search_end_offset) + for (const FileInfo& child : file_info) { - const FileInfoGCWii& file_info = m_FileInfoVector[search_start_offset]; - - if (file_info.GetName() == searching_for) + if (child.GetName() == searching_for) { // A match is found. The rest of the path is passed on to finish the search. - const FileInfo* result = FindFileInfo(rest_of_path, search_start_offset); + std::unique_ptr result = FindFileInfo(rest_of_path, child); // If the search wasn't successful, the loop continues, just in case there's a second // file info that matches searching_for (which probably won't happen in practice) if (result) return result; } - - if (file_info.IsDirectory()) - { - // Skip a directory and everything that it contains - - if (file_info.GetSize() <= search_start_offset) - { - // The next offset (obtained by GetSize) is supposed to be larger than - // the current offset. If an FST is malformed and breaks that rule, - // there's a risk that next offset pointers form a loop. - // To avoid infinite loops, this method returns. - ERROR_LOG(DISCIO, "Invalid next offset in file system"); - return nullptr; - } - - search_start_offset = file_info.GetSize(); - } - else - { - // Skip a single file - search_start_offset++; - } } return nullptr; } -const FileInfo* FileSystemGCWii::FindFileInfo(u64 disc_offset) const +std::unique_ptr FileSystemGCWii::FindFileInfo(u64 disc_offset) const { - for (auto& file_info : m_FileInfoVector) - { - if ((file_info.GetOffset() <= disc_offset) && - ((file_info.GetOffset() + file_info.GetSize()) > disc_offset)) - { - return &file_info; - } - } + if (!IsValid()) + return nullptr; - return nullptr; + return FindFileInfo(disc_offset, m_root); } -std::string FileSystemGCWii::GetPath(u64 _Address) const +std::unique_ptr FileSystemGCWii::FindFileInfo(u64 disc_offset, + const FileInfo& file_info) const { - for (size_t i = 0; i < m_FileInfoVector.size(); ++i) + for (const FileInfo& child : file_info) { - const FileInfoGCWii& file_info = m_FileInfoVector[i]; - if ((file_info.GetOffset() <= _Address) && - ((file_info.GetOffset() + file_info.GetSize()) > _Address)) + if (child.IsDirectory()) { - return GetPathFromFSTOffset(i); + std::unique_ptr result = FindFileInfo(disc_offset, child); + if (result) + return result; } - } - - return ""; -} - -std::string FileSystemGCWii::GetPathFromFSTOffset(size_t file_info_offset) const -{ - // Root entry doesn't have a name - if (file_info_offset == 0) - return ""; - - const FileInfoGCWii& file_info = m_FileInfoVector[file_info_offset]; - if (file_info.IsDirectory()) - { - // The offset of the parent directory is stored in the current directory. - - if (file_info.GetOffset() >= file_info_offset) + else if ((file_info.GetOffset() <= disc_offset) && + ((file_info.GetOffset() + file_info.GetSize()) > disc_offset)) { - // The offset of the parent directory is supposed to be smaller than - // the current offset. If an FST is malformed and breaks that rule, - // there's a risk that parent directory pointers form a loop. - // To avoid stack overflows, this method returns. - ERROR_LOG(DISCIO, "Invalid parent offset in file system"); - return ""; + return file_info.clone(); } - return GetPathFromFSTOffset(file_info.GetOffset()) + file_info.GetName() + "/"; } - else - { - // The parent directory can be found by searching backwards - // for a directory that contains this file. - size_t parent_offset = file_info_offset - 1; - while (!(m_FileInfoVector[parent_offset].IsDirectory() && - m_FileInfoVector[parent_offset].GetSize() > file_info_offset)) - { - parent_offset--; - } - return GetPathFromFSTOffset(parent_offset) + file_info.GetName(); - } + return nullptr; } u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxBufferSize, @@ -272,8 +314,8 @@ u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxB DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64 " Size: %" PRIx32, - read_length, _OffsetInFile, GetPath(file_info->GetOffset()).c_str(), - file_info->GetOffset(), file_info->GetSize()); + read_length, _OffsetInFile, file_info->GetPath().c_str(), file_info->GetOffset(), + file_info->GetSize()); m_rVolume->Read(file_info->GetOffset() + _OffsetInFile, read_length, _pBuffer, m_partition); return read_length; -- cgit v1.2.3 From ee27be06d79a88f4e4344966002211a47a9eafeb Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 9 Aug 2015 13:41:41 +0200 Subject: Filesystem: Add a cache for finding file info by disc offset FileMonitor calls this every time a read happens, and there's no code that only calls this a small number of times, so having a cache is worthwhile. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 54b7f6a0e2..8546e4d4d1 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -277,27 +278,28 @@ std::unique_ptr FileSystemGCWii::FindFileInfo(u64 disc_offset) const if (!IsValid()) return nullptr; - return FindFileInfo(disc_offset, m_root); -} - -std::unique_ptr FileSystemGCWii::FindFileInfo(u64 disc_offset, - const FileInfo& file_info) const -{ - for (const FileInfo& child : file_info) + // Build a cache (unless there already is one) + if (m_offset_file_info_cache.empty()) { - if (child.IsDirectory()) - { - std::unique_ptr result = FindFileInfo(disc_offset, child); - if (result) - return result; - } - else if ((file_info.GetOffset() <= disc_offset) && - ((file_info.GetOffset() + file_info.GetSize()) > disc_offset)) + u32 fst_entries = m_root.GetSize(); + for (u32 i = 0; i < fst_entries; i++) { - return file_info.clone(); + FileInfoGCWii file_info(m_root, i); + if (!file_info.IsDirectory()) + m_offset_file_info_cache.emplace(file_info.GetOffset() + file_info.GetSize(), i); } } + // Get the first file that ends after disc_offset + const auto it = m_offset_file_info_cache.upper_bound(disc_offset); + if (it == m_offset_file_info_cache.end()) + return nullptr; + std::unique_ptr result(std::make_unique(m_root, it->second)); + + // If the file's start isn't after disc_offset, success + if (result->GetOffset() <= disc_offset) + return result; + return nullptr; } -- cgit v1.2.3 From e3a2cd827a750505d99e83d1eafa98dc0274d9f5 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 20 Oct 2015 14:32:04 +0200 Subject: Filesystem: Do more validity checking in the constructor This makes it possible to catch errors earlier so that file systems simply fail to load instead of behaving oddly. It also makes it possible to check for errors that weren't checkable before, like the end of a directory being after the end of the parent directory. --- Source/Core/DiscIO/FileSystemGCWii.cpp | 103 +++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 36 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 8546e4d4d1..8c3ce41b5b 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -88,18 +88,7 @@ u32 FileInfoGCWii::Get(EntryProperty entry_property) const u32 FileInfoGCWii::GetSize() const { - u32 result = Get(EntryProperty::FILE_SIZE); - - if (IsDirectory() && result <= m_index) - { - // For directories, GetSize is supposed to return the index of the next entry. - // If a file system is malformed and instead has an index that isn't after this one, - // we act as if the directory is empty to avoid strange behavior. - ERROR_LOG(DISCIO, "Invalid folder end in file system"); - return m_index + 1; - } - - return result; + return Get(EntryProperty::FILE_SIZE); } u64 FileInfoGCWii::GetOffset() const @@ -114,16 +103,20 @@ bool FileInfoGCWii::IsDirectory() const u32 FileInfoGCWii::GetTotalChildren() const { - return GetSize() - (m_index + 1); + return Get(EntryProperty::FILE_SIZE) - (m_index + 1); +} + +u64 FileInfoGCWii::GetNameOffset() const +{ + return static_cast(FST_ENTRY_SIZE) * m_total_file_infos + + (Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF); } std::string FileInfoGCWii::GetName() const { // TODO: Should we really always use SHIFT-JIS? // Some names in Pikmin (NTSC-U) don't make sense without it, but is it correct? - u32 name_offset = Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF; - const u8* name = m_fst + FST_ENTRY_SIZE * m_total_file_infos + name_offset; - return SHIFTJISToUTF8(reinterpret_cast(name)); + return SHIFTJISToUTF8(reinterpret_cast(m_fst + GetNameOffset())); } std::string FileInfoGCWii::GetPath() const @@ -135,37 +128,63 @@ std::string FileInfoGCWii::GetPath() const if (IsDirectory()) { u32 parent_directory_index = Get(EntryProperty::FILE_OFFSET); - if (parent_directory_index >= m_index) - { - // The index of the parent directory is supposed to be smaller than - // the current index. If an FST is malformed and breaks that rule, - // there's a risk that parent directory pointers form a loop. - // To avoid stack overflows, this method returns. - ERROR_LOG(DISCIO, "Invalid parent offset in file system"); - return ""; - } return FileInfoGCWii(*this, parent_directory_index).GetPath() + GetName() + "/"; } else { // The parent directory can be found by searching backwards - // for a directory that contains this file. - + // for a directory that contains this file. The search cannot fail, + // because the root directory at index 0 contains all files. FileInfoGCWii potential_parent(*this, m_index - 1); - while (!(potential_parent.IsDirectory() && potential_parent.GetSize() > m_index)) + while (!(potential_parent.IsDirectory() && + potential_parent.Get(EntryProperty::FILE_SIZE) > m_index)) { - if (potential_parent.m_index == 0) - { - // This can happen if an FST has a root with a size that's too small - ERROR_LOG(DISCIO, "The parent of %s couldn't be found", GetName().c_str()); - return ""; - } potential_parent = FileInfoGCWii(*this, potential_parent.m_index - 1); } return potential_parent.GetPath() + GetName(); } } +bool FileInfoGCWii::IsValid(u64 fst_size, const FileInfoGCWii& parent_directory) const +{ + if (GetNameOffset() >= fst_size) + { + ERROR_LOG(DISCIO, "Impossibly large name offset in file system"); + return false; + } + + if (IsDirectory()) + { + if (Get(EntryProperty::FILE_OFFSET) != parent_directory.m_index) + { + ERROR_LOG(DISCIO, "Incorrect parent offset in file system"); + return false; + } + + u32 size = Get(EntryProperty::FILE_SIZE); + + if (size <= m_index) + { + ERROR_LOG(DISCIO, "Impossibly small directory size in file system"); + return false; + } + + if (size > parent_directory.Get(EntryProperty::FILE_SIZE)) + { + ERROR_LOG(DISCIO, "Impossibly large directory size in file system"); + return false; + } + + for (const FileInfo& child : *this) + { + if (!static_cast(child).IsValid(fst_size, *this)) + return false; + } + } + + return true; +} + FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partition) : FileSystem(_rVolume, partition), m_Valid(false), m_offset_shift(0), m_root(nullptr, 0, 0, 0) { @@ -217,8 +236,20 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit return; } - // If we haven't returned yet, everything succeeded - m_Valid = true; + if (FST_ENTRY_SIZE * m_root.GetSize() > fst_size) + { + ERROR_LOG(DISCIO, "File system has too many entries for its size"); + return; + } + + // If the FST's final byte isn't 0, CFileInfoGCWii::GetName() can read past the end + if (m_file_system_table[fst_size - 1] != 0) + { + ERROR_LOG(DISCIO, "File system does not end with a null byte"); + return; + } + + m_Valid = m_root.IsValid(fst_size, m_root); } FileSystemGCWii::~FileSystemGCWii() -- cgit v1.2.3 From 0b7d2e7c688a1e5e45c6d53fdcc89e4663e6d299 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 9 Aug 2015 20:53:38 +0200 Subject: Filesystem: Modernize variable names --- Source/Core/DiscIO/FileSystemGCWii.cpp | 88 +++++++++++++++++----------------- 1 file changed, 44 insertions(+), 44 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 8c3ce41b5b..62e29b2b5c 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -185,19 +185,19 @@ bool FileInfoGCWii::IsValid(u64 fst_size, const FileInfoGCWii& parent_directory) return true; } -FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partition) - : FileSystem(_rVolume, partition), m_Valid(false), m_offset_shift(0), m_root(nullptr, 0, 0, 0) +FileSystemGCWii::FileSystemGCWii(const Volume* volume, const Partition& partition) + : FileSystem(volume, partition), m_valid(false), m_offset_shift(0), m_root(nullptr, 0, 0, 0) { // Check if this is a GameCube or Wii disc - if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) + if (m_volume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) m_offset_shift = 2; // Wii file system - else if (m_rVolume->ReadSwapped(0x1c, m_partition) == u32(0xC2339F3D)) + else if (m_volume->ReadSwapped(0x1c, m_partition) == u32(0xC2339F3D)) m_offset_shift = 0; // GameCube file system else return; - const std::optional fst_offset_unshifted = m_rVolume->ReadSwapped(0x424, m_partition); - const std::optional fst_size_unshifted = m_rVolume->ReadSwapped(0x428, m_partition); + const std::optional fst_offset_unshifted = m_volume->ReadSwapped(0x424, m_partition); + const std::optional fst_size_unshifted = m_volume->ReadSwapped(0x428, m_partition); if (!fst_offset_unshifted || !fst_size_unshifted) return; const u64 fst_offset = static_cast(*fst_offset_unshifted) << m_offset_shift; @@ -222,7 +222,7 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit // Read the whole FST m_file_system_table.resize(fst_size); - if (!m_rVolume->Read(fst_offset, fst_size, m_file_system_table.data(), m_partition)) + if (!m_volume->Read(fst_offset, fst_size, m_file_system_table.data(), m_partition)) { ERROR_LOG(DISCIO, "Couldn't read file system table"); return; @@ -249,7 +249,7 @@ FileSystemGCWii::FileSystemGCWii(const Volume* _rVolume, const Partition& partit return; } - m_Valid = m_root.IsValid(fst_size, m_root); + m_valid = m_root.IsValid(fst_size, m_root); } FileSystemGCWii::~FileSystemGCWii() @@ -334,66 +334,66 @@ std::unique_ptr FileSystemGCWii::FindFileInfo(u64 disc_offset) const return nullptr; } -u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* _pBuffer, u64 _MaxBufferSize, - u64 _OffsetInFile) const +u64 FileSystemGCWii::ReadFile(const FileInfo* file_info, u8* buffer, u64 max_buffer_size, + u64 offset_in_file) const { if (!file_info || file_info->IsDirectory()) return 0; - if (_OffsetInFile >= file_info->GetSize()) + if (offset_in_file >= file_info->GetSize()) return 0; - u64 read_length = std::min(_MaxBufferSize, file_info->GetSize() - _OffsetInFile); + u64 read_length = std::min(max_buffer_size, file_info->GetSize() - offset_in_file); DEBUG_LOG(DISCIO, "Reading %" PRIx64 " bytes at %" PRIx64 " from file %s. Offset: %" PRIx64 " Size: %" PRIx32, - read_length, _OffsetInFile, file_info->GetPath().c_str(), file_info->GetOffset(), + read_length, offset_in_file, file_info->GetPath().c_str(), file_info->GetOffset(), file_info->GetSize()); - m_rVolume->Read(file_info->GetOffset() + _OffsetInFile, read_length, _pBuffer, m_partition); + m_volume->Read(file_info->GetOffset() + offset_in_file, read_length, buffer, m_partition); return read_length; } bool FileSystemGCWii::ExportFile(const FileInfo* file_info, - const std::string& _rExportFilename) const + const std::string& export_filename) const { if (!file_info || file_info->IsDirectory()) return false; - u64 remainingSize = file_info->GetSize(); - u64 fileOffset = file_info->GetOffset(); + u64 remaining_size = file_info->GetSize(); + u64 file_offset = file_info->GetOffset(); - File::IOFile f(_rExportFilename, "wb"); + File::IOFile f(export_filename, "wb"); if (!f) return false; bool result = true; - while (remainingSize) + while (remaining_size) { // Limit read size to 128 MB - size_t readSize = (size_t)std::min(remainingSize, (u64)0x08000000); + size_t read_size = (size_t)std::min(remaining_size, (u64)0x08000000); - std::vector buffer(readSize); + std::vector buffer(read_size); - result = m_rVolume->Read(fileOffset, readSize, &buffer[0], m_partition); + result = m_volume->Read(file_offset, read_size, &buffer[0], m_partition); if (!result) break; - f.WriteBytes(&buffer[0], readSize); + f.WriteBytes(&buffer[0], read_size); - remainingSize -= readSize; - fileOffset += readSize; + remaining_size -= read_size; + file_offset += read_size; } return result; } -bool FileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const +bool FileSystemGCWii::ExportApploader(const std::string& export_folder) const { - std::optional apploader_size = m_rVolume->ReadSwapped(0x2440 + 0x14, m_partition); - const std::optional trailer_size = m_rVolume->ReadSwapped(0x2440 + 0x18, m_partition); + std::optional apploader_size = m_volume->ReadSwapped(0x2440 + 0x14, m_partition); + const std::optional trailer_size = m_volume->ReadSwapped(0x2440 + 0x18, m_partition); constexpr u32 header_size = 0x20; if (!apploader_size || !trailer_size) return false; @@ -401,14 +401,14 @@ bool FileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const DEBUG_LOG(DISCIO, "Apploader size -> %x", *apploader_size); std::vector buffer(*apploader_size); - if (m_rVolume->Read(0x2440, *apploader_size, buffer.data(), m_partition)) + if (m_volume->Read(0x2440, *apploader_size, buffer.data(), m_partition)) { - std::string exportName(_rExportFolder + "/apploader.img"); + std::string export_name(export_folder + "/apploader.img"); - File::IOFile AppFile(exportName, "wb"); - if (AppFile) + File::IOFile apploader_file(export_name, "wb"); + if (apploader_file) { - AppFile.WriteBytes(buffer.data(), *apploader_size); + apploader_file.WriteBytes(buffer.data(), *apploader_size); return true; } } @@ -418,7 +418,7 @@ bool FileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const std::optional FileSystemGCWii::GetBootDOLOffset() const { - std::optional offset = m_rVolume->ReadSwapped(0x420, m_partition); + std::optional offset = m_volume->ReadSwapped(0x420, m_partition); return offset ? static_cast(*offset) << m_offset_shift : std::optional(); } @@ -430,9 +430,9 @@ std::optional FileSystemGCWii::GetBootDOLSize(u64 dol_offset) const for (u8 i = 0; i < 7; i++) { const std::optional offset = - m_rVolume->ReadSwapped(dol_offset + 0x00 + i * 4, m_partition); + m_volume->ReadSwapped(dol_offset + 0x00 + i * 4, m_partition); const std::optional size = - m_rVolume->ReadSwapped(dol_offset + 0x90 + i * 4, m_partition); + m_volume->ReadSwapped(dol_offset + 0x90 + i * 4, m_partition); if (!offset || !size) return {}; dol_size = std::max(*offset + *size, dol_size); @@ -442,9 +442,9 @@ std::optional FileSystemGCWii::GetBootDOLSize(u64 dol_offset) const for (u8 i = 0; i < 11; i++) { const std::optional offset = - m_rVolume->ReadSwapped(dol_offset + 0x1c + i * 4, m_partition); + m_volume->ReadSwapped(dol_offset + 0x1c + i * 4, m_partition); const std::optional size = - m_rVolume->ReadSwapped(dol_offset + 0xac + i * 4, m_partition); + m_volume->ReadSwapped(dol_offset + 0xac + i * 4, m_partition); if (!offset || !size) return {}; dol_size = std::max(*offset + *size, dol_size); @@ -453,7 +453,7 @@ std::optional FileSystemGCWii::GetBootDOLSize(u64 dol_offset) const return dol_size; } -bool FileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const +bool FileSystemGCWii::ExportDOL(const std::string& export_folder) const { std::optional dol_offset = GetBootDOLOffset(); if (!dol_offset) @@ -463,14 +463,14 @@ bool FileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const return false; std::vector buffer(*dol_size); - if (m_rVolume->Read(*dol_offset, *dol_size, &buffer[0], m_partition)) + if (m_volume->Read(*dol_offset, *dol_size, buffer.data(), m_partition)) { - std::string exportName(_rExportFolder + "/boot.dol"); + std::string export_name(export_folder + "/boot.dol"); - File::IOFile DolFile(exportName, "wb"); - if (DolFile) + File::IOFile dol_file(export_name, "wb"); + if (dol_file) { - DolFile.WriteBytes(&buffer[0], *dol_size); + dol_file.WriteBytes(&buffer[0], *dol_size); return true; } } -- cgit v1.2.3 From 583406d900bd0ca9111135c6a9e526aed67e8322 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 13 Jun 2017 23:05:30 +0200 Subject: Filesystem: Make destructors = default --- Source/Core/DiscIO/FileSystemGCWii.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index 62e29b2b5c..09a171677d 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -45,9 +45,7 @@ FileInfoGCWii::FileInfoGCWii(const FileInfoGCWii& file_info, u32 index) { } -FileInfoGCWii::~FileInfoGCWii() -{ -} +FileInfoGCWii::~FileInfoGCWii() = default; uintptr_t FileInfoGCWii::GetAddress() const { @@ -252,9 +250,7 @@ FileSystemGCWii::FileSystemGCWii(const Volume* volume, const Partition& partitio m_valid = m_root.IsValid(fst_size, m_root); } -FileSystemGCWii::~FileSystemGCWii() -{ -} +FileSystemGCWii::~FileSystemGCWii() = default; const FileInfo& FileSystemGCWii::GetRoot() const { -- cgit v1.2.3