diff --git a/src/LibHac/Fs/FileSystemClient.cs b/src/LibHac/Fs/FileSystemClient.cs index 8e03fc26..e779b749 100644 --- a/src/LibHac/Fs/FileSystemClient.cs +++ b/src/LibHac/Fs/FileSystemClient.cs @@ -29,6 +29,13 @@ namespace LibHac.Fs Time = timer ?? new StopWatchTimeSpanGenerator(); } + internal FileSystemClient(FileSystemServer fsServer, IFileSystemProxy fsProxy, ITimeSpanGenerator timer) + { + FsSrv = fsServer; + FsProxy = fsProxy; + Time = timer ?? new StopWatchTimeSpanGenerator(); + } + public bool HasFileSystemServer() { return FsSrv != null; diff --git a/src/LibHac/FsService/FileSystemProxy.cs b/src/LibHac/FsService/FileSystemProxy.cs index c888c167..554dac0a 100644 --- a/src/LibHac/FsService/FileSystemProxy.cs +++ b/src/LibHac/FsService/FileSystemProxy.cs @@ -12,10 +12,6 @@ namespace LibHac.FsService public class FileSystemProxy : IFileSystemProxy { private FileSystemProxyCore FsProxyCore { get; } - - /// The client instance to be used for internal operations like save indexer access. - // ReSharper disable once UnusedAutoPropertyAccessor.Local - private FileSystemClient FsClient { get; } private FileSystemServer FsServer { get; } public long CurrentProcess { get; private set; } @@ -25,10 +21,9 @@ namespace LibHac.FsService public FsPath SaveDataRootPath { get; } = default; public bool AutoCreateSaveData { get; private set; } - internal FileSystemProxy(FileSystemProxyCore fsProxyCore, FileSystemClient fsClient, FileSystemServer fsServer) + internal FileSystemProxy(FileSystemProxyCore fsProxyCore, FileSystemServer fsServer) { FsProxyCore = fsProxyCore; - FsClient = fsClient; FsServer = fsServer; CurrentProcess = -1; @@ -1072,6 +1067,32 @@ namespace LibHac.FsService throw new NotImplementedException(); } + public Result CleanUpTemporaryStorage() + { + Result rc = FsProxyCore.OpenSaveDataDirectory(out IFileSystem saveDirFs, SaveDataSpaceId.Temporary, + string.Empty, false); + if (rc.IsFailure()) return rc; + + rc = saveDirFs.CleanDirectoryRecursively("/"); + if (rc.IsFailure()) return rc; + + SaveDataIndexerReader reader = default; + + try + { + rc = FsServer.SaveDataIndexerManager.GetSaveDataIndexer(out reader, SaveDataSpaceId.Temporary); + if (rc.IsFailure()) return rc; + + reader.Indexer.Reset(); + + return Result.Success; + } + finally + { + reader.Dispose(); + } + } + public Result SetSdCardAccessibility(bool isAccessible) { throw new NotImplementedException(); diff --git a/src/LibHac/FsService/FileSystemServer.cs b/src/LibHac/FsService/FileSystemServer.cs index 6b248f96..285076b9 100644 --- a/src/LibHac/FsService/FileSystemServer.cs +++ b/src/LibHac/FsService/FileSystemServer.cs @@ -29,13 +29,15 @@ namespace LibHac.FsService throw new ArgumentException("DeviceOperator must not be null"); ExternalKeySet externalKeySet = config.ExternalKeySet ?? new ExternalKeySet(); - ITimeSpanGenerator timer = config.TimeSpanGenerator ?? new StopWatchTimeSpanGenerator(); + Timer = config.TimeSpanGenerator ?? new StopWatchTimeSpanGenerator(); FsProxyCore = new FileSystemProxyCore(config.FsCreators, externalKeySet, config.DeviceOperator); - FsClient = new FileSystemClient(this, timer); - Timer = timer; + var fsProxy = new FileSystemProxy(FsProxyCore, this); + FsClient = new FileSystemClient(this, fsProxy, Timer); SaveDataIndexerManager = new SaveDataIndexerManager(FsClient, SaveIndexerId); + + fsProxy.CleanUpTemporaryStorage(); } /// @@ -58,7 +60,7 @@ namespace LibHac.FsService public IFileSystemProxy CreateFileSystemProxyService() { - return new FileSystemProxy(FsProxyCore, FsClient, this); + return new FileSystemProxy(FsProxyCore, this); } } diff --git a/src/LibHac/FsService/ISaveDataIndexer.cs b/src/LibHac/FsService/ISaveDataIndexer.cs index 6a625756..3cac0f4b 100644 --- a/src/LibHac/FsService/ISaveDataIndexer.cs +++ b/src/LibHac/FsService/ISaveDataIndexer.cs @@ -5,6 +5,7 @@ namespace LibHac.FsService public interface ISaveDataIndexer { Result Commit(); + Result Reset(); Result Add(out ulong saveDataId, ref SaveDataAttribute key); Result Get(out SaveDataIndexerValue value, ref SaveDataAttribute key); Result AddSystemSaveData(ref SaveDataAttribute key); diff --git a/src/LibHac/FsService/SaveDataIndexer.cs b/src/LibHac/FsService/SaveDataIndexer.cs index e9001f11..bd45d895 100644 --- a/src/LibHac/FsService/SaveDataIndexer.cs +++ b/src/LibHac/FsService/SaveDataIndexer.cs @@ -112,6 +112,23 @@ namespace LibHac.FsService } } + public Result Reset() + { + lock (Locker) + { + IsKvdbLoaded = false; + + Result rc = FsClient.DeleteSaveData(SaveDataId); + + if (rc.IsSuccess() || rc == ResultFs.TargetNotFound) + { + Version++; + } + + return rc; + } + } + public Result Add(out ulong saveDataId, ref SaveDataAttribute key) { saveDataId = default; @@ -525,7 +542,7 @@ namespace LibHac.FsService { // New key was inserted before the iterator's position // increment the position to compensate - if(reader.Position >= index) + if (reader.Position >= index) { reader.Position++; } diff --git a/src/LibHac/FsService/SaveDataIndexerLite.cs b/src/LibHac/FsService/SaveDataIndexerLite.cs index c0e69590..65f8034e 100644 --- a/src/LibHac/FsService/SaveDataIndexerLite.cs +++ b/src/LibHac/FsService/SaveDataIndexerLite.cs @@ -18,6 +18,15 @@ namespace LibHac.FsService return Result.Success; } + public Result Reset() + { + lock (Locker) + { + IsKeyValueSet = false; + return Result.Success; + } + } + public Result Add(out ulong saveDataId, ref SaveDataAttribute key) { lock (Locker)