From 77c684c1140f6bf3fb7d4560d06d2efb1a2ee5e2 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 6 Jul 2018 10:51:32 -0400 Subject: Virtual Filesystem (#597) * Add VfsFile and VfsDirectory classes * Finish abstract Vfs classes * Implement RealVfsFile (computer fs backend) * Finish RealVfsFile and RealVfsDirectory * Finished OffsetVfsFile * More changes * Fix import paths * Major refactor * Remove double const * Use experimental/filesystem or filesystem depending on compiler * Port partition_filesystem * More changes * More Overhaul * FSP_SRV fixes * Fixes and testing * Try to get filesystem to compile * Filesystem on linux * Remove std::filesystem and document/test * Compile fixes * Missing include * Bug fixes * Fixes * Rename v_file and v_dir * clang-format fix * Rename NGLOG_* to LOG_* * Most review changes * Fix TODO * Guess 'main' to be Directory by filename --- src/core/file_sys/content_archive.cpp | 164 ++++++++++++++++++++ src/core/file_sys/content_archive.h | 89 +++++++++++ src/core/file_sys/disk_filesystem.cpp | 237 ----------------------------- src/core/file_sys/disk_filesystem.h | 84 ---------- src/core/file_sys/filesystem.h | 132 ---------------- src/core/file_sys/partition_filesystem.cpp | 136 ++++++++--------- src/core/file_sys/partition_filesystem.h | 29 ++-- src/core/file_sys/program_metadata.cpp | 43 ++---- src/core/file_sys/program_metadata.h | 6 +- src/core/file_sys/romfs_factory.cpp | 38 ----- src/core/file_sys/romfs_factory.h | 35 ----- src/core/file_sys/romfs_filesystem.cpp | 110 ------------- src/core/file_sys/romfs_filesystem.h | 85 ----------- src/core/file_sys/savedata_factory.cpp | 54 ------- src/core/file_sys/savedata_factory.h | 33 ---- src/core/file_sys/sdmc_factory.cpp | 39 ----- src/core/file_sys/sdmc_factory.h | 31 ---- src/core/file_sys/vfs.cpp | 187 +++++++++++++++++++++++ src/core/file_sys/vfs.h | 220 ++++++++++++++++++++++++++ src/core/file_sys/vfs_offset.cpp | 92 +++++++++++ src/core/file_sys/vfs_offset.h | 46 ++++++ src/core/file_sys/vfs_real.cpp | 168 ++++++++++++++++++++ src/core/file_sys/vfs_real.h | 65 ++++++++ 23 files changed, 1127 insertions(+), 996 deletions(-) create mode 100644 src/core/file_sys/content_archive.cpp create mode 100644 src/core/file_sys/content_archive.h delete mode 100644 src/core/file_sys/disk_filesystem.cpp delete mode 100644 src/core/file_sys/disk_filesystem.h delete mode 100644 src/core/file_sys/romfs_factory.cpp delete mode 100644 src/core/file_sys/romfs_factory.h delete mode 100644 src/core/file_sys/romfs_filesystem.cpp delete mode 100644 src/core/file_sys/romfs_filesystem.h delete mode 100644 src/core/file_sys/savedata_factory.cpp delete mode 100644 src/core/file_sys/savedata_factory.h delete mode 100644 src/core/file_sys/sdmc_factory.cpp delete mode 100644 src/core/file_sys/sdmc_factory.h create mode 100644 src/core/file_sys/vfs.cpp create mode 100644 src/core/file_sys/vfs.h create mode 100644 src/core/file_sys/vfs_offset.cpp create mode 100644 src/core/file_sys/vfs_offset.h create mode 100644 src/core/file_sys/vfs_real.cpp create mode 100644 src/core/file_sys/vfs_real.h (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp new file mode 100644 index 000000000..b45b83a26 --- /dev/null +++ b/src/core/file_sys/content_archive.cpp @@ -0,0 +1,164 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/logging/log.h" +#include "core/file_sys/content_archive.h" +#include "core/file_sys/vfs_offset.h" +#include "core/loader/loader.h" + +namespace FileSys { + +// Media offsets in headers are stored divided by 512. Mult. by this to get real offset. +constexpr u64 MEDIA_OFFSET_MULTIPLIER = 0x200; + +constexpr u64 SECTION_HEADER_SIZE = 0x200; +constexpr u64 SECTION_HEADER_OFFSET = 0x400; + +constexpr u32 IVFC_MAX_LEVEL = 6; + +enum class NCASectionFilesystemType : u8 { PFS0 = 0x2, ROMFS = 0x3 }; + +struct NCASectionHeaderBlock { + INSERT_PADDING_BYTES(3); + NCASectionFilesystemType filesystem_type; + u8 crypto_type; + INSERT_PADDING_BYTES(3); +}; +static_assert(sizeof(NCASectionHeaderBlock) == 0x8, "NCASectionHeaderBlock has incorrect size."); + +struct PFS0Superblock { + NCASectionHeaderBlock header_block; + std::array hash; + u32_le size; + INSERT_PADDING_BYTES(4); + u64_le hash_table_offset; + u64_le hash_table_size; + u64_le pfs0_header_offset; + u64_le pfs0_size; + INSERT_PADDING_BYTES(432); +}; +static_assert(sizeof(PFS0Superblock) == 0x200, "PFS0Superblock has incorrect size."); + +struct IVFCLevel { + u64_le offset; + u64_le size; + u32_le block_size; + u32_le reserved; +}; +static_assert(sizeof(IVFCLevel) == 0x18, "IVFCLevel has incorrect size."); + +struct RomFSSuperblock { + NCASectionHeaderBlock header_block; + u32_le magic; + u32_le magic_number; + INSERT_PADDING_BYTES(8); + std::array levels; + INSERT_PADDING_BYTES(64); +}; +static_assert(sizeof(RomFSSuperblock) == 0xE8, "RomFSSuperblock has incorrect size."); + +NCA::NCA(VirtualFile file_) : file(file_) { + if (sizeof(NCAHeader) != file->ReadObject(&header)) + LOG_CRITICAL(Loader, "File reader errored out during header read."); + + if (!IsValidNCA(header)) { + status = Loader::ResultStatus::ErrorInvalidFormat; + return; + } + + std::ptrdiff_t number_sections = + std::count_if(std::begin(header.section_tables), std::end(header.section_tables), + [](NCASectionTableEntry entry) { return entry.media_offset > 0; }); + + for (std::ptrdiff_t i = 0; i < number_sections; ++i) { + // Seek to beginning of this section. + NCASectionHeaderBlock block{}; + if (sizeof(NCASectionHeaderBlock) != + file->ReadObject(&block, SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE)) + LOG_CRITICAL(Loader, "File reader errored out during header read."); + + if (block.filesystem_type == NCASectionFilesystemType::ROMFS) { + RomFSSuperblock sb{}; + if (sizeof(RomFSSuperblock) != + file->ReadObject(&sb, SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE)) + LOG_CRITICAL(Loader, "File reader errored out during header read."); + + const size_t romfs_offset = + header.section_tables[i].media_offset * MEDIA_OFFSET_MULTIPLIER + + sb.levels[IVFC_MAX_LEVEL - 1].offset; + const size_t romfs_size = sb.levels[IVFC_MAX_LEVEL - 1].size; + files.emplace_back(std::make_shared(file, romfs_size, romfs_offset)); + romfs = files.back(); + } else if (block.filesystem_type == NCASectionFilesystemType::PFS0) { + PFS0Superblock sb{}; + // Seek back to beginning of this section. + if (sizeof(PFS0Superblock) != + file->ReadObject(&sb, SECTION_HEADER_OFFSET + i * SECTION_HEADER_SIZE)) + LOG_CRITICAL(Loader, "File reader errored out during header read."); + + u64 offset = (static_cast(header.section_tables[i].media_offset) * + MEDIA_OFFSET_MULTIPLIER) + + sb.pfs0_header_offset; + u64 size = MEDIA_OFFSET_MULTIPLIER * (header.section_tables[i].media_end_offset - + header.section_tables[i].media_offset); + auto npfs = std::make_shared( + std::make_shared(file, size, offset)); + + if (npfs->GetStatus() == Loader::ResultStatus::Success) { + dirs.emplace_back(npfs); + if (IsDirectoryExeFS(dirs.back())) + exefs = dirs.back(); + } + } + } + + status = Loader::ResultStatus::Success; +} + +Loader::ResultStatus NCA::GetStatus() const { + return status; +} + +std::vector> NCA::GetFiles() const { + if (status != Loader::ResultStatus::Success) + return {}; + return files; +} + +std::vector> NCA::GetSubdirectories() const { + if (status != Loader::ResultStatus::Success) + return {}; + return dirs; +} + +std::string NCA::GetName() const { + return file->GetName(); +} + +std::shared_ptr NCA::GetParentDirectory() const { + return file->GetContainingDirectory(); +} + +NCAContentType NCA::GetType() const { + return header.content_type; +} + +u64 NCA::GetTitleId() const { + if (status != Loader::ResultStatus::Success) + return {}; + return header.title_id; +} + +VirtualFile NCA::GetRomFS() const { + return romfs; +} + +VirtualDir NCA::GetExeFS() const { + return exefs; +} + +bool NCA::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { + return false; +} +} // namespace FileSys diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h new file mode 100644 index 000000000..eb4ca1c18 --- /dev/null +++ b/src/core/file_sys/content_archive.h @@ -0,0 +1,89 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/swap.h" +#include "core/file_sys/partition_filesystem.h" + +namespace FileSys { + +enum class NCAContentType : u8 { Program = 0, Meta = 1, Control = 2, Manual = 3, Data = 4 }; + +struct NCASectionTableEntry { + u32_le media_offset; + u32_le media_end_offset; + INSERT_PADDING_BYTES(0x8); +}; +static_assert(sizeof(NCASectionTableEntry) == 0x10, "NCASectionTableEntry has incorrect size."); + +struct NCAHeader { + std::array rsa_signature_1; + std::array rsa_signature_2; + u32_le magic; + u8 is_system; + NCAContentType content_type; + u8 crypto_type; + u8 key_index; + u64_le size; + u64_le title_id; + INSERT_PADDING_BYTES(0x4); + u32_le sdk_version; + u8 crypto_type_2; + INSERT_PADDING_BYTES(15); + std::array rights_id; + std::array section_tables; + std::array, 0x4> hash_tables; + std::array, 0x4> key_area; + INSERT_PADDING_BYTES(0xC0); +}; +static_assert(sizeof(NCAHeader) == 0x400, "NCAHeader has incorrect size."); + +static bool IsDirectoryExeFS(std::shared_ptr pfs) { + // According to switchbrew, an exefs must only contain these two files: + return pfs->GetFile("main") != nullptr && pfs->GetFile("main.npdm") != nullptr; +} + +static bool IsValidNCA(const NCAHeader& header) { + return header.magic == Common::MakeMagic('N', 'C', 'A', '2') || + header.magic == Common::MakeMagic('N', 'C', 'A', '3'); +} + +// An implementation of VfsDirectory that represents a Nintendo Content Archive (NCA) conatiner. +// After construction, use GetStatus to determine if the file is valid and ready to be used. +class NCA : public ReadOnlyVfsDirectory { +public: + explicit NCA(VirtualFile file); + Loader::ResultStatus GetStatus() const; + + std::vector> GetFiles() const override; + std::vector> GetSubdirectories() const override; + std::string GetName() const override; + std::shared_ptr GetParentDirectory() const override; + + NCAContentType GetType() const; + u64 GetTitleId() const; + + VirtualFile GetRomFS() const; + VirtualDir GetExeFS() const; + +protected: + bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; + +private: + std::vector dirs; + std::vector files; + + VirtualFile romfs = nullptr; + VirtualDir exefs = nullptr; + VirtualFile file; + + NCAHeader header{}; + + Loader::ResultStatus status{}; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/disk_filesystem.cpp b/src/core/file_sys/disk_filesystem.cpp deleted file mode 100644 index 8c6f15bb5..000000000 --- a/src/core/file_sys/disk_filesystem.cpp +++ /dev/null @@ -1,237 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include -#include "common/common_types.h" -#include "common/logging/log.h" -#include "core/file_sys/disk_filesystem.h" -#include "core/file_sys/errors.h" - -namespace FileSys { - -static std::string ModeFlagsToString(Mode mode) { - std::string mode_str; - u32 mode_flags = static_cast(mode); - - // Calculate the correct open mode for the file. - if ((mode_flags & static_cast(Mode::Read)) && - (mode_flags & static_cast(Mode::Write))) { - if (mode_flags & static_cast(Mode::Append)) - mode_str = "a+"; - else - mode_str = "r+"; - } else { - if (mode_flags & static_cast(Mode::Read)) - mode_str = "r"; - else if (mode_flags & static_cast(Mode::Append)) - mode_str = "a"; - else if (mode_flags & static_cast(Mode::Write)) - mode_str = "w"; - } - - mode_str += "b"; - - return mode_str; -} - -std::string Disk_FileSystem::GetName() const { - return "Disk"; -} - -ResultVal> Disk_FileSystem::OpenFile(const std::string& path, - Mode mode) const { - - // Calculate the correct open mode for the file. - std::string mode_str = ModeFlagsToString(mode); - - std::string full_path = base_directory + path; - auto file = std::make_shared(full_path, mode_str.c_str()); - - if (!file->IsOpen()) { - return ERROR_PATH_NOT_FOUND; - } - - return MakeResult>( - std::make_unique(std::move(file))); -} - -ResultCode Disk_FileSystem::DeleteFile(const std::string& path) const { - if (!FileUtil::Exists(path)) { - return ERROR_PATH_NOT_FOUND; - } - - FileUtil::Delete(path); - - return RESULT_SUCCESS; -} - -ResultCode Disk_FileSystem::RenameFile(const std::string& src_path, - const std::string& dest_path) const { - const std::string full_src_path = base_directory + src_path; - const std::string full_dest_path = base_directory + dest_path; - - if (!FileUtil::Exists(full_src_path)) { - return ERROR_PATH_NOT_FOUND; - } - // TODO(wwylele): Use correct error code - return FileUtil::Rename(full_src_path, full_dest_path) ? RESULT_SUCCESS : ResultCode(-1); -} - -ResultCode Disk_FileSystem::DeleteDirectory(const Path& path) const { - LOG_WARNING(Service_FS, "(STUBBED) called"); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultCode Disk_FileSystem::DeleteDirectoryRecursively(const Path& path) const { - LOG_WARNING(Service_FS, "(STUBBED) called"); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultCode Disk_FileSystem::CreateFile(const std::string& path, u64 size) const { - LOG_WARNING(Service_FS, "(STUBBED) called"); - - std::string full_path = base_directory + path; - if (size == 0) { - FileUtil::CreateEmptyFile(full_path); - return RESULT_SUCCESS; - } - - FileUtil::IOFile file(full_path, "wb"); - // Creates a sparse file (or a normal file on filesystems without the concept of sparse files) - // We do this by seeking to the right size, then writing a single null byte. - if (file.Seek(size - 1, SEEK_SET) && file.WriteBytes("", 1) == 1) { - return RESULT_SUCCESS; - } - - LOG_ERROR(Service_FS, "Too large file"); - // TODO(Subv): Find out the correct error code - return ResultCode(-1); -} - -ResultCode Disk_FileSystem::CreateDirectory(const std::string& path) const { - // TODO(Subv): Perform path validation to prevent escaping the emulator sandbox. - std::string full_path = base_directory + path; - - if (FileUtil::CreateDir(full_path)) { - return RESULT_SUCCESS; - } - - LOG_CRITICAL(Service_FS, "(unreachable) Unknown error creating {}", full_path); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultCode Disk_FileSystem::RenameDirectory(const Path& src_path, const Path& dest_path) const { - LOG_WARNING(Service_FS, "(STUBBED) called"); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultVal> Disk_FileSystem::OpenDirectory( - const std::string& path) const { - - std::string full_path = base_directory + path; - - if (!FileUtil::IsDirectory(full_path)) { - // TODO(Subv): Find the correct error code for this. - return ResultCode(-1); - } - - auto directory = std::make_unique(full_path); - return MakeResult>(std::move(directory)); -} - -u64 Disk_FileSystem::GetFreeSpaceSize() const { - LOG_WARNING(Service_FS, "(STUBBED) called"); - return 0; -} - -ResultVal Disk_FileSystem::GetEntryType(const std::string& path) const { - std::string full_path = base_directory + path; - if (!FileUtil::Exists(full_path)) { - return ERROR_PATH_NOT_FOUND; - } - - if (FileUtil::IsDirectory(full_path)) - return MakeResult(EntryType::Directory); - - return MakeResult(EntryType::File); -} - -ResultVal Disk_Storage::Read(const u64 offset, const size_t length, u8* buffer) const { - LOG_TRACE(Service_FS, "called offset={}, length={}", offset, length); - file->Seek(offset, SEEK_SET); - return MakeResult(file->ReadBytes(buffer, length)); -} - -ResultVal Disk_Storage::Write(const u64 offset, const size_t length, const bool flush, - const u8* buffer) const { - LOG_WARNING(Service_FS, "(STUBBED) called"); - file->Seek(offset, SEEK_SET); - size_t written = file->WriteBytes(buffer, length); - if (flush) { - file->Flush(); - } - return MakeResult(written); -} - -u64 Disk_Storage::GetSize() const { - return file->GetSize(); -} - -bool Disk_Storage::SetSize(const u64 size) const { - file->Resize(size); - file->Flush(); - return true; -} - -Disk_Directory::Disk_Directory(const std::string& path) { - unsigned size = FileUtil::ScanDirectoryTree(path, directory); - directory.size = size; - directory.isDirectory = true; - children_iterator = directory.children.begin(); -} - -u64 Disk_Directory::Read(const u64 count, Entry* entries) { - u64 entries_read = 0; - - while (entries_read < count && children_iterator != directory.children.cend()) { - const FileUtil::FSTEntry& file = *children_iterator; - const std::string& filename = file.virtualName; - Entry& entry = entries[entries_read]; - - LOG_TRACE(Service_FS, "File {}: size={} dir={}", filename, file.size, file.isDirectory); - - // TODO(Link Mauve): use a proper conversion to UTF-16. - for (size_t j = 0; j < FILENAME_LENGTH; ++j) { - entry.filename[j] = filename[j]; - if (!filename[j]) - break; - } - - if (file.isDirectory) { - entry.file_size = 0; - entry.type = EntryType::Directory; - } else { - entry.file_size = file.size; - entry.type = EntryType::File; - } - - ++entries_read; - ++children_iterator; - } - return entries_read; -} - -u64 Disk_Directory::GetEntryCount() const { - // We convert the children iterator into a const_iterator to allow template argument deduction - // in std::distance. - std::vector::const_iterator current = children_iterator; - return std::distance(current, directory.children.end()); -} - -} // namespace FileSys diff --git a/src/core/file_sys/disk_filesystem.h b/src/core/file_sys/disk_filesystem.h deleted file mode 100644 index 591e39fda..000000000 --- a/src/core/file_sys/disk_filesystem.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include "common/common_types.h" -#include "common/file_util.h" -#include "core/file_sys/directory.h" -#include "core/file_sys/filesystem.h" -#include "core/file_sys/storage.h" -#include "core/hle/result.h" - -namespace FileSys { - -class Disk_FileSystem : public FileSystemBackend { -public: - explicit Disk_FileSystem(std::string base_directory) - : base_directory(std::move(base_directory)) {} - - std::string GetName() const override; - - ResultVal> OpenFile(const std::string& path, - Mode mode) const override; - ResultCode DeleteFile(const std::string& path) const override; - ResultCode RenameFile(const std::string& src_path, const std::string& dest_path) const override; - ResultCode DeleteDirectory(const Path& path) const override; - ResultCode DeleteDirectoryRecursively(const Path& path) const override; - ResultCode CreateFile(const std::string& path, u64 size) const override; - ResultCode CreateDirectory(const std::string& path) const override; - ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override; - ResultVal> OpenDirectory( - const std::string& path) const override; - u64 GetFreeSpaceSize() const override; - ResultVal GetEntryType(const std::string& path) const override; - -protected: - std::string base_directory; -}; - -class Disk_Storage : public StorageBackend { -public: - explicit Disk_Storage(std::shared_ptr file) : file(std::move(file)) {} - - ResultVal Read(u64 offset, size_t length, u8* buffer) const override; - ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; - u64 GetSize() const override; - bool SetSize(u64 size) const override; - bool Close() const override { - return false; - } - void Flush() const override {} - -private: - std::shared_ptr file; -}; - -class Disk_Directory : public DirectoryBackend { -public: - explicit Disk_Directory(const std::string& path); - - ~Disk_Directory() override { - Close(); - } - - u64 Read(const u64 count, Entry* entries) override; - u64 GetEntryCount() const override; - - bool Close() const override { - return true; - } - -protected: - FileUtil::FSTEntry directory; - - // We need to remember the last entry we returned, so a subsequent call to Read will continue - // from the next one. This iterator will always point to the next unread entry. - std::vector::iterator children_iterator; -}; - -} // namespace FileSys diff --git a/src/core/file_sys/filesystem.h b/src/core/file_sys/filesystem.h index 295a3133e..2d925d9e4 100644 --- a/src/core/file_sys/filesystem.h +++ b/src/core/file_sys/filesystem.h @@ -66,136 +66,4 @@ private: std::u16string u16str; }; -/// Parameters of the archive, as specified in the Create or Format call. -struct ArchiveFormatInfo { - u32_le total_size; ///< The pre-defined size of the archive. - u32_le number_directories; ///< The pre-defined number of directories in the archive. - u32_le number_files; ///< The pre-defined number of files in the archive. - u8 duplicate_data; ///< Whether the archive should duplicate the data. -}; -static_assert(std::is_pod::value, "ArchiveFormatInfo is not POD"); - -class FileSystemBackend : NonCopyable { -public: - virtual ~FileSystemBackend() {} - - /** - * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.) - */ - virtual std::string GetName() const = 0; - - /** - * Create a file specified by its path - * @param path Path relative to the Archive - * @param size The size of the new file, filled with zeroes - * @return Result of the operation - */ - virtual ResultCode CreateFile(const std::string& path, u64 size) const = 0; - - /** - * Delete a file specified by its path - * @param path Path relative to the archive - * @return Result of the operation - */ - virtual ResultCode DeleteFile(const std::string& path) const = 0; - - /** - * Create a directory specified by its path - * @param path Path relative to the archive - * @return Result of the operation - */ - virtual ResultCode CreateDirectory(const std::string& path) const = 0; - - /** - * Delete a directory specified by its path - * @param path Path relative to the archive - * @return Result of the operation - */ - virtual ResultCode DeleteDirectory(const Path& path) const = 0; - - /** - * Delete a directory specified by its path and anything under it - * @param path Path relative to the archive - * @return Result of the operation - */ - virtual ResultCode DeleteDirectoryRecursively(const Path& path) const = 0; - - /** - * Rename a File specified by its path - * @param src_path Source path relative to the archive - * @param dest_path Destination path relative to the archive - * @return Result of the operation - */ - virtual ResultCode RenameFile(const std::string& src_path, - const std::string& dest_path) const = 0; - - /** - * Rename a Directory specified by its path - * @param src_path Source path relative to the archive - * @param dest_path Destination path relative to the archive - * @return Result of the operation - */ - virtual ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const = 0; - - /** - * Open a file specified by its path, using the specified mode - * @param path Path relative to the archive - * @param mode Mode to open the file with - * @return Opened file, or error code - */ - virtual ResultVal> OpenFile(const std::string& path, - Mode mode) const = 0; - - /** - * Open a directory specified by its path - * @param path Path relative to the archive - * @return Opened directory, or error code - */ - virtual ResultVal> OpenDirectory( - const std::string& path) const = 0; - - /** - * Get the free space - * @return The number of free bytes in the archive - */ - virtual u64 GetFreeSpaceSize() const = 0; - - /** - * Get the type of the specified path - * @return The type of the specified path or error code - */ - virtual ResultVal GetEntryType(const std::string& path) const = 0; -}; - -class FileSystemFactory : NonCopyable { -public: - virtual ~FileSystemFactory() {} - - /** - * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.) - */ - virtual std::string GetName() const = 0; - - /** - * Tries to open the archive of this type with the specified path - * @param path Path to the archive - * @return An ArchiveBackend corresponding operating specified archive path. - */ - virtual ResultVal> Open(const Path& path) = 0; - - /** - * Deletes the archive contents and then re-creates the base folder - * @param path Path to the archive - * @return ResultCode of the operation, 0 on success - */ - virtual ResultCode Format(const Path& path) = 0; - - /** - * Retrieves the format info about the archive with the specified path - * @param path Path to the archive - * @return Format information about the archive or error code - */ - virtual ResultVal GetFormatInfo(const Path& path) const = 0; -}; - } // namespace FileSys diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp index 46d438aca..15b1fb946 100644 --- a/src/core/file_sys/partition_filesystem.cpp +++ b/src/core/file_sys/partition_filesystem.cpp @@ -6,29 +6,30 @@ #include "common/file_util.h" #include "common/logging/log.h" #include "core/file_sys/partition_filesystem.h" +#include "core/file_sys/vfs_offset.h" #include "core/loader/loader.h" namespace FileSys { -Loader::ResultStatus PartitionFilesystem::Load(const std::string& file_path, size_t offset) { - FileUtil::IOFile file(file_path, "rb"); - if (!file.IsOpen()) - return Loader::ResultStatus::Error; - +PartitionFilesystem::PartitionFilesystem(std::shared_ptr file) { // At least be as large as the header - if (file.GetSize() < sizeof(Header)) - return Loader::ResultStatus::Error; + if (file->GetSize() < sizeof(Header)) { + status = Loader::ResultStatus::Error; + return; + } - file.Seek(offset, SEEK_SET); // For cartridges, HFSs can get very large, so we need to calculate the size up to // the actual content itself instead of just blindly reading in the entire file. Header pfs_header; - if (!file.ReadBytes(&pfs_header, sizeof(Header))) - return Loader::ResultStatus::Error; + if (sizeof(Header) != file->ReadObject(&pfs_header)) { + status = Loader::ResultStatus::Error; + return; + } if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { - return Loader::ResultStatus::ErrorInvalidFormat; + status = Loader::ResultStatus::ErrorInvalidFormat; + return; } bool is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); @@ -38,99 +39,86 @@ Loader::ResultStatus PartitionFilesystem::Load(const std::string& file_path, siz sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size; // Actually read in now... - file.Seek(offset, SEEK_SET); - std::vector file_data(metadata_size); - - if (!file.ReadBytes(file_data.data(), metadata_size)) - return Loader::ResultStatus::Error; + std::vector file_data = file->ReadBytes(metadata_size); - Loader::ResultStatus result = Load(file_data); - if (result != Loader::ResultStatus::Success) - LOG_ERROR(Service_FS, "Failed to load PFS from file {}!", file_path); - - return result; -} + if (file_data.size() != metadata_size) { + status = Loader::ResultStatus::Error; + return; + } -Loader::ResultStatus PartitionFilesystem::Load(const std::vector& file_data, size_t offset) { - size_t total_size = file_data.size() - offset; - if (total_size < sizeof(Header)) - return Loader::ResultStatus::Error; + size_t total_size = file_data.size(); + if (total_size < sizeof(Header)) { + status = Loader::ResultStatus::Error; + return; + } - memcpy(&pfs_header, &file_data[offset], sizeof(Header)); + memcpy(&pfs_header, file_data.data(), sizeof(Header)); if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { - return Loader::ResultStatus::ErrorInvalidFormat; + status = Loader::ResultStatus::ErrorInvalidFormat; + return; } is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); - size_t entries_offset = offset + sizeof(Header); - size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); + size_t entries_offset = sizeof(Header); size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size); + content_offset = strtab_offset + pfs_header.strtab_size; for (u16 i = 0; i < pfs_header.num_entries; i++) { - FileEntry entry; + FSEntry entry; - memcpy(&entry.fs_entry, &file_data[entries_offset + (i * entry_size)], sizeof(FSEntry)); - entry.name = std::string(reinterpret_cast( - &file_data[strtab_offset + entry.fs_entry.strtab_offset])); - pfs_entries.push_back(std::move(entry)); - } + memcpy(&entry, &file_data[entries_offset + (i * entry_size)], sizeof(FSEntry)); + std::string name( + reinterpret_cast(&file_data[strtab_offset + entry.strtab_offset])); - content_offset = strtab_offset + pfs_header.strtab_size; + pfs_files.emplace_back( + std::make_shared(file, entry.size, content_offset + entry.offset, name)); + } - return Loader::ResultStatus::Success; + status = Loader::ResultStatus::Success; } -u32 PartitionFilesystem::GetNumEntries() const { - return pfs_header.num_entries; +Loader::ResultStatus PartitionFilesystem::GetStatus() const { + return status; } -u64 PartitionFilesystem::GetEntryOffset(u32 index) const { - if (index > GetNumEntries()) - return 0; - - return content_offset + pfs_entries[index].fs_entry.offset; +std::vector> PartitionFilesystem::GetFiles() const { + return pfs_files; } -u64 PartitionFilesystem::GetEntrySize(u32 index) const { - if (index > GetNumEntries()) - return 0; - - return pfs_entries[index].fs_entry.size; +std::vector> PartitionFilesystem::GetSubdirectories() const { + return {}; } -std::string PartitionFilesystem::GetEntryName(u32 index) const { - if (index > GetNumEntries()) - return ""; +std::string PartitionFilesystem::GetName() const { + return is_hfs ? "HFS0" : "PFS0"; +} - return pfs_entries[index].name; +std::shared_ptr PartitionFilesystem::GetParentDirectory() const { + // TODO(DarkLordZach): Add support for nested containers. + return nullptr; } -u64 PartitionFilesystem::GetFileOffset(const std::string& name) const { +void PartitionFilesystem::PrintDebugInfo() const { + LOG_DEBUG(Service_FS, "Magic: {:.4}", pfs_header.magic); + LOG_DEBUG(Service_FS, "Files: {}", pfs_header.num_entries); for (u32 i = 0; i < pfs_header.num_entries; i++) { - if (pfs_entries[i].name == name) - return content_offset + pfs_entries[i].fs_entry.offset; + LOG_DEBUG(Service_FS, " > File {}: {} (0x{:X} bytes, at 0x{:X})", i, + pfs_files[i]->GetName(), pfs_files[i]->GetSize(), + dynamic_cast(pfs_files[i].get())->GetOffset()); } - - return 0; } -u64 PartitionFilesystem::GetFileSize(const std::string& name) const { - for (u32 i = 0; i < pfs_header.num_entries; i++) { - if (pfs_entries[i].name == name) - return pfs_entries[i].fs_entry.size; - } +bool PartitionFilesystem::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { + auto iter = std::find(pfs_files.begin(), pfs_files.end(), file); + if (iter == pfs_files.end()) + return false; - return 0; -} + pfs_files[iter - pfs_files.begin()] = pfs_files.back(); + pfs_files.pop_back(); -void PartitionFilesystem::Print() const { - LOG_DEBUG(Service_FS, "Magic: {}", pfs_header.magic); - LOG_DEBUG(Service_FS, "Files: {}", pfs_header.num_entries); - for (u32 i = 0; i < pfs_header.num_entries; i++) { - LOG_DEBUG(Service_FS, " > File {}: {} (0x{:X} bytes, at 0x{:X})", i, - pfs_entries[i].name.c_str(), pfs_entries[i].fs_entry.size, - GetFileOffset(pfs_entries[i].name)); - } + pfs_dirs.emplace_back(dir); + + return true; } } // namespace FileSys diff --git a/src/core/file_sys/partition_filesystem.h b/src/core/file_sys/partition_filesystem.h index 9c5810cf1..9656b40bf 100644 --- a/src/core/file_sys/partition_filesystem.h +++ b/src/core/file_sys/partition_filesystem.h @@ -10,6 +10,7 @@ #include "common/common_funcs.h" #include "common/common_types.h" #include "common/swap.h" +#include "core/file_sys/vfs.h" namespace Loader { enum class ResultStatus; @@ -21,19 +22,19 @@ namespace FileSys { * Helper which implements an interface to parse PFS/HFS filesystems. * Data can either be loaded from a file path or data with an offset into it. */ -class PartitionFilesystem { +class PartitionFilesystem : public ReadOnlyVfsDirectory { public: - Loader::ResultStatus Load(const std::string& file_path, size_t offset = 0); - Loader::ResultStatus Load(const std::vector& file_data, size_t offset = 0); + explicit PartitionFilesystem(std::shared_ptr file); + Loader::ResultStatus GetStatus() const; - u32 GetNumEntries() const; - u64 GetEntryOffset(u32 index) const; - u64 GetEntrySize(u32 index) const; - std::string GetEntryName(u32 index) const; - u64 GetFileOffset(const std::string& name) const; - u64 GetFileSize(const std::string& name) const; + std::vector> GetFiles() const override; + std::vector> GetSubdirectories() const override; + std::string GetName() const override; + std::shared_ptr GetParentDirectory() const override; + void PrintDebugInfo() const; - void Print() const; +protected: + bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; private: struct Header { @@ -72,16 +73,14 @@ private: #pragma pack(pop) - struct FileEntry { - FSEntry fs_entry; - std::string name; - }; + Loader::ResultStatus status; Header pfs_header; bool is_hfs; size_t content_offset; - std::vector pfs_entries; + std::vector pfs_files; + std::vector pfs_dirs; }; } // namespace FileSys diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp index 226811115..63d4b6e4f 100644 --- a/src/core/file_sys/program_metadata.cpp +++ b/src/core/file_sys/program_metadata.cpp @@ -9,40 +9,29 @@ namespace FileSys { -Loader::ResultStatus ProgramMetadata::Load(const std::string& file_path) { - FileUtil::IOFile file(file_path, "rb"); - if (!file.IsOpen()) +Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) { + size_t total_size = static_cast(file->GetSize()); + if (total_size < sizeof(Header)) return Loader::ResultStatus::Error; - std::vector file_data(file.GetSize()); - - if (!file.ReadBytes(file_data.data(), file_data.size())) + // TODO(DarkLordZach): Use ReadObject when Header/AcidHeader becomes trivially copyable. + std::vector npdm_header_data = file->ReadBytes(sizeof(Header)); + if (sizeof(Header) != npdm_header_data.size()) return Loader::ResultStatus::Error; + std::memcpy(&npdm_header, npdm_header_data.data(), sizeof(Header)); - Loader::ResultStatus result = Load(file_data); - if (result != Loader::ResultStatus::Success) - LOG_ERROR(Service_FS, "Failed to load NPDM from file {}!", file_path); - - return result; -} - -Loader::ResultStatus ProgramMetadata::Load(const std::vector file_data, size_t offset) { - size_t total_size = static_cast(file_data.size() - offset); - if (total_size < sizeof(Header)) + std::vector acid_header_data = file->ReadBytes(sizeof(AcidHeader), npdm_header.acid_offset); + if (sizeof(AcidHeader) != acid_header_data.size()) return Loader::ResultStatus::Error; + std::memcpy(&acid_header, acid_header_data.data(), sizeof(AcidHeader)); - size_t header_offset = offset; - memcpy(&npdm_header, &file_data[offset], sizeof(Header)); - - size_t aci_offset = header_offset + npdm_header.aci_offset; - size_t acid_offset = header_offset + npdm_header.acid_offset; - memcpy(&aci_header, &file_data[aci_offset], sizeof(AciHeader)); - memcpy(&acid_header, &file_data[acid_offset], sizeof(AcidHeader)); + if (sizeof(AciHeader) != file->ReadObject(&aci_header, npdm_header.aci_offset)) + return Loader::ResultStatus::Error; - size_t fac_offset = acid_offset + acid_header.fac_offset; - size_t fah_offset = aci_offset + aci_header.fah_offset; - memcpy(&acid_file_access, &file_data[fac_offset], sizeof(FileAccessControl)); - memcpy(&aci_file_access, &file_data[fah_offset], sizeof(FileAccessHeader)); + if (sizeof(FileAccessControl) != file->ReadObject(&acid_file_access, acid_header.fac_offset)) + return Loader::ResultStatus::Error; + if (sizeof(FileAccessHeader) != file->ReadObject(&aci_file_access, aci_header.fah_offset)) + return Loader::ResultStatus::Error; return Loader::ResultStatus::Success; } diff --git a/src/core/file_sys/program_metadata.h b/src/core/file_sys/program_metadata.h index b80a08485..06a7315db 100644 --- a/src/core/file_sys/program_metadata.h +++ b/src/core/file_sys/program_metadata.h @@ -10,6 +10,7 @@ #include "common/bit_field.h" #include "common/common_types.h" #include "common/swap.h" +#include "partition_filesystem.h" namespace Loader { enum class ResultStatus; @@ -37,8 +38,7 @@ enum class ProgramFilePermission : u64 { */ class ProgramMetadata { public: - Loader::ResultStatus Load(const std::string& file_path); - Loader::ResultStatus Load(const std::vector file_data, size_t offset = 0); + Loader::ResultStatus Load(VirtualFile file); bool Is64BitProgram() const; ProgramAddressSpaceType GetAddressSpaceType() const; @@ -51,6 +51,7 @@ public: void Print() const; private: + // TODO(DarkLordZach): BitField is not trivially copyable. struct Header { std::array magic; std::array reserved; @@ -77,6 +78,7 @@ private: static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong"); + // TODO(DarkLordZach): BitField is not trivially copyable. struct AcidHeader { std::array signature; std::array nca_modulus; diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp deleted file mode 100644 index 84ae0d99b..000000000 --- a/src/core/file_sys/romfs_factory.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include -#include "common/common_types.h" -#include "common/logging/log.h" -#include "core/file_sys/romfs_factory.h" -#include "core/file_sys/romfs_filesystem.h" - -namespace FileSys { - -RomFS_Factory::RomFS_Factory(Loader::AppLoader& app_loader) { - // Load the RomFS from the app - if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) { - LOG_ERROR(Service_FS, "Unable to read RomFS!"); - } -} - -ResultVal> RomFS_Factory::Open(const Path& path) { - auto archive = std::make_unique(romfs_file, data_offset, data_size); - return MakeResult>(std::move(archive)); -} - -ResultCode RomFS_Factory::Format(const Path& path) { - LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} - -ResultVal RomFS_Factory::GetFormatInfo(const Path& path) const { - LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} - -} // namespace FileSys diff --git a/src/core/file_sys/romfs_factory.h b/src/core/file_sys/romfs_factory.h deleted file mode 100644 index e0698e642..000000000 --- a/src/core/file_sys/romfs_factory.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include "common/common_types.h" -#include "core/file_sys/filesystem.h" -#include "core/hle/result.h" -#include "core/loader/loader.h" - -namespace FileSys { - -/// File system interface to the RomFS archive -class RomFS_Factory final : public FileSystemFactory { -public: - explicit RomFS_Factory(Loader::AppLoader& app_loader); - - std::string GetName() const override { - return "ArchiveFactory_RomFS"; - } - ResultVal> Open(const Path& path) override; - ResultCode Format(const Path& path) override; - ResultVal GetFormatInfo(const Path& path) const override; - -private: - std::shared_ptr romfs_file; - u64 data_offset; - u64 data_size; -}; - -} // namespace FileSys diff --git a/src/core/file_sys/romfs_filesystem.cpp b/src/core/file_sys/romfs_filesystem.cpp deleted file mode 100644 index 83162622b..000000000 --- a/src/core/file_sys/romfs_filesystem.cpp +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include -#include "common/common_types.h" -#include "common/logging/log.h" -#include "core/file_sys/romfs_filesystem.h" - -namespace FileSys { - -std::string RomFS_FileSystem::GetName() const { - return "RomFS"; -} - -ResultVal> RomFS_FileSystem::OpenFile(const std::string& path, - Mode mode) const { - return MakeResult>( - std::make_unique(romfs_file, data_offset, data_size)); -} - -ResultCode RomFS_FileSystem::DeleteFile(const std::string& path) const { - LOG_CRITICAL(Service_FS, "Attempted to delete a file from an ROMFS archive ({}).", GetName()); - // TODO(bunnei): Use correct error code - return ResultCode(-1); -} - -ResultCode RomFS_FileSystem::RenameFile(const std::string& src_path, - const std::string& dest_path) const { - LOG_CRITICAL(Service_FS, "Attempted to rename a file within an ROMFS archive ({}).", GetName()); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultCode RomFS_FileSystem::DeleteDirectory(const Path& path) const { - LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an ROMFS archive ({}).", - GetName()); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultCode RomFS_FileSystem::DeleteDirectoryRecursively(const Path& path) const { - LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an ROMFS archive ({}).", - GetName()); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultCode RomFS_FileSystem::CreateFile(const std::string& path, u64 size) const { - LOG_CRITICAL(Service_FS, "Attempted to create a file in an ROMFS archive ({}).", GetName()); - // TODO(bunnei): Use correct error code - return ResultCode(-1); -} - -ResultCode RomFS_FileSystem::CreateDirectory(const std::string& path) const { - LOG_CRITICAL(Service_FS, "Attempted to create a directory in an ROMFS archive ({}).", - GetName()); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultCode RomFS_FileSystem::RenameDirectory(const Path& src_path, const Path& dest_path) const { - LOG_CRITICAL(Service_FS, "Attempted to rename a file within an ROMFS archive ({}).", GetName()); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultVal> RomFS_FileSystem::OpenDirectory( - const std::string& path) const { - LOG_WARNING(Service_FS, "Opening Directory in a ROMFS archive"); - return MakeResult>(std::make_unique()); -} - -u64 RomFS_FileSystem::GetFreeSpaceSize() const { - LOG_WARNING(Service_FS, "Attempted to get the free space in an ROMFS archive"); - return 0; -} - -ResultVal RomFS_FileSystem::GetEntryType(const std::string& path) const { - LOG_CRITICAL(Service_FS, "Called within an ROMFS archive (path {}).", path); - // TODO(wwylele): Use correct error code - return ResultCode(-1); -} - -ResultVal RomFS_Storage::Read(const u64 offset, const size_t length, u8* buffer) const { - LOG_TRACE(Service_FS, "called offset={}, length={}", offset, length); - romfs_file->Seek(data_offset + offset, SEEK_SET); - size_t read_length = (size_t)std::min((u64)length, data_size - offset); - - return MakeResult(romfs_file->ReadBytes(buffer, read_length)); -} - -ResultVal RomFS_Storage::Write(const u64 offset, const size_t length, const bool flush, - const u8* buffer) const { - LOG_ERROR(Service_FS, "Attempted to write to ROMFS file"); - // TODO(Subv): Find error code - return MakeResult(0); -} - -u64 RomFS_Storage::GetSize() const { - return data_size; -} - -bool RomFS_Storage::SetSize(const u64 size) const { - LOG_ERROR(Service_FS, "Attempted to set the size of an ROMFS file"); - return false; -} - -} // namespace FileSys diff --git a/src/core/file_sys/romfs_filesystem.h b/src/core/file_sys/romfs_filesystem.h deleted file mode 100644 index ba9d85823..000000000 --- a/src/core/file_sys/romfs_filesystem.h +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include -#include "common/common_types.h" -#include "common/file_util.h" -#include "core/file_sys/directory.h" -#include "core/file_sys/filesystem.h" -#include "core/file_sys/storage.h" -#include "core/hle/result.h" - -namespace FileSys { - -/** - * Helper which implements an interface to deal with Switch .istorage ROMFS images used in some - * archives This should be subclassed by concrete archive types, which will provide the input data - * (load the raw ROMFS archive) and override any required methods - */ -class RomFS_FileSystem : public FileSystemBackend { -public: - RomFS_FileSystem(std::shared_ptr file, u64 offset, u64 size) - : romfs_file(file), data_offset(offset), data_size(size) {} - - std::string GetName() const override; - - ResultVal> OpenFile(const std::string& path, - Mode mode) const override; - ResultCode DeleteFile(const std::string& path) const override; - ResultCode RenameFile(const std::string& src_path, const std::string& dest_path) const override; - ResultCode DeleteDirectory(const Path& path) const override; - ResultCode DeleteDirectoryRecursively(const Path& path) const override; - ResultCode CreateFile(const std::string& path, u64 size) const override; - ResultCode CreateDirectory(const std::string& path) const override; - ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override; - ResultVal> OpenDirectory( - const std::string& path) const override; - u64 GetFreeSpaceSize() const override; - ResultVal GetEntryType(const std::string& path) const override; - -protected: - std::shared_ptr romfs_file; - u64 data_offset; - u64 data_size; -}; - -class RomFS_Storage : public StorageBackend { -public: - RomFS_Storage(std::shared_ptr file, u64 offset, u64 size) - : romfs_file(file), data_offset(offset), data_size(size) {} - - ResultVal Read(u64 offset, size_t length, u8* buffer) const override; - ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; - u64 GetSize() const override; - bool SetSize(u64 size) const override; - bool Close() const override { - return false; - } - void Flush() const override {} - -private: - std::shared_ptr romfs_file; - u64 data_offset; - u64 data_size; -}; - -class ROMFSDirectory : public DirectoryBackend { -public: - u64 Read(const u64 count, Entry* entries) override { - return 0; - } - u64 GetEntryCount() const override { - return 0; - } - bool Close() const override { - return false; - } -}; - -} // namespace FileSys diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp deleted file mode 100644 index d78baf9c3..000000000 --- a/src/core/file_sys/savedata_factory.cpp +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include "common/common_types.h" -#include "common/logging/log.h" -#include "core/core.h" -#include "core/file_sys/disk_filesystem.h" -#include "core/file_sys/savedata_factory.h" -#include "core/hle/kernel/process.h" - -namespace FileSys { - -SaveData_Factory::SaveData_Factory(std::string nand_directory) - : nand_directory(std::move(nand_directory)) {} - -ResultVal> SaveData_Factory::Open(const Path& path) { - std::string save_directory = GetFullPath(); - // Return an error if the save data doesn't actually exist. - if (!FileUtil::IsDirectory(save_directory)) { - // TODO(Subv): Find out correct error code. - return ResultCode(-1); - } - - auto archive = std::make_unique(save_directory); - return MakeResult>(std::move(archive)); -} - -ResultCode SaveData_Factory::Format(const Path& path) { - LOG_WARNING(Service_FS, "Format archive {}", GetName()); - // Create the save data directory. - if (!FileUtil::CreateFullPath(GetFullPath())) { - // TODO(Subv): Find the correct error code. - return ResultCode(-1); - } - - return RESULT_SUCCESS; -} - -ResultVal SaveData_Factory::GetFormatInfo(const Path& path) const { - LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} - -std::string SaveData_Factory::GetFullPath() const { - u64 title_id = Core::CurrentProcess()->program_id; - // TODO(Subv): Somehow obtain this value. - u32 user = 0; - return fmt::format("{}save/{:016X}/{:08X}/", nand_directory, title_id, user); -} - -} // namespace FileSys diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h deleted file mode 100644 index 73a42aab6..000000000 --- a/src/core/file_sys/savedata_factory.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include "common/common_types.h" -#include "core/file_sys/filesystem.h" -#include "core/hle/result.h" - -namespace FileSys { - -/// File system interface to the SaveData archive -class SaveData_Factory final : public FileSystemFactory { -public: - explicit SaveData_Factory(std::string nand_directory); - - std::string GetName() const override { - return "SaveData_Factory"; - } - ResultVal> Open(const Path& path) override; - ResultCode Format(const Path& path) override; - ResultVal GetFormatInfo(const Path& path) const override; - -private: - std::string nand_directory; - - std::string GetFullPath() const; -}; - -} // namespace FileSys diff --git a/src/core/file_sys/sdmc_factory.cpp b/src/core/file_sys/sdmc_factory.cpp deleted file mode 100644 index 2e5ffb764..000000000 --- a/src/core/file_sys/sdmc_factory.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include "common/common_types.h" -#include "common/logging/log.h" -#include "common/string_util.h" -#include "core/core.h" -#include "core/file_sys/disk_filesystem.h" -#include "core/file_sys/sdmc_factory.h" - -namespace FileSys { - -SDMC_Factory::SDMC_Factory(std::string sd_directory) : sd_directory(std::move(sd_directory)) {} - -ResultVal> SDMC_Factory::Open(const Path& path) { - // Create the SD Card directory if it doesn't already exist. - if (!FileUtil::IsDirectory(sd_directory)) { - FileUtil::CreateFullPath(sd_directory); - } - - auto archive = std::make_unique(sd_directory); - return MakeResult>(std::move(archive)); -} - -ResultCode SDMC_Factory::Format(const Path& path) { - LOG_ERROR(Service_FS, "Unimplemented Format archive {}", GetName()); - // TODO(Subv): Find the right error code for this - return ResultCode(-1); -} - -ResultVal SDMC_Factory::GetFormatInfo(const Path& path) const { - LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive {}", GetName()); - // TODO(bunnei): Find the right error code for this - return ResultCode(-1); -} - -} // namespace FileSys diff --git a/src/core/file_sys/sdmc_factory.h b/src/core/file_sys/sdmc_factory.h deleted file mode 100644 index 93becda25..000000000 --- a/src/core/file_sys/sdmc_factory.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include "common/common_types.h" -#include "core/file_sys/filesystem.h" -#include "core/hle/result.h" - -namespace FileSys { - -/// File system interface to the SDCard archive -class SDMC_Factory final : public FileSystemFactory { -public: - explicit SDMC_Factory(std::string sd_directory); - - std::string GetName() const override { - return "SDMC_Factory"; - } - ResultVal> Open(const Path& path) override; - ResultCode Format(const Path& path) override; - ResultVal GetFormatInfo(const Path& path) const override; - -private: - std::string sd_directory; -}; - -} // namespace FileSys diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp new file mode 100644 index 000000000..c99071d6a --- /dev/null +++ b/src/core/file_sys/vfs.cpp @@ -0,0 +1,187 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include "common/file_util.h" +#include "core/file_sys/vfs.h" + +namespace FileSys { + +VfsFile::~VfsFile() = default; + +std::string VfsFile::GetExtension() const { + return FileUtil::GetExtensionFromFilename(GetName()); +} + +VfsDirectory::~VfsDirectory() = default; + +boost::optional VfsFile::ReadByte(size_t offset) const { + u8 out{}; + size_t size = Read(&out, 1, offset); + if (size == 1) + return out; + + return boost::none; +} + +std::vector VfsFile::ReadBytes(size_t size, size_t offset) const { + std::vector out(size); + size_t read_size = Read(out.data(), size, offset); + out.resize(read_size); + return out; +} + +std::vector VfsFile::ReadAllBytes() const { + return ReadBytes(GetSize()); +} + +bool VfsFile::WriteByte(u8 data, size_t offset) { + return Write(&data, 1, offset) == 1; +} + +size_t VfsFile::WriteBytes(std::vector data, size_t offset) { + return Write(data.data(), data.size(), offset); +} + +std::shared_ptr VfsDirectory::GetFileRelative(const std::string& path) const { + auto vec = FileUtil::SplitPathComponents(path); + vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), + vec.end()); + if (vec.empty()) + return nullptr; + if (vec.size() == 1) + return GetFile(vec[0]); + auto dir = GetSubdirectory(vec[0]); + for (size_t i = 1; i < vec.size() - 1; ++i) { + if (dir == nullptr) + return nullptr; + dir = dir->GetSubdirectory(vec[i]); + } + if (dir == nullptr) + return nullptr; + return dir->GetFile(vec.back()); +} + +std::shared_ptr VfsDirectory::GetFileAbsolute(const std::string& path) const { + if (IsRoot()) + return GetFileRelative(path); + + return GetParentDirectory()->GetFileAbsolute(path); +} + +std::shared_ptr VfsDirectory::GetDirectoryRelative(const std::string& path) const { + auto vec = FileUtil::SplitPathComponents(path); + vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), + vec.end()); + if (vec.empty()) + // return std::shared_ptr(this); + return nullptr; + auto dir = GetSubdirectory(vec[0]); + for (size_t i = 1; i < vec.size(); ++i) { + dir = dir->GetSubdirectory(vec[i]); + } + return dir; +} + +std::shared_ptr VfsDirectory::GetDirectoryAbsolute(const std::string& path) const { + if (IsRoot()) + return GetDirectoryRelative(path); + + return GetParentDirectory()->GetDirectoryAbsolute(path); +} + +std::shared_ptr VfsDirectory::GetFile(const std::string& name) const { + const auto& files = GetFiles(); + const auto iter = std::find_if(files.begin(), files.end(), + [&name](const auto& file1) { return name == file1->GetName(); }); + return iter == files.end() ? nullptr : *iter; +} + +std::shared_ptr VfsDirectory::GetSubdirectory(const std::string& name) const { + const auto& subs = GetSubdirectories(); + const auto iter = std::find_if(subs.begin(), subs.end(), + [&name](const auto& file1) { return name == file1->GetName(); }); + return iter == subs.end() ? nullptr : *iter; +} + +bool VfsDirectory::IsRoot() const { + return GetParentDirectory() == nullptr; +} + +size_t VfsDirectory::GetSize() const { + const auto& files = GetFiles(); + const auto file_total = + std::accumulate(files.begin(), files.end(), 0ull, + [](const auto& f1, const auto& f2) { return f1 + f2->GetSize(); }); + + const auto& sub_dir = GetSubdirectories(); + const auto subdir_total = + std::accumulate(sub_dir.begin(), sub_dir.end(), 0ull, + [](const auto& f1, const auto& f2) { return f1 + f2->GetSize(); }); + + return file_total + subdir_total; +} + +bool VfsDirectory::DeleteSubdirectoryRecursive(const std::string& name) { + auto dir = GetSubdirectory(name); + if (dir == nullptr) + return false; + + bool success = true; + for (const auto& file : dir->GetFiles()) { + if (!DeleteFile(file->GetName())) + success = false; + } + + for (const auto& sdir : dir->GetSubdirectories()) { + if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) + success = false; + } + + return success; +} + +bool VfsDirectory::Copy(const std::string& src, const std::string& dest) { + const auto f1 = GetFile(src); + auto f2 = CreateFile(dest); + if (f1 == nullptr || f2 == nullptr) + return false; + + if (!f2->Resize(f1->GetSize())) { + DeleteFile(dest); + return false; + } + + return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize(); +} + +bool ReadOnlyVfsDirectory::IsWritable() const { + return false; +} + +bool ReadOnlyVfsDirectory::IsReadable() const { + return true; +} + +std::shared_ptr ReadOnlyVfsDirectory::CreateSubdirectory(const std::string& name) { + return nullptr; +} + +std::shared_ptr ReadOnlyVfsDirectory::CreateFile(const std::string& name) { + return nullptr; +} + +bool ReadOnlyVfsDirectory::DeleteSubdirectory(const std::string& name) { + return false; +} + +bool ReadOnlyVfsDirectory::DeleteFile(const std::string& name) { + return false; +} + +bool ReadOnlyVfsDirectory::Rename(const std::string& name) { + return false; +} +} // namespace FileSys diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h new file mode 100644 index 000000000..0e30991b4 --- /dev/null +++ b/src/core/file_sys/vfs.h @@ -0,0 +1,220 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include +#include "boost/optional.hpp" +#include "common/common_types.h" +#include "common/file_util.h" + +namespace FileSys { +struct VfsFile; +struct VfsDirectory; + +// Convenience typedefs to use VfsDirectory and VfsFile +using VirtualDir = std::shared_ptr; +using VirtualFile = std::shared_ptr; + +// A class representing a file in an abstract filesystem. +struct VfsFile : NonCopyable { + virtual ~VfsFile(); + + // Retrieves the file name. + virtual std::string GetName() const = 0; + // Retrieves the extension of the file name. + virtual std::string GetExtension() const; + // Retrieves the size of the file. + virtual size_t GetSize() const = 0; + // Resizes the file to new_size. Returns whether or not the operation was successful. + virtual bool Resize(size_t new_size) = 0; + // Gets a pointer to the directory containing this file, returning nullptr if there is none. + virtual std::shared_ptr GetContainingDirectory() const = 0; + + // Returns whether or not the file can be written to. + virtual bool IsWritable() const = 0; + // Returns whether or not the file can be read from. + virtual bool IsReadable() const = 0; + + // The primary method of reading from the file. Reads length bytes into data starting at offset + // into file. Returns number of bytes successfully read. + virtual size_t Read(u8* data, size_t length, size_t offset = 0) const = 0; + // The primary method of writing to the file. Writes length bytes from data starting at offset + // into file. Returns number of bytes successfully written. + virtual size_t Write(const u8* data, size_t length, size_t offset = 0) = 0; + + // Reads exactly one byte at the offset provided, returning boost::none on error. + virtual boost::optional ReadByte(size_t offset = 0) const; + // Reads size bytes starting at offset in file into a vector. + virtual std::vector ReadBytes(size_t size, size_t offset = 0) const; + // Reads all the bytes from the file into a vector. Equivalent to 'file->Read(file->GetSize(), + // 0)' + virtual std::vector ReadAllBytes() const; + + // Reads an array of type T, size number_elements starting at offset. + // Returns the number of bytes (sizeof(T)*number_elements) read successfully. + template + size_t ReadArray(T* data, size_t number_elements, size_t offset = 0) const { + static_assert(std::is_trivially_copyable::value, + "Data type must be trivially copyable."); + + return Read(reinterpret_cast(data), number_elements * sizeof(T), offset); + } + + // Reads size bytes into the memory starting at data starting at offset into the file. + // Returns the number of bytes read successfully. + template + size_t ReadBytes(T* data, size_t size, size_t offset = 0) const { + static_assert(std::is_trivially_copyable::value, + "Data type must be trivially copyable."); + return Read(reinterpret_cast(data), size, offset); + } + + // Reads one object of type T starting at offset in file. + // Returns the number of bytes read successfully (sizeof(T)). + template + size_t ReadObject(T* data, size_t offset = 0) const { + static_assert(std::is_trivially_copyable::value, + "Data type must be trivially copyable."); + return Read(reinterpret_cast(data), sizeof(T), offset); + } + + // Writes exactly one byte to offset in file and retuns whether or not the byte was written + // successfully. + virtual bool WriteByte(u8 data, size_t offset = 0); + // Writes a vector of bytes to offset in file and returns the number of bytes successfully + // written. + virtual size_t WriteBytes(std::vector data, size_t offset = 0); + + // Writes an array of type T, size number_elements to offset in file. + // Returns the number of bytes (sizeof(T)*number_elements) written successfully. + template + size_t WriteArray(T* data, size_t number_elements, size_t offset = 0) { + static_assert(std::is_trivially_copyable::value, + "Data type must be trivially copyable."); + + return Write(data, number_elements * sizeof(T), offset); + } + + // Writes size bytes starting at memory location data to offset in file. + // Returns the number of bytes written successfully. + template + size_t WriteBytes(T* data, size_t size, size_t offset = 0) { + static_assert(std::is_trivially_copyable::value, + "Data type must be trivially copyable."); + return Write(reinterpret_cast(data), size, offset); + } + + // Writes one object of type T to offset in file. + // Returns the number of bytes written successfully (sizeof(T)). + template + size_t WriteObject(const T& data, size_t offset = 0) { + static_assert(std::is_trivially_copyable::value, + "Data type must be trivially copyable."); + return Write(&data, sizeof(T), offset); + } + + // Renames the file to name. Returns whether or not the operation was successsful. + virtual bool Rename(const std::string& name) = 0; +}; + +// A class representing a directory in an abstract filesystem. +struct VfsDirectory : NonCopyable { + virtual ~VfsDirectory(); + + // Retrives the file located at path as if the current directory was root. Returns nullptr if + // not found. + virtual std::shared_ptr GetFileRelative(const std::string& path) const; + // Calls GetFileRelative(path) on the root of the current directory. + virtual std::shared_ptr GetFileAbsolute(const std::string& path) const; + + // Retrives the directory located at path as if the current directory was root. Returns nullptr + // if not found. + virtual std::shared_ptr GetDirectoryRelative(const std::string& path) const; + // Calls GetDirectoryRelative(path) on the root of the current directory. + virtual std::shared_ptr GetDirectoryAbsolute(const std::string& path) const; + + // Returns a vector containing all of the files in this directory. + virtual std::vector> GetFiles() const = 0; + // Returns the file with filename matching name. Returns nullptr if directory dosen't have a + // file with name. + virtual std::shared_ptr GetFile(const std::string& name) const; + + // Returns a vector containing all of the subdirectories in this directory. + virtual std::vector> GetSubdirectories() const = 0; + // Returns the directory with name matching name. Returns nullptr if directory dosen't have a + // directory with name. + virtual std::shared_ptr GetSubdirectory(const std::string& name) const; + + // Returns whether or not the directory can be written to. + virtual bool IsWritable() const = 0; + // Returns whether of not the directory can be read from. + virtual bool IsReadable() const = 0; + + // Returns whether or not the directory is the root of the current file tree. + virtual bool IsRoot() const; + + // Returns the name of the directory. + virtual std::string GetName() const = 0; + // Returns the total size of all files and subdirectories in this directory. + virtual size_t GetSize() const; + // Returns the parent directory of this directory. Returns nullptr if this directory is root or + // has no parent. + virtual std::shared_ptr GetParentDirectory() const = 0; + + // Creates a new subdirectory with name name. Returns a pointer to the new directory or nullptr + // if the operation failed. + virtual std::shared_ptr CreateSubdirectory(const std::string& name) = 0; + // Creates a new file with name name. Returns a pointer to the new file or nullptr if the + // operation failed. + virtual std::shared_ptr CreateFile(const std::string& name) = 0; + + // Deletes the subdirectory with name and returns true on success. + virtual bool DeleteSubdirectory(const std::string& name) = 0; + // Deletes all subdirectories and files of subdirectory with name recirsively and then deletes + // the subdirectory. Returns true on success. + virtual bool DeleteSubdirectoryRecursive(const std::string& name); + // Returnes whether or not the file with name name was deleted successfully. + virtual bool DeleteFile(const std::string& name) = 0; + + // Returns whether or not this directory was renamed to name. + virtual bool Rename(const std::string& name) = 0; + + // Returns whether or not the file with name src was successfully copied to a new file with name + // dest. + virtual bool Copy(const std::string& src, const std::string& dest); + + // Interprets the file with name file instead as a directory of type directory. + // The directory must have a constructor that takes a single argument of type + // std::shared_ptr. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a + // subdirectory in one call. + template + bool InterpretAsDirectory(const std::string& file) { + auto file_p = GetFile(file); + if (file_p == nullptr) + return false; + return ReplaceFileWithSubdirectory(file, std::make_shared(file_p)); + } + +protected: + // Backend for InterpretAsDirectory. + // Removes all references to file and adds a reference to dir in the directory's implementation. + virtual bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) = 0; +}; + +// A convenience partial-implementation of VfsDirectory that stubs out methods that should only work +// if writable. This is to avoid redundant empty methods everywhere. +struct ReadOnlyVfsDirectory : public VfsDirectory { + bool IsWritable() const override; + bool IsReadable() const override; + std::shared_ptr CreateSubdirectory(const std::string& name) override; + std::shared_ptr CreateFile(const std::string& name) override; + bool DeleteSubdirectory(const std::string& name) override; + bool DeleteFile(const std::string& name) override; + bool Rename(const std::string& name) override; +}; +} // namespace FileSys diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp new file mode 100644 index 000000000..288499cb5 --- /dev/null +++ b/src/core/file_sys/vfs_offset.cpp @@ -0,0 +1,92 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/file_sys/vfs_offset.h" + +namespace FileSys { + +OffsetVfsFile::OffsetVfsFile(std::shared_ptr file_, size_t size_, size_t offset_, + const std::string& name_) + : file(file_), offset(offset_), size(size_), name(name_) {} + +std::string OffsetVfsFile::GetName() const { + return name.empty() ? file->GetName() : name; +} + +size_t OffsetVfsFile::GetSize() const { + return size; +} + +bool OffsetVfsFile::Resize(size_t new_size) { + if (offset + new_size < file->GetSize()) { + size = new_size; + } else { + auto res = file->Resize(offset + new_size); + if (!res) + return false; + size = new_size; + } + + return true; +} + +std::shared_ptr OffsetVfsFile::GetContainingDirectory() const { + return file->GetContainingDirectory(); +} + +bool OffsetVfsFile::IsWritable() const { + return file->IsWritable(); +} + +bool OffsetVfsFile::IsReadable() const { + return file->IsReadable(); +} + +size_t OffsetVfsFile::Read(u8* data, size_t length, size_t r_offset) const { + return file->Read(data, TrimToFit(length, r_offset), offset + r_offset); +} + +size_t OffsetVfsFile::Write(const u8* data, size_t length, size_t r_offset) { + return file->Write(data, TrimToFit(length, r_offset), offset + r_offset); +} + +boost::optional OffsetVfsFile::ReadByte(size_t r_offset) const { + if (r_offset < size) + return file->ReadByte(offset + r_offset); + + return boost::none; +} + +std::vector OffsetVfsFile::ReadBytes(size_t r_size, size_t r_offset) const { + return file->ReadBytes(TrimToFit(r_size, r_offset), offset + r_offset); +} + +std::vector OffsetVfsFile::ReadAllBytes() const { + return file->ReadBytes(size, offset); +} + +bool OffsetVfsFile::WriteByte(u8 data, size_t r_offset) { + if (r_offset < size) + return file->WriteByte(data, offset + r_offset); + + return false; +} + +size_t OffsetVfsFile::WriteBytes(std::vector data, size_t r_offset) { + return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset); +} + +bool OffsetVfsFile::Rename(const std::string& name) { + return file->Rename(name); +} + +size_t OffsetVfsFile::GetOffset() const { + return offset; +} + +size_t OffsetVfsFile::TrimToFit(size_t r_size, size_t r_offset) const { + return std::max(std::min(size - r_offset, r_size), 0); +} + +} // namespace FileSys diff --git a/src/core/file_sys/vfs_offset.h b/src/core/file_sys/vfs_offset.h new file mode 100644 index 000000000..adc615b38 --- /dev/null +++ b/src/core/file_sys/vfs_offset.h @@ -0,0 +1,46 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/file_sys/vfs.h" + +namespace FileSys { + +// An implementation of VfsFile that wraps around another VfsFile at a certain offset. +// Similar to seeking to an offset. +// If the file is writable, operations that would write past the end of the offset file will expand +// the size of this wrapper. +struct OffsetVfsFile : public VfsFile { + OffsetVfsFile(std::shared_ptr file, size_t size, size_t offset = 0, + const std::string& new_name = ""); + + std::string GetName() const override; + size_t GetSize() const override; + bool Resize(size_t new_size) override; + std::shared_ptr GetContainingDirectory() const override; + bool IsWritable() const override; + bool IsReadable() const override; + size_t Read(u8* data, size_t length, size_t offset) const override; + size_t Write(const u8* data, size_t length, size_t offset) override; + boost::optional ReadByte(size_t offset) const override; + std::vector ReadBytes(size_t size, size_t offset) const override; + std::vector ReadAllBytes() const override; + bool WriteByte(u8 data, size_t offset) override; + size_t WriteBytes(std::vector data, size_t offset) override; + + bool Rename(const std::string& name) override; + + size_t GetOffset() const; + +private: + size_t TrimToFit(size_t r_size, size_t r_offset) const; + + std::shared_ptr file; + size_t offset; + size_t size; + std::string name; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp new file mode 100644 index 000000000..8b95e8c72 --- /dev/null +++ b/src/core/file_sys/vfs_real.cpp @@ -0,0 +1,168 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/common_paths.h" +#include "common/logging/log.h" +#include "core/file_sys/vfs_real.h" + +namespace FileSys { + +static std::string PermissionsToCharArray(Mode perms) { + std::string out; + switch (perms) { + case Mode::Read: + out += "r"; + break; + case Mode::Write: + out += "r+"; + break; + case Mode::Append: + out += "a"; + break; + } + return out + "b"; +} + +RealVfsFile::RealVfsFile(const std::string& path_, Mode perms_) + : backing(path_, PermissionsToCharArray(perms_).c_str()), path(path_), + parent_path(FileUtil::GetParentPath(path_)), + path_components(FileUtil::SplitPathComponents(path_)), + parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)), + perms(perms_) {} + +std::string RealVfsFile::GetName() const { + return path_components.back(); +} + +size_t RealVfsFile::GetSize() const { + return backing.GetSize(); +} + +bool RealVfsFile::Resize(size_t new_size) { + return backing.Resize(new_size); +} + +std::shared_ptr RealVfsFile::GetContainingDirectory() const { + return std::make_shared(parent_path, perms); +} + +bool RealVfsFile::IsWritable() const { + return perms == Mode::Append || perms == Mode::Write; +} + +bool RealVfsFile::IsReadable() const { + return perms == Mode::Read || perms == Mode::Write; +} + +size_t RealVfsFile::Read(u8* data, size_t length, size_t offset) const { + if (!backing.Seek(offset, SEEK_SET)) + return 0; + return backing.ReadBytes(data, length); +} + +size_t RealVfsFile::Write(const u8* data, size_t length, size_t offset) { + if (!backing.Seek(offset, SEEK_SET)) + return 0; + return backing.WriteBytes(data, length); +} + +bool RealVfsFile::Rename(const std::string& name) { + const auto out = FileUtil::Rename(GetName(), name); + path = parent_path + DIR_SEP + name; + path_components = parent_components; + path_components.push_back(name); + backing = FileUtil::IOFile(path, PermissionsToCharArray(perms).c_str()); + return out; +} + +RealVfsDirectory::RealVfsDirectory(const std::string& path_, Mode perms_) + : path(FileUtil::RemoveTrailingSlash(path_)), parent_path(FileUtil::GetParentPath(path)), + path_components(FileUtil::SplitPathComponents(path)), + parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)), + perms(perms_) { + if (!FileUtil::Exists(path) && (perms == Mode::Write || perms == Mode::Append)) + FileUtil::CreateDir(path); + unsigned size; + if (perms != Mode::Append) { + FileUtil::ForeachDirectoryEntry( + &size, path, + [this](unsigned* entries_out, const std::string& directory, + const std::string& filename) { + std::string full_path = directory + DIR_SEP + filename; + if (FileUtil::IsDirectory(full_path)) + subdirectories.emplace_back( + std::make_shared(full_path, perms)); + else + files.emplace_back(std::make_shared(full_path, perms)); + return true; + }); + } +} + +std::vector> RealVfsDirectory::GetFiles() const { + return std::vector>(files); +} + +std::vector> RealVfsDirectory::GetSubdirectories() const { + return std::vector>(subdirectories); +} + +bool RealVfsDirectory::IsWritable() const { + return perms == Mode::Write || perms == Mode::Append; +} + +bool RealVfsDirectory::IsReadable() const { + return perms == Mode::Read || perms == Mode::Write; +} + +std::string RealVfsDirectory::GetName() const { + return path_components.back(); +} + +std::shared_ptr RealVfsDirectory::GetParentDirectory() const { + if (path_components.size() <= 1) + return nullptr; + + return std::make_shared(parent_path, perms); +} + +std::shared_ptr RealVfsDirectory::CreateSubdirectory(const std::string& name) { + if (!FileUtil::CreateDir(path + DIR_SEP + name)) + return nullptr; + subdirectories.emplace_back(std::make_shared(path + DIR_SEP + name, perms)); + return subdirectories.back(); +} + +std::shared_ptr RealVfsDirectory::CreateFile(const std::string& name) { + if (!FileUtil::CreateEmptyFile(path + DIR_SEP + name)) + return nullptr; + files.emplace_back(std::make_shared(path + DIR_SEP + name, perms)); + return files.back(); +} + +bool RealVfsDirectory::DeleteSubdirectory(const std::string& name) { + return FileUtil::DeleteDirRecursively(path + DIR_SEP + name); +} + +bool RealVfsDirectory::DeleteFile(const std::string& name) { + return FileUtil::Delete(path + DIR_SEP + name); +} + +bool RealVfsDirectory::Rename(const std::string& name) { + return FileUtil::Rename(path, parent_path + DIR_SEP + name); +} + +bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { + auto iter = std::find(files.begin(), files.end(), file); + if (iter == files.end()) + return false; + + files[iter - files.begin()] = files.back(); + files.pop_back(); + + subdirectories.emplace_back(dir); + + return true; +} +} // namespace FileSys diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h new file mode 100644 index 000000000..01717f485 --- /dev/null +++ b/src/core/file_sys/vfs_real.h @@ -0,0 +1,65 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/file_util.h" +#include "core/file_sys/filesystem.h" +#include "core/file_sys/vfs.h" + +namespace FileSys { + +// An implmentation of VfsFile that represents a file on the user's computer. +struct RealVfsFile : public VfsFile { + RealVfsFile(const std::string& name, Mode perms = Mode::Read); + + std::string GetName() const override; + size_t GetSize() const override; + bool Resize(size_t new_size) override; + std::shared_ptr GetContainingDirectory() const override; + bool IsWritable() const override; + bool IsReadable() const override; + size_t Read(u8* data, size_t length, size_t offset) const override; + size_t Write(const u8* data, size_t length, size_t offset) override; + bool Rename(const std::string& name) override; + +private: + FileUtil::IOFile backing; + std::string path; + std::string parent_path; + std::vector path_components; + std::vector parent_components; + Mode perms; +}; + +// An implementation of VfsDirectory that represents a directory on the user's computer. +struct RealVfsDirectory : public VfsDirectory { + RealVfsDirectory(const std::string& path, Mode perms); + + std::vector> GetFiles() const override; + std::vector> GetSubdirectories() const override; + bool IsWritable() const override; + bool IsReadable() const override; + std::string GetName() const override; + std::shared_ptr GetParentDirectory() const override; + std::shared_ptr CreateSubdirectory(const std::string& name) override; + std::shared_ptr CreateFile(const std::string& name) override; + bool DeleteSubdirectory(const std::string& name) override; + bool DeleteFile(const std::string& name) override; + bool Rename(const std::string& name) override; + +protected: + bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; + +private: + std::string path; + std::string parent_path; + std::vector path_components; + std::vector parent_components; + Mode perms; + std::vector> files; + std::vector> subdirectories; +}; + +} // namespace FileSys -- cgit v1.2.3