From 25b94292b334fd14f0ab30db2cf7009348f5ea2b Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Thu, 29 Apr 2021 16:10:55 -0700 Subject: [PATCH] Modify ISaveDataFileSystemCreator --- .../FsCreator/ISaveDataFileSystemCreator.cs | 10 ++--- .../FsCreator/SaveDataFileSystemCreator.cs | 45 ++++++++++++------- .../FsSrv/SaveDataFileSystemServiceImpl.cs | 9 ++-- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/LibHac/FsSrv/FsCreator/ISaveDataFileSystemCreator.cs b/src/LibHac/FsSrv/FsCreator/ISaveDataFileSystemCreator.cs index 25b94cec..3e44fe31 100644 --- a/src/LibHac/FsSrv/FsCreator/ISaveDataFileSystemCreator.cs +++ b/src/LibHac/FsSrv/FsCreator/ISaveDataFileSystemCreator.cs @@ -9,11 +9,11 @@ namespace LibHac.FsSrv.FsCreator { Result CreateFile(out IFile file, IFileSystem sourceFileSystem, ulong saveDataId, OpenMode openMode); - Result Create(out IFileSystem fileSystem, - out ReferenceCountedDisposable extraDataAccessor, IFileSystem sourceFileSystem, - ulong saveDataId, bool allowDirectorySaveData, bool useDeviceUniqueMac, SaveDataType type, - ISaveDataCommitTimeStampGetter timeStampGetter); + Result Create(out ReferenceCountedDisposable fileSystem, + out ReferenceCountedDisposable extraDataAccessor, + ReferenceCountedDisposable sourceFileSystem, ulong saveDataId, bool allowDirectorySaveData, + bool useDeviceUniqueMac, SaveDataType type, ISaveDataCommitTimeStampGetter timeStampGetter); void SetSdCardEncryptionSeed(ReadOnlySpan seed); } -} +} \ No newline at end of file diff --git a/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs b/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs index c5a03bfd..32491003 100644 --- a/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs +++ b/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs @@ -28,16 +28,16 @@ namespace LibHac.FsSrv.FsCreator throw new NotImplementedException(); } - public Result Create(out IFileSystem fileSystem, - out ReferenceCountedDisposable extraDataAccessor, IFileSystem sourceFileSystem, - ulong saveDataId, bool allowDirectorySaveData, bool useDeviceUniqueMac, SaveDataType type, - ISaveDataCommitTimeStampGetter timeStampGetter) + public Result Create(out ReferenceCountedDisposable fileSystem, + out ReferenceCountedDisposable extraDataAccessor, + ReferenceCountedDisposable sourceFileSystem, ulong saveDataId, bool allowDirectorySaveData, + bool useDeviceUniqueMac, SaveDataType type, ISaveDataCommitTimeStampGetter timeStampGetter) { UnsafeHelpers.SkipParamInit(out fileSystem, out extraDataAccessor); var saveDataPath = $"/{saveDataId:x16}".ToU8String(); - Result rc = sourceFileSystem.GetEntryType(out DirectoryEntryType entryType, saveDataPath); + Result rc = sourceFileSystem.Target.GetEntryType(out DirectoryEntryType entryType, saveDataPath); if (rc.IsFailure()) { return ResultFs.PathNotFound.Includes(rc) ? ResultFs.TargetNotFound.LogConverted(rc) : rc; @@ -48,30 +48,43 @@ namespace LibHac.FsSrv.FsCreator case DirectoryEntryType.Directory: if (!allowDirectorySaveData) return ResultFs.InvalidSaveDataEntryType.Log(); - rc = SubdirectoryFileSystem.CreateNew(out SubdirectoryFileSystem subDirFs, sourceFileSystem, - saveDataPath); - if (rc.IsFailure()) return rc; + var subDirFs = new SubdirectoryFileSystem(ref sourceFileSystem); + + rc = subDirFs.Initialize(saveDataPath); + if (rc.IsFailure()) + { + subDirFs.Dispose(); + return rc; + } bool isPersistentSaveData = type != SaveDataType.Temporary; bool isUserSaveData = type == SaveDataType.Account || type == SaveDataType.Device; rc = DirectorySaveDataFileSystem.CreateNew(out DirectorySaveDataFileSystem saveFs, subDirFs, - isPersistentSaveData, isUserSaveData, true); + timeStampGetter, _randomGenerator, isPersistentSaveData, isUserSaveData, true, null); if (rc.IsFailure()) return rc; - fileSystem = saveFs; + ReferenceCountedDisposable sharedSaveFs = null; + try + { + sharedSaveFs = new ReferenceCountedDisposable(saveFs); + fileSystem = sharedSaveFs.AddReference(); + extraDataAccessor = sharedSaveFs.AddReference(); - // Todo: Dummy ISaveDataExtraDataAccessor - - return Result.Success; + return Result.Success; + } + finally + { + sharedSaveFs?.Dispose(); + } case DirectoryEntryType.File: - rc = sourceFileSystem.OpenFile(out IFile saveDataFile, saveDataPath, OpenMode.ReadWrite); + rc = sourceFileSystem.Target.OpenFile(out IFile saveDataFile, saveDataPath, OpenMode.ReadWrite); if (rc.IsFailure()) return rc; var saveDataStorage = new DisposingFileStorage(saveDataFile); - fileSystem = new SaveDataFileSystem(_keySet, saveDataStorage, IntegrityCheckLevel.ErrorOnInvalid, - false); + fileSystem = new ReferenceCountedDisposable(new SaveDataFileSystem(_keySet, + saveDataStorage, IntegrityCheckLevel.ErrorOnInvalid, false)); // Todo: ISaveDataExtraDataAccessor diff --git a/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs b/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs index e3047a57..f67ceecd 100644 --- a/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs @@ -146,18 +146,18 @@ namespace LibHac.FsSrv // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (saveDataFs is null) { + ReferenceCountedDisposable saveFs = null; ReferenceCountedDisposable extraDataAccessor = null; try { // Todo: Update ISaveDataFileSystemCreator bool useDeviceUniqueMac = IsDeviceUniqueMac(spaceId); - rc = _config.SaveFsCreator.Create(out IFileSystem saveFs, - out extraDataAccessor, saveDirectoryFs.Target, saveDataId, - allowDirectorySaveData, useDeviceUniqueMac, type, _timeStampGetter); + rc = _config.SaveFsCreator.Create(out saveFs, out extraDataAccessor, saveDirectoryFs, + saveDataId, allowDirectorySaveData, useDeviceUniqueMac, type, _timeStampGetter); if (rc.IsFailure()) return rc; - saveDataFs = new ReferenceCountedDisposable(saveFs); + saveDataFs = Shared.Move(ref saveFs); if (cacheExtraData) { @@ -166,6 +166,7 @@ namespace LibHac.FsSrv } finally { + saveFs?.Dispose(); extraDataAccessor?.Dispose(); } }