From 43af31836ebe923f0bd34d85b74788e78d04b4e2 Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Tue, 16 Apr 2019 14:32:18 -0400
Subject: [PATCH] filesystem: Pass Size Getter functions to IFileSystem for
 sizes

---
 .../hle/service/filesystem/filesystem.cpp     | 12 -------
 src/core/hle/service/filesystem/filesystem.h  |  6 ----
 src/core/hle/service/filesystem/fsp_srv.cpp   | 33 +++++++++++++++++--
 3 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/src/core/hle/service/filesystem/filesystem.cpp b/src/core/hle/service/filesystem/filesystem.cpp
index adc1917aa..d8debf612 100644
--- a/src/core/hle/service/filesystem/filesystem.cpp
+++ b/src/core/hle/service/filesystem/filesystem.cpp
@@ -29,11 +29,6 @@
 
 namespace Service::FileSystem {
 
-// Size of emulated sd card free space, reported in bytes.
-// Just using 32GB because thats reasonable
-// TODO(DarkLordZach): Eventually make this configurable in settings.
-constexpr u64 EMULATED_SD_REPORTED_SIZE = 32000000000;
-
 // A default size for normal/journal save data size if application control metadata cannot be found.
 // This should be large enough to satisfy even the most extreme requirements (~4.2GB)
 constexpr u64 SUFFICIENT_SAVE_DATA_SIZE = 0xF0000000;
@@ -227,13 +222,6 @@ ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const s
     return MakeResult(dir);
 }
 
-u64 VfsDirectoryServiceWrapper::GetFreeSpaceSize() const {
-    if (backing->IsWritable())
-        return EMULATED_SD_REPORTED_SIZE;
-
-    return 0;
-}
-
 ResultVal<FileSys::EntryType> VfsDirectoryServiceWrapper::GetEntryType(
     const std::string& path_) const {
     std::string path(FileUtil::SanitizePath(path_));
diff --git a/src/core/hle/service/filesystem/filesystem.h b/src/core/hle/service/filesystem/filesystem.h
index aa4e437ac..2eb3a641d 100644
--- a/src/core/hle/service/filesystem/filesystem.h
+++ b/src/core/hle/service/filesystem/filesystem.h
@@ -215,12 +215,6 @@ public:
      */
     ResultVal<FileSys::VirtualDir> OpenDirectory(const std::string& path);
 
-    /**
-     * Get the free space
-     * @return The number of free bytes in the archive
-     */
-    u64 GetFreeSpaceSize() const;
-
     /**
      * Get the type of the specified path
      * @return The type of the specified path or error code
diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp
index d3cd46a9b..85f8e4a63 100644
--- a/src/core/hle/service/filesystem/fsp_srv.cpp
+++ b/src/core/hle/service/filesystem/fsp_srv.cpp
@@ -30,6 +30,18 @@
 
 namespace Service::FileSystem {
 
+struct SizeGetter {
+    std::function<u64()> free;
+    std::function<u64()> total;
+
+    static SizeGetter FromStorageId(const FileSystemController& fsc, FileSys::StorageId id) {
+        return {
+            [&fsc, id] { return fsc.GetFreeSpaceSize(id); },
+            [&fsc, id] { return fsc.GetTotalSpaceSize(id); },
+        };
+    }
+};
+
 enum class FileSystemType : u8 {
     Invalid0 = 0,
     Invalid1 = 1,
@@ -289,8 +301,8 @@ private:
 
 class IFileSystem final : public ServiceFramework<IFileSystem> {
 public:
-    explicit IFileSystem(FileSys::VirtualDir backend)
-        : ServiceFramework("IFileSystem"), backend(std::move(backend)) {
+    explicit IFileSystem(FileSys::VirtualDir backend, SizeGetter size)
+        : ServiceFramework("IFileSystem"), backend(std::move(backend)), size(std::move(size)) {
         static const FunctionInfo functions[] = {
             {0, &IFileSystem::CreateFile, "CreateFile"},
             {1, &IFileSystem::DeleteFile, "DeleteFile"},
@@ -467,8 +479,25 @@ public:
         rb.Push(RESULT_SUCCESS);
     }
 
+    void GetFreeSpaceSize(Kernel::HLERequestContext& ctx) {
+        LOG_DEBUG(Service_FS, "called");
+
+        IPC::ResponseBuilder rb{ctx, 4};
+        rb.Push(RESULT_SUCCESS);
+        rb.Push(size.free());
+    }
+
+    void GetTotalSpaceSize(Kernel::HLERequestContext& ctx) {
+        LOG_DEBUG(Service_FS, "called");
+
+        IPC::ResponseBuilder rb{ctx, 4};
+        rb.Push(RESULT_SUCCESS);
+        rb.Push(size.total());
+    }
+
 private:
     VfsDirectoryServiceWrapper backend;
+    SizeGetter size;
 };
 
 class ISaveDataInfoReader final : public ServiceFramework<ISaveDataInfoReader> {