From 00851a5ef442947c4237f32e063c37e7751db3ed Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Fri, 19 Jan 2018 22:34:48 -0500
Subject: [PATCH] file_sys: Cleanup to better match Switch file system
 constructs.

file_sys: Add factory class for RomFS file system.
---
 src/core/CMakeLists.txt                       | 14 ++++---
 .../{directory_backend.h => directory.h}      |  2 +-
 .../{archive_backend.cpp => filesystem.cpp}   |  4 +-
 .../{archive_backend.h => filesystem.h}       | 18 ++++-----
 src/core/file_sys/path_parser.h               |  2 +-
 src/core/file_sys/romfs_factory.cpp           | 39 ++++++++++++++++++
 src/core/file_sys/romfs_factory.h             | 35 ++++++++++++++++
 ...romfs_archive.cpp => romfs_filesystem.cpp} | 40 +++++++++----------
 .../{romfs_archive.h => romfs_filesystem.h}   | 18 ++++-----
 .../file_sys/{file_backend.h => storage.h}    | 27 ++++++-------
 10 files changed, 136 insertions(+), 63 deletions(-)
 rename src/core/file_sys/{directory_backend.h => directory.h} (98%)
 rename src/core/file_sys/{archive_backend.cpp => filesystem.cpp} (97%)
 rename src/core/file_sys/{archive_backend.h => filesystem.h} (92%)
 create mode 100644 src/core/file_sys/romfs_factory.cpp
 create mode 100644 src/core/file_sys/romfs_factory.h
 rename src/core/file_sys/{romfs_archive.cpp => romfs_filesystem.cpp} (62%)
 rename src/core/file_sys/{romfs_archive.h => romfs_filesystem.h} (80%)
 rename src/core/file_sys/{file_backend.h => storage.h} (86%)

diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index b2dcc039a..5ff1311a2 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -6,15 +6,17 @@ add_library(core STATIC
     core.h
     core_timing.cpp
     core_timing.h
-    file_sys/archive_backend.cpp
-    file_sys/archive_backend.h
-    file_sys/directory_backend.h
+    file_sys/directory.h
     file_sys/errors.h
-    file_sys/file_backend.h
+    file_sys/filesystem.cpp
+    file_sys/filesystem.h
     file_sys/path_parser.cpp
     file_sys/path_parser.h
-    file_sys/romfs_archive.cpp
-    file_sys/romfs_archive.h
+    file_sys/romfs_factory.cpp
+    file_sys/romfs_factory.h
+    file_sys/romfs_filesystem.cpp
+    file_sys/romfs_filesystem.h
+    file_sys/storage.h
     frontend/emu_window.cpp
     frontend/emu_window.h
     frontend/framebuffer_layout.cpp
diff --git a/src/core/file_sys/directory_backend.h b/src/core/file_sys/directory.h
similarity index 98%
rename from src/core/file_sys/directory_backend.h
rename to src/core/file_sys/directory.h
index 0c93f2074..5a40bf472 100644
--- a/src/core/file_sys/directory_backend.h
+++ b/src/core/file_sys/directory.h
@@ -1,4 +1,4 @@
-// Copyright 2014 Citra Emulator Project
+// Copyright 2018 yuzu emulator team
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
diff --git a/src/core/file_sys/archive_backend.cpp b/src/core/file_sys/filesystem.cpp
similarity index 97%
rename from src/core/file_sys/archive_backend.cpp
rename to src/core/file_sys/filesystem.cpp
index fc472b44f..82fdb3c46 100644
--- a/src/core/file_sys/archive_backend.cpp
+++ b/src/core/file_sys/filesystem.cpp
@@ -1,4 +1,4 @@
-// Copyright 2015 Citra Emulator Project
+// Copyright 2018 yuzu emulator team
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
@@ -7,7 +7,7 @@
 #include <sstream>
 #include "common/logging/log.h"
 #include "common/string_util.h"
-#include "core/file_sys/archive_backend.h"
+#include "core/file_sys/filesystem.h"
 #include "core/memory.h"
 
 namespace FileSys {
diff --git a/src/core/file_sys/archive_backend.h b/src/core/file_sys/filesystem.h
similarity index 92%
rename from src/core/file_sys/archive_backend.h
rename to src/core/file_sys/filesystem.h
index 2255bee42..eb3d9c4d6 100644
--- a/src/core/file_sys/archive_backend.h
+++ b/src/core/file_sys/filesystem.h
@@ -1,4 +1,4 @@
-// Copyright 2014 Citra Emulator Project
+// Copyright 2018 yuzu emulator team
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
@@ -15,7 +15,7 @@
 
 namespace FileSys {
 
-class FileBackend;
+class StorageBackend;
 class DirectoryBackend;
 
 // Path string type
@@ -71,9 +71,9 @@ struct ArchiveFormatInfo {
 };
 static_assert(std::is_pod<ArchiveFormatInfo>::value, "ArchiveFormatInfo is not POD");
 
-class ArchiveBackend : NonCopyable {
+class FileSystemBackend : NonCopyable {
 public:
-    virtual ~ArchiveBackend() {}
+    virtual ~FileSystemBackend() {}
 
     /**
      * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
@@ -138,8 +138,8 @@ public:
      * @param mode Mode to open the file with
      * @return Opened file, or error code
      */
-    virtual ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
-                                                             const Mode& mode) const = 0;
+    virtual ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const Path& path,
+                                                          const Mode& mode) const = 0;
 
     /**
      * Open a directory specified by its path
@@ -155,9 +155,9 @@ public:
     virtual u64 GetFreeSpaceSize() const = 0;
 };
 
-class ArchiveFactory : NonCopyable {
+class FileSystemFactory : NonCopyable {
 public:
-    virtual ~ArchiveFactory() {}
+    virtual ~FileSystemFactory() {}
 
     /**
      * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
@@ -169,7 +169,7 @@ public:
      * @param path Path to the archive
      * @return An ArchiveBackend corresponding operating specified archive path.
      */
-    virtual ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) = 0;
+    virtual ResultVal<std::unique_ptr<FileSystemBackend>> Open(const Path& path) = 0;
 
     /**
      * Deletes the archive contents and then re-creates the base folder
diff --git a/src/core/file_sys/path_parser.h b/src/core/file_sys/path_parser.h
index b9f52f65d..184f59d55 100644
--- a/src/core/file_sys/path_parser.h
+++ b/src/core/file_sys/path_parser.h
@@ -6,7 +6,7 @@
 
 #include <string>
 #include <vector>
-#include "core/file_sys/archive_backend.h"
+#include "core/file_sys/filesystem.h"
 
 namespace FileSys {
 
diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp
new file mode 100644
index 000000000..590c2acb3
--- /dev/null
+++ b/src/core/file_sys/romfs_factory.cpp
@@ -0,0 +1,39 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <algorithm>
+#include <memory>
+#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<std::unique_ptr<FileSystemBackend>> RomFS_Factory::Open(const Path& path) {
+    auto archive = std::make_unique<RomFS_FileSystem>(romfs_file, data_offset, data_size);
+    return MakeResult<std::unique_ptr<FileSystemBackend>>(std::move(archive));
+}
+
+ResultCode RomFS_Factory::Format(const Path& path,
+                                        const FileSys::ArchiveFormatInfo& format_info) {
+    LOG_ERROR(Service_FS, "Unimplemented Format archive %s", GetName().c_str());
+    // TODO(bunnei): Find the right error code for this
+    return ResultCode(-1);
+}
+
+ResultVal<ArchiveFormatInfo> RomFS_Factory::GetFormatInfo(const Path& path) const {
+    LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
+    // 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
new file mode 100644
index 000000000..10ea13966
--- /dev/null
+++ b/src/core/file_sys/romfs_factory.h
@@ -0,0 +1,35 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <memory>
+#include <string>
+#include <vector>
+#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<std::unique_ptr<FileSystemBackend>> Open(const Path& path) override;
+    ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
+    ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
+
+private:
+    std::shared_ptr<FileUtil::IOFile> romfs_file;
+    u64 data_offset;
+    u64 data_size;
+};
+
+} // namespace FileSys
diff --git a/src/core/file_sys/romfs_archive.cpp b/src/core/file_sys/romfs_filesystem.cpp
similarity index 62%
rename from src/core/file_sys/romfs_archive.cpp
rename to src/core/file_sys/romfs_filesystem.cpp
index 0d93fccd4..5b5c5a73e 100644
--- a/src/core/file_sys/romfs_archive.cpp
+++ b/src/core/file_sys/romfs_filesystem.cpp
@@ -6,79 +6,79 @@
 #include <memory>
 #include "common/common_types.h"
 #include "common/logging/log.h"
-#include "core/file_sys/romfs_archive.h"
+#include "core/file_sys/romfs_filesystem.h"
 
 namespace FileSys {
 
-std::string ROMFSArchive::GetName() const {
+std::string RomFS_FileSystem::GetName() const {
     return "RomFS";
 }
 
-ResultVal<std::unique_ptr<FileBackend>> ROMFSArchive::OpenFile(const Path& path,
-                                                               const Mode& mode) const {
-    return MakeResult<std::unique_ptr<FileBackend>>(
-        std::make_unique<ROMFSFile>(romfs_file, data_offset, data_size));
+ResultVal<std::unique_ptr<StorageBackend>> RomFS_FileSystem::OpenFile(const Path& path,
+                                                                      const Mode& mode) const {
+    return MakeResult<std::unique_ptr<StorageBackend>>(
+        std::make_unique<RomFS_Storage>(romfs_file, data_offset, data_size));
 }
 
-ResultCode ROMFSArchive::DeleteFile(const Path& path) const {
+ResultCode RomFS_FileSystem::DeleteFile(const Path& path) const {
     LOG_CRITICAL(Service_FS, "Attempted to delete a file from an ROMFS archive (%s).",
                  GetName().c_str());
     // TODO(bunnei): Use correct error code
     return ResultCode(-1);
 }
 
-ResultCode ROMFSArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
+ResultCode RomFS_FileSystem::RenameFile(const Path& src_path, const Path& dest_path) const {
     LOG_CRITICAL(Service_FS, "Attempted to rename a file within an ROMFS archive (%s).",
                  GetName().c_str());
     // TODO(wwylele): Use correct error code
     return ResultCode(-1);
 }
 
-ResultCode ROMFSArchive::DeleteDirectory(const Path& path) const {
+ResultCode RomFS_FileSystem::DeleteDirectory(const Path& path) const {
     LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an ROMFS archive (%s).",
                  GetName().c_str());
     // TODO(wwylele): Use correct error code
     return ResultCode(-1);
 }
 
-ResultCode ROMFSArchive::DeleteDirectoryRecursively(const Path& path) const {
+ResultCode RomFS_FileSystem::DeleteDirectoryRecursively(const Path& path) const {
     LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an ROMFS archive (%s).",
                  GetName().c_str());
     // TODO(wwylele): Use correct error code
     return ResultCode(-1);
 }
 
-ResultCode ROMFSArchive::CreateFile(const Path& path, u64 size) const {
+ResultCode RomFS_FileSystem::CreateFile(const Path& path, u64 size) const {
     LOG_CRITICAL(Service_FS, "Attempted to create a file in an ROMFS archive (%s).",
                  GetName().c_str());
     // TODO(bunnei): Use correct error code
     return ResultCode(-1);
 }
 
-ResultCode ROMFSArchive::CreateDirectory(const Path& path) const {
+ResultCode RomFS_FileSystem::CreateDirectory(const Path& path) const {
     LOG_CRITICAL(Service_FS, "Attempted to create a directory in an ROMFS archive (%s).",
                  GetName().c_str());
     // TODO(wwylele): Use correct error code
     return ResultCode(-1);
 }
 
-ResultCode ROMFSArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
+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 (%s).",
                  GetName().c_str());
     // TODO(wwylele): Use correct error code
     return ResultCode(-1);
 }
 
-ResultVal<std::unique_ptr<DirectoryBackend>> ROMFSArchive::OpenDirectory(const Path& path) const {
+ResultVal<std::unique_ptr<DirectoryBackend>> RomFS_FileSystem::OpenDirectory(const Path& path) const {
     return MakeResult<std::unique_ptr<DirectoryBackend>>(std::make_unique<ROMFSDirectory>());
 }
 
-u64 ROMFSArchive::GetFreeSpaceSize() const {
+u64 RomFS_FileSystem::GetFreeSpaceSize() const {
     LOG_WARNING(Service_FS, "Attempted to get the free space in an ROMFS archive");
     return 0;
 }
 
-ResultVal<size_t> ROMFSFile::Read(const u64 offset, const size_t length, u8* buffer) const {
+ResultVal<size_t> RomFS_Storage::Read(const u64 offset, const size_t length, u8* buffer) const {
     LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length);
     romfs_file->Seek(data_offset + offset, SEEK_SET);
     size_t read_length = (size_t)std::min((u64)length, data_size - offset);
@@ -86,18 +86,18 @@ ResultVal<size_t> ROMFSFile::Read(const u64 offset, const size_t length, u8* buf
     return MakeResult<size_t>(romfs_file->ReadBytes(buffer, read_length));
 }
 
-ResultVal<size_t> ROMFSFile::Write(const u64 offset, const size_t length, const bool flush,
-                                   const u8* buffer) const {
+ResultVal<size_t> 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<size_t>(0);
 }
 
-u64 ROMFSFile::GetSize() const {
+u64 RomFS_Storage::GetSize() const {
     return data_size;
 }
 
-bool ROMFSFile::SetSize(const u64 size) const {
+bool RomFS_Storage::SetSize(const u64 size) const {
     LOG_ERROR(Service_FS, "Attempted to set the size of an ROMFS file");
     return false;
 }
diff --git a/src/core/file_sys/romfs_archive.h b/src/core/file_sys/romfs_filesystem.h
similarity index 80%
rename from src/core/file_sys/romfs_archive.h
rename to src/core/file_sys/romfs_filesystem.h
index 2b6c573ba..900ea567a 100644
--- a/src/core/file_sys/romfs_archive.h
+++ b/src/core/file_sys/romfs_filesystem.h
@@ -10,9 +10,9 @@
 #include <vector>
 #include "common/common_types.h"
 #include "common/file_util.h"
-#include "core/file_sys/archive_backend.h"
-#include "core/file_sys/directory_backend.h"
-#include "core/file_sys/file_backend.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 {
@@ -22,15 +22,15 @@ namespace FileSys {
  * 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 ROMFSArchive : public ArchiveBackend {
+class RomFS_FileSystem : public FileSystemBackend {
 public:
-    ROMFSArchive(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
+    RomFS_FileSystem(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
         : romfs_file(file), data_offset(offset), data_size(size) {}
 
     std::string GetName() const override;
 
-    ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
-                                                     const Mode& mode) const override;
+    ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const Path& path,
+                                                        const Mode& mode) const override;
     ResultCode DeleteFile(const Path& path) const override;
     ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override;
     ResultCode DeleteDirectory(const Path& path) const override;
@@ -47,9 +47,9 @@ protected:
     u64 data_size;
 };
 
-class ROMFSFile : public FileBackend {
+class RomFS_Storage : public StorageBackend {
 public:
-    ROMFSFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
+    RomFS_Storage(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
         : romfs_file(file), data_offset(offset), data_size(size) {}
 
     ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/storage.h
similarity index 86%
rename from src/core/file_sys/file_backend.h
rename to src/core/file_sys/storage.h
index 5e7c2bab4..2a6811831 100644
--- a/src/core/file_sys/file_backend.h
+++ b/src/core/file_sys/storage.h
@@ -1,4 +1,4 @@
-// Copyright 2014 Citra Emulator Project
+// Copyright 2018 yuzu emulator team
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
@@ -8,15 +8,12 @@
 #include "common/common_types.h"
 #include "core/hle/result.h"
 
-////////////////////////////////////////////////////////////////////////////////////////////////////
-// FileSys namespace
-
 namespace FileSys {
 
-class FileBackend : NonCopyable {
+class StorageBackend : NonCopyable {
 public:
-    FileBackend() {}
-    virtual ~FileBackend() {}
+    StorageBackend() {}
+    virtual ~StorageBackend() {}
 
     /**
      * Read data from the file
@@ -39,10 +36,9 @@ public:
                                     const u8* buffer) const = 0;
 
     /**
-     * Get the size of the file in bytes
-     * @return Size of the file in bytes
+     * Flushes the file
      */
-    virtual u64 GetSize() const = 0;
+    virtual void Flush() const = 0;
 
     /**
      * Set the size of the file in bytes
@@ -51,16 +47,17 @@ public:
      */
     virtual bool SetSize(u64 size) const = 0;
 
+    /**
+     * Get the size of the file in bytes
+     * @return Size of the file in bytes
+     */
+    virtual u64 GetSize() const = 0;
+
     /**
      * Close the file
      * @return true if the file closed correctly
      */
     virtual bool Close() const = 0;
-
-    /**
-     * Flushes the file
-     */
-    virtual void Flush() const = 0;
 };
 
 } // namespace FileSys