From 5f85c0b8e2292fbe48b8765e805818ea10613e43 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 7 Aug 2021 21:46:57 -0700 Subject: [PATCH] Use using statements to dispose Fs.Path variables --- src/LibHac/Fs/Common/Path.cs | 44 ++++- src/LibHac/Fs/Fsa/FileSystemAccessor.cs | 88 ++++------ src/LibHac/Fs/Fsa/IFileSystem.cs | 4 +- src/LibHac/FsSrv/BaseFileSystemService.cs | 2 +- src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs | 17 +- src/LibHac/FsSrv/FileSystemProxyImpl.cs | 3 +- .../FsCreator/EmulatedBisFileSystemCreator.cs | 2 +- .../EmulatedSdCardFileSystemCreator.cs | 2 +- .../FsCreator/SaveDataFileSystemCreator.cs | 5 +- .../FsSrv/Impl/FileSystemInterfaceAdapter.cs | 85 ++++----- src/LibHac/FsSrv/Impl/MultiCommitManager.cs | 36 ++-- src/LibHac/FsSrv/NcaFileSystemService.cs | 35 ++-- src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs | 25 ++- src/LibHac/FsSrv/SaveDataFileSystemService.cs | 10 +- .../FsSrv/SaveDataFileSystemServiceImpl.cs | 62 +++---- .../FsSystem/ConcatenationFileSystem.cs | 51 +++--- .../FsSystem/DirectorySaveDataFileSystem.cs | 127 ++++++-------- src/LibHac/FsSystem/FileSystemExtensions.cs | 20 +-- src/LibHac/FsSystem/LocalFileSystem.cs | 9 +- src/LibHac/FsSystem/SubdirectoryFileSystem.cs | 85 ++++----- src/LibHac/FsSystem/Utility12.cs | 84 ++++----- src/LibHac/LibHac.csproj | 11 ++ src/LibHac/SwitchFs.cs | 16 +- src/hactoolnet/ProcessSave.cs | 2 +- tests/LibHac.Tests/Fs/FsaExtensions.cs | 164 +++++++----------- .../Fs/SubdirectoryFileSystemTests.cs | 8 +- 26 files changed, 442 insertions(+), 555 deletions(-) diff --git a/src/LibHac/Fs/Common/Path.cs b/src/LibHac/Fs/Common/Path.cs index ad12394e..2b801bab 100644 --- a/src/LibHac/Fs/Common/Path.cs +++ b/src/LibHac/Fs/Common/Path.cs @@ -5,6 +5,7 @@ using LibHac.Common; using LibHac.Diag; using LibHac.Util; using static LibHac.Fs.StringTraits; +using static InlineIL.IL.Emit; // ReSharper disable once CheckNamespace namespace LibHac.Fs @@ -26,6 +27,48 @@ namespace LibHac.Fs public bool IsBackslashAllowed() => (_value & (1 << 4)) != 0; } + /// + /// Contains functions like those in because ref struct + /// types can't be used as generics yet. + /// + public static class PathExtensions + { + /// + /// Reinterprets the given read-only reference as a reference. + /// + /// This function allows using a expression with s + /// while still being able to pass it by reference. + /// This function is a static method instead of an instance method because + /// as a static method we get escape analysis so the lifetime of the returned reference is restricted to that + /// of the input reference. + /// The read-only reference to reinterpret. + /// A reference to the given . + // ReSharper disable once EntityNameCapturedOnly.Global + public static ref Path Ref(this in Path path) + { + Ldarg(nameof(path)); + Ret(); + throw InlineIL.IL.Unreachable(); + } + + public static ref Path GetNullRef() + { + Ldc_I4_0(); + Conv_U(); + Ret(); + throw InlineIL.IL.Unreachable(); + } + + public static bool IsNullRef(in Path path) + { + Ldarg_0(); + Ldc_I4_0(); + Conv_U(); + Ceq(); + return InlineIL.IL.Return(); + } + } + /// /// Represents a file path stored as a UTF-8 string. /// @@ -133,7 +176,6 @@ namespace LibHac.Fs private int _writeBufferLength; private bool _isNormalized; - // Todo: Hack around "using" variables being read only public void Dispose() { byte[] writeBuffer = Shared.Move(ref _writeBuffer); diff --git a/src/LibHac/Fs/Fsa/FileSystemAccessor.cs b/src/LibHac/Fs/Fsa/FileSystemAccessor.cs index 102091fd..5b627f87 100644 --- a/src/LibHac/Fs/Fsa/FileSystemAccessor.cs +++ b/src/LibHac/Fs/Fsa/FileSystemAccessor.cs @@ -167,8 +167,8 @@ namespace LibHac.Fs.Impl public Result CreateFile(U8Span path, long size, CreateFileOptions option) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; if (_isPathCacheAttached) @@ -181,83 +181,77 @@ namespace LibHac.Fs.Impl if (rc.IsFailure()) return rc; } - pathNormalized.Dispose(); return Result.Success; } public Result DeleteFile(U8Span path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.DeleteFile(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result CreateDirectory(U8Span path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.CreateDirectory(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result DeleteDirectory(U8Span path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.CreateDirectory(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result DeleteDirectoryRecursively(U8Span path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.DeleteDirectoryRecursively(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result CleanDirectoryRecursively(U8Span path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.CleanDirectoryRecursively(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result RenameFile(U8Span currentPath, U8Span newPath) { - var currentPathNormalized = new Path(); - Result rc = SetUpPath(ref currentPathNormalized, currentPath); + using var currentPathNormalized = new Path(); + Result rc = SetUpPath(ref currentPathNormalized.Ref(), currentPath); if (rc.IsFailure()) return rc; - var newPathNormalized = new Path(); - rc = SetUpPath(ref newPathNormalized, newPath); + using var newPathNormalized = new Path(); + rc = SetUpPath(ref newPathNormalized.Ref(), newPath); if (rc.IsFailure()) return rc; if (_isPathCacheAttached) @@ -270,19 +264,17 @@ namespace LibHac.Fs.Impl if (rc.IsFailure()) return rc; } - currentPathNormalized.Dispose(); - newPathNormalized.Dispose(); return Result.Success; } public Result RenameDirectory(U8Span currentPath, U8Span newPath) { - var currentPathNormalized = new Path(); - Result rc = SetUpPath(ref currentPathNormalized, currentPath); + using var currentPathNormalized = new Path(); + Result rc = SetUpPath(ref currentPathNormalized.Ref(), currentPath); if (rc.IsFailure()) return rc; - var newPathNormalized = new Path(); - rc = SetUpPath(ref newPathNormalized, newPath); + using var newPathNormalized = new Path(); + rc = SetUpPath(ref newPathNormalized.Ref(), newPath); if (rc.IsFailure()) return rc; if (_isPathCacheAttached) @@ -294,9 +286,6 @@ namespace LibHac.Fs.Impl rc = _fileSystem.RenameDirectory(in currentPathNormalized, in newPathNormalized); if (rc.IsFailure()) return rc; } - - currentPathNormalized.Dispose(); - newPathNormalized.Dispose(); return Result.Success; } @@ -304,14 +293,13 @@ namespace LibHac.Fs.Impl { UnsafeHelpers.SkipParamInit(out entryType); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.GetEntryType(out entryType, in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } @@ -319,14 +307,13 @@ namespace LibHac.Fs.Impl { UnsafeHelpers.SkipParamInit(out freeSpace); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.GetFreeSpaceSize(out freeSpace, in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } @@ -334,14 +321,13 @@ namespace LibHac.Fs.Impl { UnsafeHelpers.SkipParamInit(out totalSpace); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.GetTotalSpaceSize(out totalSpace, in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } @@ -349,8 +335,8 @@ namespace LibHac.Fs.Impl { UnsafeHelpers.SkipParamInit(out file); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; IFile iFile = null; @@ -384,7 +370,6 @@ namespace LibHac.Fs.Impl finally { iFile?.Dispose(); - pathNormalized.Dispose(); } } @@ -392,8 +377,8 @@ namespace LibHac.Fs.Impl { UnsafeHelpers.SkipParamInit(out directory); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; IDirectory iDirectory = null; @@ -415,7 +400,6 @@ namespace LibHac.Fs.Impl finally { iDirectory?.Dispose(); - pathNormalized.Dispose(); } } @@ -449,27 +433,25 @@ namespace LibHac.Fs.Impl { UnsafeHelpers.SkipParamInit(out timeStamp); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.GetFileTimeStampRaw(out timeStamp, in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result QueryEntry(Span outBuffer, ReadOnlySpan inBuffer, QueryId queryId, U8Span path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; rc = _fileSystem.QueryEntry(outBuffer, inBuffer, queryId, in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } @@ -681,7 +663,7 @@ namespace LibHac.Fs.Impl var fileInfoString = new U8StringBuilder(stringBuffer, true); fileInfoString.Append(LogHandle).AppendFormat(file.Value.GetHashCode(), 'x', 16).Append(LogOpenMode) - .Append(openModeString.Buffer).Append(LogSize).AppendFormat(fileSize).Append((byte) '\n'); + .Append(openModeString.Buffer).Append(LogSize).AppendFormat(fileSize).Append((byte)'\n'); Hos.Diag.Impl.LogImpl(LogFsModuleName, LogSeverity.Error, fileInfoString.Buffer); fileInfoString.Dispose(); diff --git a/src/LibHac/Fs/Fsa/IFileSystem.cs b/src/LibHac/Fs/Fsa/IFileSystem.cs index ad6fd1f0..0f524eee 100644 --- a/src/LibHac/Fs/Fsa/IFileSystem.cs +++ b/src/LibHac/Fs/Fsa/IFileSystem.cs @@ -163,7 +163,7 @@ namespace LibHac.Fs.Fsa if (path.IsNull()) return ResultFs.NullptrArgument.Log(); - var pathNormalized = new Path(); + using var pathNormalized = new Path(); Result rs = pathNormalized.InitializeWithNormalization(path); if (rs.IsFailure()) return rs; @@ -210,7 +210,7 @@ namespace LibHac.Fs.Fsa if (path.IsNull()) return ResultFs.NullptrArgument.Log(); - var pathNormalized = new Path(); + using var pathNormalized = new Path(); Result rs = pathNormalized.InitializeWithNormalization(path); if (rs.IsFailure()) return rs; diff --git a/src/LibHac/FsSrv/BaseFileSystemService.cs b/src/LibHac/FsSrv/BaseFileSystemService.cs index 0b0e5fe0..4b46f33f 100644 --- a/src/LibHac/FsSrv/BaseFileSystemService.cs +++ b/src/LibHac/FsSrv/BaseFileSystemService.cs @@ -117,7 +117,7 @@ namespace LibHac.FsSrv using var scopedContext = new ScopedStorageLayoutTypeSetter(storageFlag); // Normalize the path - var pathNormalized = new Path(); + using var pathNormalized = new Path(); rc = pathNormalized.Initialize(rootPath.Str); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs b/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs index 9dcad3d4..402481c8 100644 --- a/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs +++ b/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs @@ -48,33 +48,31 @@ namespace LibHac.FsSrv Result rc = BaseFileSystemService.OpenBisFileSystem(out fileSystem, BisPartitionId.User); if (rc.IsFailure()) return rc; - var path = new Path(); + using var path = new Path(); var sb = new U8StringBuilder(pathBuffer); sb.Append((byte)'/') .Append(CustomStorage.GetCustomStorageDirectoryName(CustomStorageId.System)); - rc = PathFunctions.SetUpFixedPath(ref path, pathBuffer); + rc = PathFunctions.SetUpFixedPath(ref path.Ref(), pathBuffer); if (rc.IsFailure()) return rc; tempFs = Shared.Move(ref fileSystem); rc = Utility.WrapSubDirectory(out fileSystem, ref tempFs, in path, true); if (rc.IsFailure()) return rc; - - path.Dispose(); } else if (storageId == CustomStorageId.SdCard) { Result rc = BaseFileSystemService.OpenSdCardProxyFileSystem(out fileSystem); if (rc.IsFailure()) return rc; - var path = new Path(); + using var path = new Path(); var sb = new U8StringBuilder(pathBuffer); sb.Append((byte)'/') .Append(CommonPaths.SdCardNintendoRootDirectoryName) .Append((byte)'/') .Append(CustomStorage.GetCustomStorageDirectoryName(CustomStorageId.SdCard)); - rc = PathFunctions.SetUpFixedPath(ref path, pathBuffer); + rc = PathFunctions.SetUpFixedPath(ref path.Ref(), pathBuffer); if (rc.IsFailure()) return rc; tempFs = Shared.Move(ref fileSystem); @@ -85,8 +83,6 @@ namespace LibHac.FsSrv rc = FsCreators.EncryptedFileSystemCreator.Create(out fileSystem, ref tempFs, IEncryptedFileSystemCreator.KeyId.CustomStorage, SdEncryptionSeed); if (rc.IsFailure()) return rc; - - path.Dispose(); } else { @@ -107,18 +103,17 @@ namespace LibHac.FsSrv { UnsafeHelpers.SkipParamInit(out fileSystem); - var pathHost = new Path(); + using var pathHost = new Path(); Result rc = pathHost.Initialize(in path); if (rc.IsFailure()) return rc; - rc = FsCreators.TargetManagerFileSystemCreator.NormalizeCaseOfPath(out bool isSupported, ref pathHost); + rc = FsCreators.TargetManagerFileSystemCreator.NormalizeCaseOfPath(out bool isSupported, ref pathHost.Ref()); if (rc.IsFailure()) return rc; rc = FsCreators.TargetManagerFileSystemCreator.Create(out fileSystem, in pathHost, isSupported, false, Result.Success); if (rc.IsFailure()) return rc; - pathHost.Dispose(); return Result.Success; } diff --git a/src/LibHac/FsSrv/FileSystemProxyImpl.cs b/src/LibHac/FsSrv/FileSystemProxyImpl.cs index ee8812dd..03d96d39 100644 --- a/src/LibHac/FsSrv/FileSystemProxyImpl.cs +++ b/src/LibHac/FsSrv/FileSystemProxyImpl.cs @@ -530,7 +530,7 @@ namespace LibHac.FsSrv ReferenceCountedDisposable hostFileSystem = null; try { - var pathNormalized = new Path(); + using var pathNormalized = new Path(); if (path.Str.At(0) == DirectorySeparator && path.Str.At(1) != DirectorySeparator) { @@ -562,7 +562,6 @@ namespace LibHac.FsSrv fileSystem = FileSystemInterfaceAdapter.CreateShared(ref hostFileSystem, adapterFlags, false); - pathNormalized.Dispose(); return Result.Success; } finally diff --git a/src/LibHac/FsSrv/FsCreator/EmulatedBisFileSystemCreator.cs b/src/LibHac/FsSrv/FsCreator/EmulatedBisFileSystemCreator.cs index 6b77cb0b..bcea1973 100644 --- a/src/LibHac/FsSrv/FsCreator/EmulatedBisFileSystemCreator.cs +++ b/src/LibHac/FsSrv/FsCreator/EmulatedBisFileSystemCreator.cs @@ -73,7 +73,7 @@ namespace LibHac.FsSrv.FsCreator return ResultFs.PreconditionViolation.Log(); } - var bisRootPath = new Path(); + using var bisRootPath = new Path(); Result rc = bisRootPath.Initialize(GetPartitionPath(partitionId).ToU8String()); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/FsSrv/FsCreator/EmulatedSdCardFileSystemCreator.cs b/src/LibHac/FsSrv/FsCreator/EmulatedSdCardFileSystemCreator.cs index 1c2150e6..91b1f5f7 100644 --- a/src/LibHac/FsSrv/FsCreator/EmulatedSdCardFileSystemCreator.cs +++ b/src/LibHac/FsSrv/FsCreator/EmulatedSdCardFileSystemCreator.cs @@ -68,7 +68,7 @@ namespace LibHac.FsSrv.FsCreator string path = Path ?? DefaultPath; - var sdCardPath = new Path(); + using var sdCardPath = new Path(); Result rc = sdCardPath.Initialize(StringUtils.StringToUtf8(path)); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs b/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs index 83754417..2992219c 100644 --- a/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs +++ b/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs @@ -52,8 +52,8 @@ namespace LibHac.FsSrv.FsCreator Assert.SdkRequiresNotNull(cacheManager); - var saveImageName = new Path(); - Result rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName, saveImageNameBuffer, saveDataId); + using var saveImageName = new Path(); + Result rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); if (rc.IsFailure()) return rc; rc = baseFileSystem.Target.GetEntryType(out DirectoryEntryType type, in saveImageName); @@ -86,7 +86,6 @@ namespace LibHac.FsSrv.FsCreator fileSystem = saveFs.AddReference(); extraDataAccessor = saveFs.AddReference(); - saveImageName.Dispose(); return Result.Success; } finally diff --git a/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs b/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs index 14a63df7..d650018b 100644 --- a/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs +++ b/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs @@ -337,34 +337,32 @@ namespace LibHac.FsSrv.Impl if (size < 0) return ResultFs.InvalidSize.Log(); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.CreateFile(in pathNormalized, size, (CreateFileOptions)option); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result DeleteFile(in PathSf path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.DeleteFile(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result CreateDirectory(in PathSf path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; if (pathNormalized == RootDir) @@ -373,14 +371,13 @@ namespace LibHac.FsSrv.Impl rc = _baseFileSystem.Target.CreateDirectory(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result DeleteDirectory(in PathSf path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; if (pathNormalized == RootDir) @@ -389,14 +386,13 @@ namespace LibHac.FsSrv.Impl rc = _baseFileSystem.Target.DeleteDirectory(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result DeleteDirectoryRecursively(in PathSf path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; if (pathNormalized == RootDir) @@ -405,49 +401,45 @@ namespace LibHac.FsSrv.Impl rc = _baseFileSystem.Target.DeleteDirectoryRecursively(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result CleanDirectoryRecursively(in PathSf path) { - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.CleanDirectoryRecursively(in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } public Result RenameFile(in PathSf currentPath, in PathSf newPath) { - var currentPathNormalized = new Path(); - Result rc = SetUpPath(ref currentPathNormalized, in currentPath); + using var currentPathNormalized = new Path(); + Result rc = SetUpPath(ref currentPathNormalized.Ref(), in currentPath); if (rc.IsFailure()) return rc; - var newPathNormalized = new Path(); - rc = SetUpPath(ref newPathNormalized, in newPath); + using var newPathNormalized = new Path(); + rc = SetUpPath(ref newPathNormalized.Ref(), in newPath); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.RenameFile(in currentPathNormalized, in newPathNormalized); if (rc.IsFailure()) return rc; - currentPathNormalized.Dispose(); - newPathNormalized.Dispose(); return Result.Success; } public Result RenameDirectory(in PathSf currentPath, in PathSf newPath) { - var currentPathNormalized = new Path(); - Result rc = SetUpPath(ref currentPathNormalized, in currentPath); + using var currentPathNormalized = new Path(); + Result rc = SetUpPath(ref currentPathNormalized.Ref(), in currentPath); if (rc.IsFailure()) return rc; - var newPathNormalized = new Path(); - rc = SetUpPath(ref newPathNormalized, in newPath); + using var newPathNormalized = new Path(); + rc = SetUpPath(ref newPathNormalized.Ref(), in newPath); if (rc.IsFailure()) return rc; if (PathUtility.IsSubPath(currentPathNormalized.GetString(), newPathNormalized.GetString())) @@ -456,8 +448,6 @@ namespace LibHac.FsSrv.Impl rc = _baseFileSystem.Target.RenameDirectory(in currentPathNormalized, in newPathNormalized); if (rc.IsFailure()) return rc; - currentPathNormalized.Dispose(); - newPathNormalized.Dispose(); return Result.Success; } @@ -465,15 +455,14 @@ namespace LibHac.FsSrv.Impl { UnsafeHelpers.SkipParamInit(out entryType); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.GetEntryType(out DirectoryEntryType type, in pathNormalized); if (rc.IsFailure()) return rc; entryType = (uint)type; - pathNormalized.Dispose(); return Result.Success; } @@ -481,15 +470,14 @@ namespace LibHac.FsSrv.Impl { UnsafeHelpers.SkipParamInit(out freeSpace); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.GetFreeSpaceSize(out long space, in pathNormalized); if (rc.IsFailure()) return rc; freeSpace = space; - pathNormalized.Dispose(); return Result.Success; } @@ -497,15 +485,14 @@ namespace LibHac.FsSrv.Impl { UnsafeHelpers.SkipParamInit(out totalSpace); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.GetTotalSpaceSize(out long space, in pathNormalized); if (rc.IsFailure()) return rc; totalSpace = space; - pathNormalized.Dispose(); return Result.Success; } @@ -514,8 +501,8 @@ namespace LibHac.FsSrv.Impl const int maxTryCount = 2; UnsafeHelpers.SkipParamInit(out file); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; IFile fileInterface = null; @@ -536,7 +523,6 @@ namespace LibHac.FsSrv.Impl var adapter = new FileInterfaceAdapter(Shared.Move(ref fileInterface), ref selfReference, _allowAllOperations); file = new ReferenceCountedDisposable(adapter); - pathNormalized.Dispose(); return Result.Success; } finally @@ -550,8 +536,8 @@ namespace LibHac.FsSrv.Impl const int maxTryCount = 2; UnsafeHelpers.SkipParamInit(out directory); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; IDirectory dirInterface = null; @@ -573,7 +559,6 @@ namespace LibHac.FsSrv.Impl var adapter = new DirectoryInterfaceAdapter(dirInterface, ref selfReference); directory = new ReferenceCountedDisposable(adapter); - pathNormalized.Dispose(); return Result.Success; } finally @@ -591,15 +576,14 @@ namespace LibHac.FsSrv.Impl { UnsafeHelpers.SkipParamInit(out timeStamp); - var pathNormalized = new Path(); - Result rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.GetFileTimeStampRaw(out FileTimeStampRaw tempTimeStamp, in pathNormalized); if (rc.IsFailure()) return rc; timeStamp = tempTimeStamp; - pathNormalized.Dispose(); return Result.Success; } @@ -623,15 +607,14 @@ namespace LibHac.FsSrv.Impl Result rc = PermissionCheck((QueryId)queryId, this); if (rc.IsFailure()) return rc; - var pathNormalized = new Path(); - rc = SetUpPath(ref pathNormalized, in path); + using var pathNormalized = new Path(); + rc = SetUpPath(ref pathNormalized.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.Target.QueryEntry(outBuffer.Buffer, inBuffer.Buffer, (QueryId)queryId, in pathNormalized); if (rc.IsFailure()) return rc; - pathNormalized.Dispose(); return Result.Success; } diff --git a/src/LibHac/FsSrv/Impl/MultiCommitManager.cs b/src/LibHac/FsSrv/Impl/MultiCommitManager.cs index 955d7d3a..a50a7c4d 100644 --- a/src/LibHac/FsSrv/Impl/MultiCommitManager.cs +++ b/src/LibHac/FsSrv/Impl/MultiCommitManager.cs @@ -290,8 +290,8 @@ namespace LibHac.FsSrv.Impl private static Result RecoverCommit(ISaveDataMultiCommitCoreInterface multiCommitInterface, IFileSystem contextFs, SaveDataFileSystemServiceImpl saveService) { - var contextFilePath = new Fs.Path(); - Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath, CommitContextFileName); + using var contextFilePath = new Fs.Path(); + Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath.Ref(), CommitContextFileName); if (rc.IsFailure()) return rc; IFile contextFile = null; @@ -374,7 +374,6 @@ namespace LibHac.FsSrv.Impl } } - contextFilePath.Dispose(); return recoveryResult; } finally @@ -465,8 +464,8 @@ namespace LibHac.FsSrv.Impl } } - var contextFilePath = new Fs.Path(); - rc = PathFunctions.SetUpFixedPath(ref contextFilePath, CommitContextFileName); + using var contextFilePath = new Fs.Path(); + rc = PathFunctions.SetUpFixedPath(ref contextFilePath.Ref(), CommitContextFileName); if (rc.IsFailure()) return rc; // Delete the commit context file @@ -476,7 +475,6 @@ namespace LibHac.FsSrv.Impl rc = contextFs.Commit(); if (rc.IsFailure()) return rc; - contextFilePath.Dispose(); return recoveryResult; } @@ -514,8 +512,8 @@ namespace LibHac.FsSrv.Impl if (needsRecovery) { - var contextFilePath = new Fs.Path(); - rc = PathFunctions.SetUpFixedPath(ref contextFilePath, CommitContextFileName); + using var contextFilePath = new Fs.Path(); + rc = PathFunctions.SetUpFixedPath(ref contextFilePath.Ref(), CommitContextFileName); if (rc.IsFailure()) return rc; rc = fileSystem.Target.OpenFile(out IFile file, in contextFilePath, OpenMode.Read); @@ -527,8 +525,6 @@ namespace LibHac.FsSrv.Impl if (ResultFs.PathNotFound.Includes(rc)) needsRecovery = false; } - - contextFilePath.Dispose(); } if (!needsRecovery) @@ -575,13 +571,12 @@ namespace LibHac.FsSrv.Impl { if (_fileSystem is null) return; - var contextFilePath = new Fs.Path(); - PathFunctions.SetUpFixedPath(ref contextFilePath, CommitContextFileName).IgnoreResult(); + using var contextFilePath = new Fs.Path(); + PathFunctions.SetUpFixedPath(ref contextFilePath.Ref(), CommitContextFileName).IgnoreResult(); _fileSystem.DeleteFile(in contextFilePath).IgnoreResult(); _fileSystem.Commit().IgnoreResult(); _fileSystem = null; - contextFilePath.Dispose(); } /// @@ -592,8 +587,8 @@ namespace LibHac.FsSrv.Impl /// The of the operation. public Result Create(long counter, int fileSystemCount) { - var contextFilePath = new Fs.Path(); - Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath, CommitContextFileName); + using var contextFilePath = new Fs.Path(); + Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath.Ref(), CommitContextFileName); if (rc.IsFailure()) return rc; IFile contextFile = null; @@ -645,7 +640,6 @@ namespace LibHac.FsSrv.Impl rc = _fileSystem.Commit(); if (rc.IsFailure()) return rc; - contextFilePath.Dispose(); return Result.Success; } @@ -656,8 +650,8 @@ namespace LibHac.FsSrv.Impl /// The of the operation. public Result CommitProvisionallyDone() { - var contextFilePath = new Fs.Path(); - Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath, CommitContextFileName); + using var contextFilePath = new Fs.Path(); + Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath.Ref(), CommitContextFileName); if (rc.IsFailure()) return rc; IFile contextFile = null; @@ -680,7 +674,6 @@ namespace LibHac.FsSrv.Impl contextFile?.Dispose(); } - contextFilePath.Dispose(); return _fileSystem.Commit(); } @@ -690,8 +683,8 @@ namespace LibHac.FsSrv.Impl /// The of the operation. public Result CommitDone() { - var contextFilePath = new Fs.Path(); - Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath, CommitContextFileName); + using var contextFilePath = new Fs.Path(); + Result rc = PathFunctions.SetUpFixedPath(ref contextFilePath.Ref(), CommitContextFileName); if (rc.IsFailure()) return rc; rc = _fileSystem.DeleteFile(in contextFilePath); @@ -701,7 +694,6 @@ namespace LibHac.FsSrv.Impl if (rc.IsFailure()) return rc; _fileSystem = null; - contextFilePath.Dispose(); return Result.Success; } } diff --git a/src/LibHac/FsSrv/NcaFileSystemService.cs b/src/LibHac/FsSrv/NcaFileSystemService.cs index cd8aeb50..11296a13 100644 --- a/src/LibHac/FsSrv/NcaFileSystemService.cs +++ b/src/LibHac/FsSrv/NcaFileSystemService.cs @@ -94,17 +94,17 @@ namespace LibHac.FsSrv if (rc.IsFailure()) return rc; // Try to find the path to the original version of the file system - var originalPath = new Path(); + using var originalPath = new Path(); Result originalResult = ServiceImpl.ResolveApplicationHtmlDocumentPath(out bool isDirectory, - ref originalPath, new Ncm.ApplicationId(programId.Value), ownerProgramInfo.StorageId); + ref originalPath.Ref(), new Ncm.ApplicationId(programId.Value), ownerProgramInfo.StorageId); // The file system might have a patch version with no original version, so continue if not found if (originalResult.IsFailure() && !ResultLr.HtmlDocumentNotFound.Includes(originalResult)) return originalResult; // Try to find the path to the patch file system - var patchPath = new Path(); - Result patchResult = ServiceImpl.ResolveRegisteredHtmlDocumentPath(ref patchPath, programId.Value); + using var patchPath = new Path(); + Result patchResult = ServiceImpl.ResolveRegisteredHtmlDocumentPath(ref patchPath.Ref(), programId.Value); ReferenceCountedDisposable tempFileSystem = null; ReferenceCountedDisposable accessFailureManager = null; @@ -126,11 +126,9 @@ namespace LibHac.FsSrv if (patchResult.IsFailure()) return patchResult; - var emptyPath = new Path(); - rc = emptyPath.InitializeAsEmpty(); - if (rc.IsFailure()) return rc; - - ref Path originalNcaPath = ref originalResult.IsSuccess() ? ref originalPath : ref emptyPath; + ref readonly Path originalNcaPath = ref originalResult.IsSuccess() + ? ref originalPath + : ref PathExtensions.GetNullRef(); // Open the file system using both the original and patch versions rc = ServiceImpl.OpenFileSystemWithPatch(out tempFileSystem, in originalNcaPath, in patchPath, @@ -233,7 +231,7 @@ namespace LibHac.FsSrv bool canMountSystemDataPrivate = ac.GetAccessibilityFor(AccessibilityType.MountSystemDataPrivate).CanRead; - var pathNormalized = new Path(); + using var pathNormalized = new Path(); rc = pathNormalized.InitializeWithReplaceUnc(path.Str); if (rc.IsFailure()) return rc; @@ -337,8 +335,8 @@ namespace LibHac.FsSrv if (!programInfo.AccessControl.CanCall(OperationType.GetRightsId)) return ResultFs.PermissionDenied.Log(); - var programPath = new Path(); - rc = ServiceImpl.ResolveProgramPath(out bool isDirectory, ref programPath, programId, storageId); + using var programPath = new Path(); + rc = ServiceImpl.ResolveProgramPath(out bool isDirectory, ref programPath.Ref(), programId, storageId); if (rc.IsFailure()) return rc; if (isDirectory) @@ -349,7 +347,6 @@ namespace LibHac.FsSrv outRightsId = rightsId; - programPath.Dispose(); return Result.Success; } @@ -365,7 +362,7 @@ namespace LibHac.FsSrv if (!programInfo.AccessControl.CanCall(OperationType.GetRightsId)) return ResultFs.PermissionDenied.Log(); - var pathNormalized = new Path(); + using var pathNormalized = new Path(); rc = pathNormalized.Initialize(path.Str); if (rc.IsFailure()) return rc; @@ -385,7 +382,6 @@ namespace LibHac.FsSrv outRightsId = rightsId; outKeyGeneration = keyGeneration; - pathNormalized.Dispose(); return Result.Success; } @@ -400,8 +396,8 @@ namespace LibHac.FsSrv StorageType storageFlag = ServiceImpl.GetStorageFlag(programId); using var scopedContext = new ScopedStorageLayoutTypeSetter(storageFlag); - var programPath = new Path(); - Result rc = ServiceImpl.ResolveRomPath(out bool isDirectory, ref programPath, programId, storageId); + using var programPath = new Path(); + Result rc = ServiceImpl.ResolveRomPath(out bool isDirectory, ref programPath.Ref(), programId, storageId); if (rc.IsFailure()) return rc; isHostFs = Utility.IsHostFsMountName(programPath.GetString()); @@ -502,8 +498,9 @@ namespace LibHac.FsSrv if (!programInfo.AccessControl.CanCall(OperationType.RegisterUpdatePartition)) return ResultFs.PermissionDenied.Log(); - var programPath = new Path(); - rc = ServiceImpl.ResolveRomPath(out _, ref programPath, programInfo.ProgramIdValue, programInfo.StorageId); + using var programPath = new Path(); + rc = ServiceImpl.ResolveRomPath(out _, ref programPath.Ref(), programInfo.ProgramIdValue, + programInfo.StorageId); if (rc.IsFailure()) return rc; return ServiceImpl.RegisterUpdatePartition(programInfo.ProgramIdValue, in programPath); diff --git a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs index e4e95d53..b79f87e5 100644 --- a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs @@ -121,7 +121,7 @@ namespace LibHac.FsSrv if (!mountNameInfo.IsHostFs) return ResultFs.PermissionDenied.Log(); - var directoryPath = new Path(); + using var directoryPath = new Path(); rc = directoryPath.InitializeWithNormalization(currentPath.Value); if (rc.IsFailure()) return rc; @@ -238,7 +238,7 @@ namespace LibHac.FsSrv ReferenceCountedDisposable baseFileSystem = null; ReferenceCountedDisposable subDirFileSystem = null; ReferenceCountedDisposable tempFileSystem = null; - Path contentStoragePath = default; + try { Result rc; @@ -280,8 +280,8 @@ namespace LibHac.FsSrv sb.Append(StringTraits.DirectorySeparator).Append(ContentStorageDirectoryName); } - contentStoragePath = new Path(); - rc = PathFunctions.SetUpFixedPath(ref contentStoragePath, contentStoragePathBuffer); + using var contentStoragePath = new Path(); + rc = PathFunctions.SetUpFixedPath(ref contentStoragePath.Ref(), contentStoragePathBuffer); if (rc.IsFailure()) return rc; // Make sure the content storage path exists @@ -311,7 +311,6 @@ namespace LibHac.FsSrv baseFileSystem?.Dispose(); subDirFileSystem?.Dispose(); tempFileSystem?.Dispose(); - contentStoragePath.Dispose(); } } @@ -481,7 +480,7 @@ namespace LibHac.FsSrv { path = path.Slice(CommonPaths.HostRootFileSystemMountName.Length); - var rootPathEmpty = new Path(); + using var rootPathEmpty = new Path(); Result rc = rootPathEmpty.InitializeAsEmpty(); if (rc.IsFailure()) return rc; @@ -579,24 +578,22 @@ namespace LibHac.FsSrv { UnsafeHelpers.SkipParamInit(out fileSystem); - var pathRoot = new Path(); - var pathData = new Path(); + using var pathRoot = new Path(); + using var pathData = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref pathData, + Result rc = PathFunctions.SetUpFixedPath(ref pathData.Ref(), new[] { (byte)'/', (byte)'d', (byte)'a', (byte)'t', (byte)'a' }); if (rc.IsFailure()) return rc; rc = pathRoot.Combine(in path, in pathData); if (rc.IsFailure()) return rc; - rc = _config.TargetManagerFsCreator.NormalizeCaseOfPath(out bool isSupported, ref pathRoot); + rc = _config.TargetManagerFsCreator.NormalizeCaseOfPath(out bool isSupported, ref pathRoot.Ref()); if (rc.IsFailure()) return rc; rc = _config.TargetManagerFsCreator.Create(out fileSystem, in pathRoot, isSupported, false, Result.Success); if (rc.IsFailure()) return rc; - pathData.Dispose(); - pathRoot.Dispose(); return Result.Success; } @@ -721,8 +718,8 @@ namespace LibHac.FsSrv ReferenceCountedDisposable subDirFs = null; try { - var directoryPath = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref directoryPath, dirName); + using var directoryPath = new Path(); + Result rc = PathFunctions.SetUpFixedPath(ref directoryPath.Ref(), dirName); if (rc.IsFailure()) return rc; if (directoryPath.IsEmpty()) diff --git a/src/LibHac/FsSrv/SaveDataFileSystemService.cs b/src/LibHac/FsSrv/SaveDataFileSystemService.cs index 39a23bfd..3e75bdd7 100644 --- a/src/LibHac/FsSrv/SaveDataFileSystemService.cs +++ b/src/LibHac/FsSrv/SaveDataFileSystemService.cs @@ -626,7 +626,7 @@ namespace LibHac.FsSrv if (!programInfo.AccessControl.CanCall(OperationType.DebugSaveData)) return ResultFs.PermissionDenied.Log(); - var saveDataRootPath = new Path(); + using var saveDataRootPath = new Path(); if (path.Str[0] == NullTerminator) { @@ -648,7 +648,6 @@ namespace LibHac.FsSrv if (rc.IsFailure()) return rc; _saveDataRootPath.Initialize(in saveDataRootPath); - saveDataRootPath.Dispose(); return Result.Success; } @@ -661,12 +660,11 @@ namespace LibHac.FsSrv if (!programInfo.AccessControl.CanCall(OperationType.DebugSaveData)) return ResultFs.PermissionDenied.Log(); - var saveDataRootPath = new Path(); + using var saveDataRootPath = new Path(); rc = saveDataRootPath.InitializeAsEmpty(); if (rc.IsFailure()) return rc; _saveDataRootPath.Initialize(in saveDataRootPath); - saveDataRootPath.Dispose(); return Result.Success; } @@ -2114,8 +2112,8 @@ namespace LibHac.FsSrv Result rc = _serviceImpl.OpenSaveDataDirectoryFileSystem(out fileSystem, SaveDataSpaceId.Temporary); if (rc.IsFailure()) return rc; - var pathRoot = new Path(); - rc = PathFunctions.SetUpFixedPath(ref pathRoot, new[] { (byte)'/' }); + using var pathRoot = new Path(); + rc = PathFunctions.SetUpFixedPath(ref pathRoot.Ref(), new[] { (byte)'/' }); if (rc.IsFailure()) return rc; rc = fileSystem.Target.CleanDirectoryRecursively(in pathRoot); diff --git a/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs b/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs index 8e105c63..1d335106 100644 --- a/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs @@ -98,8 +98,8 @@ namespace LibHac.FsSrv ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); Span saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); - var saveImageName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName, saveImageNameBuffer, saveDataId); + using var saveImageName = new Path(); + rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); if (rc.IsFailure()) return rc; rc = fileSystem.Target.GetEntryType(out _, in saveImageName); @@ -223,8 +223,8 @@ namespace LibHac.FsSrv ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); Span saveDataMetaIdDirectoryNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); - var saveDataMetaIdDirectoryName = new Path(); - Result rc = PathFunctions.SetUpFixedPathSaveMetaDir(ref saveDataMetaIdDirectoryName, + using var saveDataMetaIdDirectoryName = new Path(); + Result rc = PathFunctions.SetUpFixedPathSaveMetaDir(ref saveDataMetaIdDirectoryName.Ref(), saveDataMetaIdDirectoryNameBuffer, saveDataId); if (rc.IsFailure()) return rc; @@ -259,8 +259,8 @@ namespace LibHac.FsSrv ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); Span saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); - var saveDataMetaName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName, saveDataMetaNameBuffer, + using var saveDataMetaName = new Path(); + rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer, (uint)metaType); if (rc.IsFailure()) return rc; @@ -286,8 +286,8 @@ namespace LibHac.FsSrv ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); Span saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); - var saveDataMetaName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName, saveDataMetaNameBuffer, + using var saveDataMetaName = new Path(); + rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer, (uint)metaType); if (rc.IsFailure()) return rc; @@ -317,15 +317,15 @@ namespace LibHac.FsSrv ReferenceCountedDisposable fileSystem = null; try { - var saveDataMetaDirectoryName = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref saveDataMetaDirectoryName, metaDirName); + using var saveDataMetaDirectoryName = new Path(); + Result rc = PathFunctions.SetUpFixedPath(ref saveDataMetaDirectoryName.Ref(), metaDirName); if (rc.IsFailure()) return rc; rc = OpenSaveDataDirectoryFileSystemImpl(out fileSystem, spaceId, in saveDataMetaDirectoryName, false); if (rc.IsFailure()) return rc; - var saveDataIdDirectoryName = new Path(); - PathFunctions.SetUpFixedPathSaveId(ref saveDataIdDirectoryName, saveDataIdDirectoryNameBuffer, + using var saveDataIdDirectoryName = new Path(); + PathFunctions.SetUpFixedPathSaveId(ref saveDataIdDirectoryName.Ref(), saveDataIdDirectoryNameBuffer, saveDataId); if (rc.IsFailure()) return rc; @@ -335,9 +335,6 @@ namespace LibHac.FsSrv if (rc.IsFailure() && !ResultFs.PathNotFound.Includes(rc)) return rc; - saveDataMetaDirectoryName.Dispose(); - saveDataIdDirectoryName.Dispose(); - return Result.Success; } finally @@ -363,8 +360,8 @@ namespace LibHac.FsSrv ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); Span saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); - var saveDataMetaName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName, saveDataMetaNameBuffer, + using var saveDataMetaName = new Path(); + rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer, (uint)metaType); if (rc.IsFailure()) return rc; @@ -395,8 +392,8 @@ namespace LibHac.FsSrv in saveDataRootPath, false); if (rc.IsFailure()) return rc; - var saveImageName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName, saveImageNameBuffer, saveDataId); + using var saveImageName = new Path(); + rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); if (rc.IsFailure()) return rc; if (_config.IsPseudoSaveData()) @@ -476,8 +473,8 @@ namespace LibHac.FsSrv Result rc = OpenSaveDataDirectoryFileSystem(out fileSystem, spaceId, in saveDataRootPath, false); if (rc.IsFailure()) return rc; - var saveImageName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName, saveImageNameBuffer, saveDataId); + using var saveImageName = new Path(); + rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); if (rc.IsFailure()) return rc; // Check if the save data is a file or a directory @@ -501,7 +498,6 @@ namespace LibHac.FsSrv if (rc.IsFailure()) return rc; } - saveImageName.Dispose(); return Result.Success; } finally @@ -643,7 +639,7 @@ namespace LibHac.FsSrv public Result OpenSaveDataDirectoryFileSystem(out ReferenceCountedDisposable fileSystem, SaveDataSpaceId spaceId) { - var rootPath = new Path(); + using var rootPath = new Path(); return OpenSaveDataDirectoryFileSystem(out fileSystem, spaceId, in rootPath, true); } @@ -667,18 +663,17 @@ namespace LibHac.FsSrv tmFs.Dispose(); - var path = new Path(); + using var path = new Path(); rc = path.Initialize(in saveDataRootPath); if (rc.IsFailure()) return rc; - rc = _config.TargetManagerFsCreator.NormalizeCaseOfPath(out bool isTargetFsCaseSensitive, ref path); + rc = _config.TargetManagerFsCreator.NormalizeCaseOfPath(out bool isTargetFsCaseSensitive, ref path.Ref()); if (rc.IsFailure()) return rc; rc = _config.TargetManagerFsCreator.Create(out tmFs, in path, isTargetFsCaseSensitive, false, ResultFs.SaveDataRootPathUnavailable.Value); if (rc.IsFailure()) return rc; - path.Dispose(); return Result.Success; } finally @@ -687,7 +682,7 @@ namespace LibHac.FsSrv } } - var saveDataDirPath = new Path(); + using var saveDataDirPath = new Path(); ReadOnlySpan saveDirName; if (spaceId == SaveDataSpaceId.Temporary) @@ -699,13 +694,12 @@ namespace LibHac.FsSrv saveDirName = new[] { (byte)'/', (byte)'s', (byte)'a', (byte)'v', (byte)'e' }; // /save } - rc = PathFunctions.SetUpFixedPath(ref saveDataDirPath, saveDirName); + rc = PathFunctions.SetUpFixedPath(ref saveDataDirPath.Ref(), saveDirName); if (rc.IsFailure()) return rc; rc = OpenSaveDataDirectoryFileSystemImpl(out fileSystem, spaceId, in saveDataDirPath, true); if (rc.IsFailure()) return rc; - saveDataDirPath.Dispose(); return Result.Success; } @@ -743,6 +737,7 @@ namespace LibHac.FsSrv case SaveDataSpaceId.SdSystem: case SaveDataSpaceId.SdCache: + { rc = _config.BaseFsService.OpenSdCardProxyFileSystem(out baseFileSystem, true); if (rc.IsFailure()) return rc; @@ -752,12 +747,12 @@ namespace LibHac.FsSrv ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); Span pathParentBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); - var parentPath = new Path(); - rc = PathFunctions.SetUpFixedPathSingleEntry(ref parentPath, pathParentBuffer, + using var parentPath = new Path(); + rc = PathFunctions.SetUpFixedPathSingleEntry(ref parentPath.Ref(), pathParentBuffer, CommonPaths.SdCardNintendoRootDirectoryName); if (rc.IsFailure()) return rc; - var pathSdRoot = new Path(); + using var pathSdRoot = new Path(); rc = pathSdRoot.Combine(in parentPath, in basePath); if (rc.IsFailure()) return rc; @@ -770,9 +765,8 @@ namespace LibHac.FsSrv IEncryptedFileSystemCreator.KeyId.Save, in _encryptionSeed); if (rc.IsFailure()) return rc; - parentPath.Dispose(); - pathSdRoot.Dispose(); return Result.Success; + } case SaveDataSpaceId.ProperSystem: rc = _config.BaseFsService.OpenBisFileSystem(out baseFileSystem, diff --git a/src/LibHac/FsSystem/ConcatenationFileSystem.cs b/src/LibHac/FsSystem/ConcatenationFileSystem.cs index aa2d281d..580a7549 100644 --- a/src/LibHac/FsSystem/ConcatenationFileSystem.cs +++ b/src/LibHac/FsSystem/ConcatenationFileSystem.cs @@ -200,7 +200,7 @@ namespace LibHac.FsSystem int currentTailIndex = GetInternalFileCount(currentSize) - 1; int newTailIndex = GetInternalFileCount(size) - 1; - var internalFilePath = new Path(); + using var internalFilePath = new Path(); rc = internalFilePath.Initialize(in _path); if (rc.IsFailure()) return rc; @@ -211,7 +211,7 @@ namespace LibHac.FsSystem for (int i = currentTailIndex + 1; i < newTailIndex; i++) { - rc = AppendInternalFilePath(ref internalFilePath, i); + rc = AppendInternalFilePath(ref internalFilePath.Ref(), i); if (rc.IsFailure()) return rc; rc = _baseFileSystem.CreateFile(in internalFilePath, GetInternalFileSize(size, i), @@ -234,7 +234,7 @@ namespace LibHac.FsSystem _files[i].Dispose(); _files.RemoveAt(i); - rc = AppendInternalFilePath(ref internalFilePath, i); + rc = AppendInternalFilePath(ref internalFilePath.Ref(), i); if (rc.IsFailure()) return rc; rc = _baseFileSystem.DeleteFile(in internalFilePath); @@ -248,7 +248,6 @@ namespace LibHac.FsSystem if (rc.IsFailure()) return rc; } - internalFilePath.Dispose(); return Result.Success; } @@ -430,7 +429,7 @@ namespace LibHac.FsSystem if (!_mode.HasFlag(OpenDirectoryMode.NoFileSize)) { - var internalFilePath = new Path(); + using var internalFilePath = new Path(); rc = internalFilePath.Initialize(in _path); if (rc.IsFailure()) return rc; @@ -439,8 +438,6 @@ namespace LibHac.FsSystem rc = _concatenationFileSystem.GetFileSize(out entry.Size, in internalFilePath); if (rc.IsFailure()) return rc; - - internalFilePath.Dispose(); } } @@ -586,13 +583,13 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out count); - var internalFilePath = new Path(); + using var internalFilePath = new Path(); Result rc = internalFilePath.Initialize(in path); if (rc.IsFailure()) return rc; for (int i = 0; ; i++) { - rc = AppendInternalFilePath(ref internalFilePath, i); + rc = AppendInternalFilePath(ref internalFilePath.Ref(), i); if (rc.IsFailure()) return rc; rc = _baseFileSystem.GetEntryType(out _, in internalFilePath); @@ -603,7 +600,6 @@ namespace LibHac.FsSystem if (ResultFs.PathNotFound.Includes(rc)) { count = i; - internalFilePath.Dispose(); return Result.Success; } @@ -661,7 +657,7 @@ namespace LibHac.FsSystem ConcatenationFile concatFile = null; var internalFiles = new List(fileCount); - var filePath = new Path(); + using var filePath = new Path(); filePath.Initialize(in path); if (rc.IsFailure()) return rc; @@ -669,7 +665,7 @@ namespace LibHac.FsSystem { for (int i = 0; i < fileCount; i++) { - rc = AppendInternalFilePath(ref filePath, i); + rc = AppendInternalFilePath(ref filePath.Ref(), i); if (rc.IsFailure()) return rc; rc = _baseFileSystem.OpenFile(out IFile internalFile, in filePath, mode); @@ -691,7 +687,6 @@ namespace LibHac.FsSystem } finally { - filePath.Dispose(); concatFile?.Dispose(); if (internalFiles is not null) @@ -734,8 +729,8 @@ namespace LibHac.FsSystem return _baseFileSystem.CreateFile(path, size, newOption); } - var parentPath = new Path(); - Result rc = GenerateParentPath(ref parentPath, in path); + using var parentPath = new Path(); + Result rc = GenerateParentPath(ref parentPath.Ref(), in path); if (rc.IsFailure()) return rc; if (IsConcatenationFile(in parentPath)) @@ -750,25 +745,24 @@ namespace LibHac.FsSystem // Handle the empty file case by manually creating a single empty internal file if (size == 0) { - var emptyFilePath = new Path(); - rc = GenerateInternalFilePath(ref emptyFilePath, 0, in path); + using var emptyFilePath = new Path(); + rc = GenerateInternalFilePath(ref emptyFilePath.Ref(), 0, in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.CreateFile(in emptyFilePath, 0, newOption); if (rc.IsFailure()) return rc; - emptyFilePath.Dispose(); return Result.Success; } long remaining = size; - var filePath = new Path(); + using var filePath = new Path(); filePath.Initialize(in path); if (rc.IsFailure()) return rc; for (int i = 0; remaining > 0; i++) { - rc = AppendInternalFilePath(ref filePath, i); + rc = AppendInternalFilePath(ref filePath.Ref(), i); if (rc.IsFailure()) return rc; long fileSize = Math.Min(remaining, _InternalFileSize); @@ -781,7 +775,7 @@ namespace LibHac.FsSystem { for (int index = i - 1; index >= 0; index--) { - rc = GenerateInternalFilePath(ref filePath, index, in path); + rc = GenerateInternalFilePath(ref filePath.Ref(), index, in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.DeleteFile(in filePath); @@ -800,7 +794,6 @@ namespace LibHac.FsSystem remaining -= fileSize; } - filePath.Dispose(); return Result.Success; } @@ -814,13 +807,13 @@ namespace LibHac.FsSystem Result rc = GetInternalFileCount(out int count, path); if (rc.IsFailure()) return rc; - var filePath = new Path(); + using var filePath = new Path(); rc = filePath.Initialize(in path); if (rc.IsFailure()) return rc; for (int i = count - 1; i >= 0; i--) { - rc = AppendInternalFilePath(ref filePath, i); + rc = AppendInternalFilePath(ref filePath.Ref(), i); if (rc.IsFailure()) return rc; rc = _baseFileSystem.DeleteFile(in filePath); @@ -833,15 +826,14 @@ namespace LibHac.FsSystem rc = _baseFileSystem.DeleteDirectoryRecursively(in path); if (rc.IsFailure()) return rc; - filePath.Dispose(); return Result.Success; } protected override Result DoCreateDirectory(in Path path) { // Check if the parent path is a concatenation file because we can't create a directory inside one. - var parentPath = new Path(); - Result rc = GenerateParentPath(ref parentPath, in path); + using var parentPath = new Path(); + Result rc = GenerateParentPath(ref parentPath.Ref(), in path); if (rc.IsFailure()) return rc; if (IsConcatenationFile(in parentPath)) @@ -928,7 +920,7 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out size); - var internalFilePath = new Path(); + using var internalFilePath = new Path(); Result rc = internalFilePath.Initialize(in path); if (rc.IsFailure()) return rc; @@ -936,7 +928,7 @@ namespace LibHac.FsSystem for (int i = 0; ; i++) { - rc = AppendInternalFilePath(ref internalFilePath, i); + rc = AppendInternalFilePath(ref internalFilePath.Ref(), i); if (rc.IsFailure()) return rc; rc = _baseFileSystem.GetFileSize(out long internalFileSize, in internalFilePath); @@ -947,7 +939,6 @@ namespace LibHac.FsSystem if (ResultFs.PathNotFound.Includes(rc)) { size = sizeTotal; - internalFilePath.Dispose(); return Result.Success; } diff --git a/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs b/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs index 22bf00d9..a2ecae0e 100644 --- a/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs +++ b/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs @@ -255,16 +255,16 @@ namespace LibHac.FsSystem Result rc = GetFileSystemLock(); if (rc.IsFailure()) return rc; - var pathModifiedDirectory = new Path(); - rc = PathFunctions.SetUpFixedPath(ref pathModifiedDirectory, ModifiedDirectoryName); + using var pathModifiedDirectory = new Path(); + rc = PathFunctions.SetUpFixedPath(ref pathModifiedDirectory.Ref(), ModifiedDirectoryName); if (rc.IsFailure()) return rc; - var pathCommittedDirectory = new Path(); - rc = PathFunctions.SetUpFixedPath(ref pathCommittedDirectory, CommittedDirectoryName); + using var pathCommittedDirectory = new Path(); + rc = PathFunctions.SetUpFixedPath(ref pathCommittedDirectory.Ref(), CommittedDirectoryName); if (rc.IsFailure()) return rc; - var pathSynchronizingDirectory = new Path(); - rc = PathFunctions.SetUpFixedPath(ref pathSynchronizingDirectory, SynchronizingDirectoryName); + using var pathSynchronizingDirectory = new Path(); + rc = PathFunctions.SetUpFixedPath(ref pathSynchronizingDirectory.Ref(), SynchronizingDirectoryName); if (rc.IsFailure()) return rc; // Ensure the working directory exists @@ -322,10 +322,6 @@ namespace LibHac.FsSystem rc = InitializeExtraData(); if (rc.IsFailure()) return rc; - pathModifiedDirectory.Dispose(); - pathCommittedDirectory.Dispose(); - pathSynchronizingDirectory.Dispose(); - return Result.Success; } @@ -335,8 +331,8 @@ namespace LibHac.FsSystem if (_lockFile is not null) return Result.Success; - var pathLockFile = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref pathLockFile, LockFileName); + using var pathLockFile = new Path(); + Result rc = PathFunctions.SetUpFixedPath(ref pathLockFile.Ref(), LockFileName); if (rc.IsFailure()) return rc; rc = _baseFs.OpenFile(out _lockFile, in pathLockFile, OpenMode.ReadWrite); @@ -357,33 +353,31 @@ namespace LibHac.FsSystem } } - pathLockFile.Dispose(); return Result.Success; } private Result ResolvePath(ref Path outFullPath, in Path path) { - var pathDirectoryName = new Path(); + using var pathDirectoryName = new Path(); // Use the committed directory directly if journaling is supported but not enabled ReadOnlySpan directoryName = _isJournalingSupported && !_isJournalingEnabled ? CommittedDirectoryName : ModifiedDirectoryName; - Result rc = PathFunctions.SetUpFixedPath(ref pathDirectoryName, directoryName); + Result rc = PathFunctions.SetUpFixedPath(ref pathDirectoryName.Ref(), directoryName); if (rc.IsFailure()) return rc; rc = outFullPath.Combine(in pathDirectoryName, in path); if (rc.IsFailure()) return rc; - pathDirectoryName.Dispose(); return Result.Success; } protected override Result DoCreateFile(in Path path, long size, CreateFileOptions option) { - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -391,14 +385,13 @@ namespace LibHac.FsSystem rc = _baseFs.CreateFile(in fullPath, size, option); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoDeleteFile(in Path path) { - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -406,14 +399,13 @@ namespace LibHac.FsSystem rc = _baseFs.DeleteFile(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoCreateDirectory(in Path path) { - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -421,14 +413,13 @@ namespace LibHac.FsSystem rc = _baseFs.CreateDirectory(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoDeleteDirectory(in Path path) { - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -436,14 +427,13 @@ namespace LibHac.FsSystem rc = _baseFs.DeleteDirectory(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoDeleteDirectoryRecursively(in Path path) { - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -451,14 +441,13 @@ namespace LibHac.FsSystem rc = _baseFs.DeleteDirectoryRecursively(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoCleanDirectoryRecursively(in Path path) { - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -466,19 +455,18 @@ namespace LibHac.FsSystem rc = _baseFs.CleanDirectoryRecursively(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoRenameFile(in Path currentPath, in Path newPath) { - var currentFullPath = new Path(); - var newFullPath = new Path(); + using var currentFullPath = new Path(); + using var newFullPath = new Path(); - Result rc = ResolvePath(ref currentFullPath, in currentPath); + Result rc = ResolvePath(ref currentFullPath.Ref(), in currentPath); if (rc.IsFailure()) return rc; - rc = ResolvePath(ref newFullPath, in newPath); + rc = ResolvePath(ref newFullPath.Ref(), in newPath); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -486,20 +474,18 @@ namespace LibHac.FsSystem rc = _baseFs.RenameFile(in currentFullPath, in newFullPath); if (rc.IsFailure()) return rc; - currentFullPath.Dispose(); - newFullPath.Dispose(); return Result.Success; } protected override Result DoRenameDirectory(in Path currentPath, in Path newPath) { - var currentFullPath = new Path(); - var newFullPath = new Path(); + using var currentFullPath = new Path(); + using var newFullPath = new Path(); - Result rc = ResolvePath(ref currentFullPath, in currentPath); + Result rc = ResolvePath(ref currentFullPath.Ref(), in currentPath); if (rc.IsFailure()) return rc; - rc = ResolvePath(ref newFullPath, in newPath); + rc = ResolvePath(ref newFullPath.Ref(), in newPath); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -507,8 +493,6 @@ namespace LibHac.FsSystem rc = _baseFs.RenameDirectory(in currentFullPath, in newFullPath); if (rc.IsFailure()) return rc; - currentFullPath.Dispose(); - newFullPath.Dispose(); return Result.Success; } @@ -516,8 +500,8 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out entryType); - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -525,7 +509,6 @@ namespace LibHac.FsSystem rc = _baseFs.GetEntryType(out entryType, in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } @@ -533,8 +516,8 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out file); - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -549,7 +532,6 @@ namespace LibHac.FsSystem _openWritableFileCount++; } - fullPath.Dispose(); return Result.Success; } @@ -557,8 +539,8 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out directory); - var fullPath = new Path(); - Result rc = ResolvePath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolvePath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); @@ -566,7 +548,6 @@ namespace LibHac.FsSystem rc = _baseFs.OpenDirectory(out directory, in fullPath, mode); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } @@ -697,14 +678,13 @@ namespace LibHac.FsSystem using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); - var pathModifiedDirectory = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref pathModifiedDirectory, ModifiedDirectoryName); + using var pathModifiedDirectory = new Path(); + Result rc = PathFunctions.SetUpFixedPath(ref pathModifiedDirectory.Ref(), ModifiedDirectoryName); if (rc.IsFailure()) return rc; rc = _baseFs.GetFreeSpaceSize(out freeSpace, in pathModifiedDirectory); if (rc.IsFailure()) return rc; - pathModifiedDirectory.Dispose(); return Result.Success; } @@ -714,14 +694,13 @@ namespace LibHac.FsSystem using ScopedLock scopedLock = ScopedLock.Lock(ref _mutex); - var pathModifiedDirectory = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref pathModifiedDirectory, ModifiedDirectoryName); + using var pathModifiedDirectory = new Path(); + Result rc = PathFunctions.SetUpFixedPath(ref pathModifiedDirectory.Ref(), ModifiedDirectoryName); if (rc.IsFailure()) return rc; rc = _baseFs.GetTotalSpaceSize(out totalSpace, in pathModifiedDirectory); if (rc.IsFailure()) return rc; - pathModifiedDirectory.Dispose(); return Result.Success; } @@ -758,16 +737,16 @@ namespace LibHac.FsSystem private Result InitializeExtraData() { - var pathModifiedExtraData = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref pathModifiedExtraData, ModifiedExtraDataName); + using var pathModifiedExtraData = new Path(); + Result rc = PathFunctions.SetUpFixedPath(ref pathModifiedExtraData.Ref(), ModifiedExtraDataName); if (rc.IsFailure()) return rc; - var pathCommittedExtraData = new Path(); - rc = PathFunctions.SetUpFixedPath(ref pathCommittedExtraData, CommittedExtraDataName); + using var pathCommittedExtraData = new Path(); + rc = PathFunctions.SetUpFixedPath(ref pathCommittedExtraData.Ref(), CommittedExtraDataName); if (rc.IsFailure()) return rc; - var pathSynchronizingExtraData = new Path(); - rc = PathFunctions.SetUpFixedPath(ref pathSynchronizingExtraData, SynchronizingExtraDataName); + using var pathSynchronizingExtraData = new Path(); + rc = PathFunctions.SetUpFixedPath(ref pathSynchronizingExtraData.Ref(), SynchronizingExtraDataName); if (rc.IsFailure()) return rc; // Ensure the extra data files exist @@ -827,10 +806,6 @@ namespace LibHac.FsSystem } } - pathModifiedExtraData.Dispose(); - pathCommittedExtraData.Dispose(); - pathSynchronizingExtraData.Dispose(); - return Result.Success; } @@ -947,8 +922,8 @@ namespace LibHac.FsSystem { Assert.SdkRequires(_mutex.IsLockedByCurrentThread()); - var pathExtraData = new Path(); - Result rc = GetExtraDataPath(ref pathExtraData); + using var pathExtraData = new Path(); + Result rc = GetExtraDataPath(ref pathExtraData.Ref()); if (rc.IsFailure()) return rc; rc = _baseFs.OpenFile(out IFile file, in pathExtraData, OpenMode.Write); @@ -960,7 +935,6 @@ namespace LibHac.FsSystem if (rc.IsFailure()) return rc; } - pathExtraData.Dispose(); return Result.Success; } @@ -1024,8 +998,8 @@ namespace LibHac.FsSystem Assert.SdkRequires(_mutex.IsLockedByCurrentThread()); - var pathExtraData = new Path(); - Result rc = GetExtraDataPath(ref pathExtraData); + using var pathExtraData = new Path(); + Result rc = GetExtraDataPath(ref pathExtraData.Ref()); if (rc.IsFailure()) return rc; rc = _baseFs.OpenFile(out IFile file, in pathExtraData, OpenMode.Read); @@ -1039,7 +1013,6 @@ namespace LibHac.FsSystem Assert.SdkEqual(bytesRead, Unsafe.SizeOf()); } - pathExtraData.Dispose(); return Result.Success; } diff --git a/src/LibHac/FsSystem/FileSystemExtensions.cs b/src/LibHac/FsSystem/FileSystemExtensions.cs index 30c9e821..012f8342 100644 --- a/src/LibHac/FsSystem/FileSystemExtensions.cs +++ b/src/LibHac/FsSystem/FileSystemExtensions.cs @@ -20,12 +20,12 @@ namespace LibHac.FsSystem var directoryEntryBuffer = new DirectoryEntry(); - var sourcePathNormalized = new Path(); - Result rc = InitializeFromString(ref sourcePathNormalized, sourcePath); + using var sourcePathNormalized = new Path(); + Result rc = InitializeFromString(ref sourcePathNormalized.Ref(), sourcePath); if (rc.IsFailure()) return rc; - var destPathNormalized = new Path(); - rc = InitializeFromString(ref destPathNormalized, destPath); + using var destPathNormalized = new Path(); + rc = InitializeFromString(ref destPathNormalized.Ref(), destPath); if (rc.IsFailure()) return rc; byte[] workBuffer = ArrayPool.Shared.Rent(bufferSize); @@ -285,8 +285,8 @@ namespace LibHac.FsSystem public static void SetConcatenationFileAttribute(this IFileSystem fs, string path) { - var pathNormalized = new Path(); - InitializeFromString(ref pathNormalized, path).ThrowIfFailure(); + using var pathNormalized = new Path(); + InitializeFromString(ref pathNormalized.Ref(), path).ThrowIfFailure(); fs.QueryEntry(Span.Empty, Span.Empty, QueryId.SetConcatenationFileAttribute, in pathNormalized); } @@ -299,8 +299,8 @@ namespace LibHac.FsSystem { string subPath = PathTools.Combine(path, entry.Name); - var subPathNormalized = new Path(); - InitializeFromString(ref subPathNormalized, subPath).ThrowIfFailure(); + using var subPathNormalized = new Path(); + InitializeFromString(ref subPathNormalized.Ref(), subPath).ThrowIfFailure(); if (entry.Type == DirectoryEntryType.Directory) { @@ -340,8 +340,8 @@ namespace LibHac.FsSystem public static Result EnsureDirectoryExists(this IFileSystem fs, string path) { - var pathNormalized = new Path(); - Result rc = InitializeFromString(ref pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = InitializeFromString(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; return Utility12.EnsureDirectory(fs, in pathNormalized); diff --git a/src/LibHac/FsSystem/LocalFileSystem.cs b/src/LibHac/FsSystem/LocalFileSystem.cs index b50d60ec..5b9be9e0 100644 --- a/src/LibHac/FsSystem/LocalFileSystem.cs +++ b/src/LibHac/FsSystem/LocalFileSystem.cs @@ -87,7 +87,7 @@ namespace LibHac.FsSystem // If the root path is empty, we interpret any incoming paths as rooted paths. if (rootPath == string.Empty) { - var path = new Path(); + using var path = new Path(); rc = path.InitializeAsEmpty(); if (rc.IsFailure()) return rc; @@ -128,7 +128,7 @@ namespace LibHac.FsSystem } ReadOnlySpan utf8Path = StringUtils.StringToUtf8(rootPathNormalized); - var pathNormalized = new Path(); + using var pathNormalized = new Path(); if (utf8Path.At(0) == DirectorySeparator && utf8Path.At(1) != DirectorySeparator) { @@ -154,7 +154,6 @@ namespace LibHac.FsSystem _rootPathUtf16 = _rootPath.ToString(); - pathNormalized.Dispose(); return Result.Success; } @@ -165,7 +164,7 @@ namespace LibHac.FsSystem // Always normalize the incoming path even if it claims to already be normalized // because we don't want to allow access to anything outside the root path. - var pathNormalized = new Path(); + using var pathNormalized = new Path(); Result rc = pathNormalized.Initialize(path.GetString()); if (rc.IsFailure()) return rc; @@ -178,7 +177,7 @@ namespace LibHac.FsSystem Path rootPath = _rootPath.GetPath(); - var fullPath = new Path(); + using var fullPath = new Path(); rc = fullPath.Combine(in rootPath, in pathNormalized); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/FsSystem/SubdirectoryFileSystem.cs b/src/LibHac/FsSystem/SubdirectoryFileSystem.cs index e527a018..2cac315c 100644 --- a/src/LibHac/FsSystem/SubdirectoryFileSystem.cs +++ b/src/LibHac/FsSystem/SubdirectoryFileSystem.cs @@ -48,14 +48,13 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out entryType); - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.GetEntryType(out entryType, in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } @@ -63,14 +62,13 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out freeSpace); - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.GetFreeSpaceSize(out freeSpace, in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } @@ -78,14 +76,13 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out totalSpace); - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.GetTotalSpaceSize(out totalSpace, in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } @@ -93,14 +90,13 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out timeStamp); - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.GetFileTimeStampRaw(out timeStamp, in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } @@ -108,14 +104,13 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out file); - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.OpenFile(out file, in fullPath, mode); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } @@ -123,142 +118,130 @@ namespace LibHac.FsSystem { UnsafeHelpers.SkipParamInit(out directory); - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.OpenDirectory(out directory, in fullPath, mode); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoCreateFile(in Path path, long size, CreateFileOptions option) { - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.CreateFile(in fullPath, size, option); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoDeleteFile(in Path path) { - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.DeleteFile(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoCreateDirectory(in Path path) { - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.CreateDirectory(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoDeleteDirectory(in Path path) { - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.DeleteDirectory(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoDeleteDirectoryRecursively(in Path path) { - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.DeleteDirectoryRecursively(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoCleanDirectoryRecursively(in Path path) { - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.CleanDirectoryRecursively(in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } protected override Result DoRenameFile(in Path currentPath, in Path newPath) { - var currentFullPath = new Path(); - Result rc = ResolveFullPath(ref currentFullPath, in currentPath); + using var currentFullPath = new Path(); + Result rc = ResolveFullPath(ref currentFullPath.Ref(), in currentPath); if (rc.IsFailure()) return rc; - var newFullPath = new Path(); - rc = ResolveFullPath(ref newFullPath, in newPath); + using var newFullPath = new Path(); + rc = ResolveFullPath(ref newFullPath.Ref(), in newPath); if (rc.IsFailure()) return rc; rc = _baseFileSystem.RenameFile(in currentFullPath, in newFullPath); if (rc.IsFailure()) return rc; - currentFullPath.Dispose(); - newFullPath.Dispose(); return Result.Success; } protected override Result DoRenameDirectory(in Path currentPath, in Path newPath) { - var currentFullPath = new Path(); - Result rc = ResolveFullPath(ref currentFullPath, in currentPath); + using var currentFullPath = new Path(); + Result rc = ResolveFullPath(ref currentFullPath.Ref(), in currentPath); if (rc.IsFailure()) return rc; - var newFullPath = new Path(); - rc = ResolveFullPath(ref newFullPath, in newPath); + using var newFullPath = new Path(); + rc = ResolveFullPath(ref newFullPath.Ref(), in newPath); if (rc.IsFailure()) return rc; rc = _baseFileSystem.RenameDirectory(in currentFullPath, in newFullPath); if (rc.IsFailure()) return rc; - currentFullPath.Dispose(); - newFullPath.Dispose(); return Result.Success; } protected override Result DoQueryEntry(Span outBuffer, ReadOnlySpan inBuffer, QueryId queryId, in Path path) { - var fullPath = new Path(); - Result rc = ResolveFullPath(ref fullPath, in path); + using var fullPath = new Path(); + Result rc = ResolveFullPath(ref fullPath.Ref(), in path); if (rc.IsFailure()) return rc; rc = _baseFileSystem.QueryEntry(outBuffer, inBuffer, queryId, in fullPath); if (rc.IsFailure()) return rc; - fullPath.Dispose(); return Result.Success; } diff --git a/src/LibHac/FsSystem/Utility12.cs b/src/LibHac/FsSystem/Utility12.cs index 918852a1..90fa6804 100644 --- a/src/LibHac/FsSystem/Utility12.cs +++ b/src/LibHac/FsSystem/Utility12.cs @@ -144,12 +144,12 @@ namespace LibHac.FsSystem FsIterationTask onEnterDir, FsIterationTask onExitDir, FsIterationTask onFile, ref FsIterationTaskClosure closure) { - var pathBuffer = new Path(); + using var pathBuffer = new Path(); Result rc = pathBuffer.Initialize(in rootPath); if (rc.IsFailure()) return rc; - rc = IterateDirectoryRecursivelyInternal(fs, ref pathBuffer, ref dirEntry, onEnterDir, onExitDir, onFile, - ref closure); + rc = IterateDirectoryRecursivelyInternal(fs, ref pathBuffer.Ref(), ref dirEntry, onEnterDir, onExitDir, + onFile, ref closure); if (rc.IsFailure()) return rc; return Result.Success; @@ -159,11 +159,11 @@ namespace LibHac.FsSystem FsIterationTask onEnterDir, FsIterationTask onExitDir, FsIterationTask onFile, ref FsIterationTaskClosure closure) { - var pathBuffer = new Path(); + using var pathBuffer = new Path(); Result rc = pathBuffer.Initialize(in rootPath); if (rc.IsFailure()) return rc; - return CleanupDirectoryRecursivelyInternal(fs, ref pathBuffer, ref dirEntry, onEnterDir, onExitDir, onFile, + return CleanupDirectoryRecursivelyInternal(fs, ref pathBuffer.Ref(), ref dirEntry, onEnterDir, onExitDir, onFile, ref closure); } @@ -327,8 +327,8 @@ namespace LibHac.FsSystem } } - var rootPath = new Path(); - Result rc = PathFunctions.SetUpFixedPath(ref rootPath, RootPath); + using var rootPath = new Path(); + Result rc = PathFunctions.SetUpFixedPath(ref rootPath.Ref(), RootPath); if (rc.IsFailure()) return rc; var closure = new FsIterationTaskClosure(); @@ -343,52 +343,44 @@ namespace LibHac.FsSystem private static Result EnsureDirectoryImpl(IFileSystem fileSystem, in Path path) { - var pathCopy = new Path(); + using var pathCopy = new Path(); + bool isFinished; - try + Result rc = pathCopy.Initialize(in path); + if (rc.IsFailure()) return rc; + + using var parser = new DirectoryPathParser(); + rc = parser.Initialize(ref pathCopy.Ref()); + if (rc.IsFailure()) return rc; + + do { - bool isFinished; - - Result rc = pathCopy.Initialize(in path); - if (rc.IsFailure()) return rc; - - using var parser = new DirectoryPathParser(); - rc = parser.Initialize(ref pathCopy); - if (rc.IsFailure()) return rc; - - do + // Check if the path exists + rc = fileSystem.GetEntryType(out DirectoryEntryType type, in parser.CurrentPath); + if (!rc.IsSuccess()) { - // Check if the path exists - rc = fileSystem.GetEntryType(out DirectoryEntryType type, in parser.CurrentPath); - if (!rc.IsSuccess()) - { - // Something went wrong if we get a result other than PathNotFound - if (!ResultFs.PathNotFound.Includes(rc)) - return rc; + // Something went wrong if we get a result other than PathNotFound + if (!ResultFs.PathNotFound.Includes(rc)) + return rc; - // Create the directory - rc = fileSystem.CreateDirectory(in parser.CurrentPath); - if (rc.IsFailure() && !ResultFs.PathAlreadyExists.Includes(rc)) - return rc; + // Create the directory + rc = fileSystem.CreateDirectory(in parser.CurrentPath); + if (rc.IsFailure() && !ResultFs.PathAlreadyExists.Includes(rc)) + return rc; - // Check once more if the path exists - rc = fileSystem.GetEntryType(out type, in parser.CurrentPath); - if (rc.IsFailure()) return rc; - } - - if (type == DirectoryEntryType.File) - return ResultFs.PathAlreadyExists.Log(); - - rc = parser.ReadNext(out isFinished); + // Check once more if the path exists + rc = fileSystem.GetEntryType(out type, in parser.CurrentPath); if (rc.IsFailure()) return rc; - } while (!isFinished); + } - return Result.Success; - } - finally - { - pathCopy.Dispose(); - } + if (type == DirectoryEntryType.File) + return ResultFs.PathAlreadyExists.Log(); + + rc = parser.ReadNext(out isFinished); + if (rc.IsFailure()) return rc; + } while (!isFinished); + + return Result.Success; } public static Result EnsureDirectory(IFileSystem fileSystem, in Path path) diff --git a/src/LibHac/LibHac.csproj b/src/LibHac/LibHac.csproj index 55974281..009ab3ce 100644 --- a/src/LibHac/LibHac.csproj +++ b/src/LibHac/LibHac.csproj @@ -38,7 +38,18 @@ + + + + + + + + + + + diff --git a/src/LibHac/SwitchFs.cs b/src/LibHac/SwitchFs.cs index ede41179..648fe0a5 100644 --- a/src/LibHac/SwitchFs.cs +++ b/src/LibHac/SwitchFs.cs @@ -44,11 +44,11 @@ namespace LibHac { var concatFs = new ConcatenationFileSystem(fileSystem); - var contentDirPath = new Fs.Path(); - PathFunctions.SetUpFixedPath(ref contentDirPath, "/Nintendo/Contents".ToU8String()).ThrowIfFailure(); + using var contentDirPath = new Fs.Path(); + PathFunctions.SetUpFixedPath(ref contentDirPath.Ref(), "/Nintendo/Contents".ToU8String()).ThrowIfFailure(); - var saveDirPath = new Fs.Path(); - PathFunctions.SetUpFixedPath(ref saveDirPath, "/Nintendo/save".ToU8String()).ThrowIfFailure(); + using var saveDirPath = new Fs.Path(); + PathFunctions.SetUpFixedPath(ref saveDirPath.Ref(), "/Nintendo/save".ToU8String()).ThrowIfFailure(); var contentDirFs = new SubdirectoryFileSystem(concatFs); contentDirFs.Initialize(in contentDirPath).ThrowIfFailure(); @@ -75,15 +75,15 @@ namespace LibHac if (concatFs.DirectoryExists("/save")) { - var savePath = new Fs.Path(); - PathFunctions.SetUpFixedPath(ref savePath, "/save".ToU8String()); + using var savePath = new Fs.Path(); + PathFunctions.SetUpFixedPath(ref savePath.Ref(), "/save".ToU8String()); saveDirFs = new SubdirectoryFileSystem(concatFs); saveDirFs.Initialize(in savePath).ThrowIfFailure(); } - var contentsPath = new Fs.Path(); - PathFunctions.SetUpFixedPath(ref contentsPath, "/Contents".ToU8String()); + using var contentsPath = new Fs.Path(); + PathFunctions.SetUpFixedPath(ref contentsPath.Ref(), "/Contents".ToU8String()); contentDirFs = new SubdirectoryFileSystem(concatFs); contentDirFs.Initialize(in contentsPath).ThrowIfFailure(); diff --git a/src/hactoolnet/ProcessSave.cs b/src/hactoolnet/ProcessSave.cs index cc542319..9b57dd59 100644 --- a/src/hactoolnet/ProcessSave.cs +++ b/src/hactoolnet/ProcessSave.cs @@ -330,7 +330,7 @@ namespace hactoolnet var sb = new StringBuilder(); sb.AppendLine(); - var emptyPath = new LibHac.Fs.Path(); + using var emptyPath = new LibHac.Fs.Path(); emptyPath.InitializeAsEmpty().ThrowIfFailure(); save.GetFreeSpaceSize(out long freeSpace, in emptyPath).ThrowIfFailure(); diff --git a/tests/LibHac.Tests/Fs/FsaExtensions.cs b/tests/LibHac.Tests/Fs/FsaExtensions.cs index dab65e7e..db7f77cb 100644 --- a/tests/LibHac.Tests/Fs/FsaExtensions.cs +++ b/tests/LibHac.Tests/Fs/FsaExtensions.cs @@ -8,10 +8,8 @@ namespace LibHac.Tests.Fs { public static class FsaExtensions { - private static Result SetUpPath(out Path path, string value) + private static Result SetUpPath(ref Path path, string value) { - path = new Path(); - if (value is null) return ResultFs.NullptrArgument.Log(); @@ -26,13 +24,11 @@ namespace LibHac.Tests.Fs public static Result CreateFile(this IFileSystem fs, string path, long size, CreateFileOptions option) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.CreateFile(in pathNormalized, size, option); - - pathNormalized.Dispose(); - return rc; + return fs.CreateFile(in pathNormalized, size, option); } public static Result CreateFile(this IFileSystem fs, string path, long size) @@ -42,224 +38,188 @@ namespace LibHac.Tests.Fs public static Result DeleteFile(this IFileSystem fs, string path) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.DeleteFile(in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.DeleteFile(in pathNormalized); } public static Result CreateDirectory(this IFileSystem fs, string path) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.CreateDirectory(in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.CreateDirectory(in pathNormalized); } public static Result DeleteDirectory(this IFileSystem fs, string path) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.DeleteDirectory(in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.DeleteDirectory(in pathNormalized); } public static Result DeleteDirectoryRecursively(this IFileSystem fs, string path) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.DeleteDirectoryRecursively(in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.DeleteDirectoryRecursively(in pathNormalized); } public static Result CleanDirectoryRecursively(this IFileSystem fs, string path) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.CleanDirectoryRecursively(in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.CleanDirectoryRecursively(in pathNormalized); } public static Result RenameFile(this IFileSystem fs, string currentPath, string newPath) { - Result rc = SetUpPath(out Path currentPathNormalized, currentPath); + using var currentPathNormalized = new Path(); + Result rc = SetUpPath(ref currentPathNormalized.Ref(), currentPath); if (rc.IsFailure()) return rc; - rc = SetUpPath(out Path newPathNormalized, newPath); + using var newPathNormalized = new Path(); + rc = SetUpPath(ref newPathNormalized.Ref(), newPath); if (rc.IsFailure()) return rc; - rc = fs.RenameFile(in currentPathNormalized, in newPathNormalized); - - currentPathNormalized.Dispose(); - newPathNormalized.Dispose(); - return rc; + return fs.RenameFile(in currentPathNormalized, in newPathNormalized); } public static Result RenameDirectory(this IFileSystem fs, string currentPath, string newPath) { - Result rc = SetUpPath(out Path currentPathNormalized, currentPath); + using var currentPathNormalized = new Path(); + Result rc = SetUpPath(ref currentPathNormalized.Ref(), currentPath); if (rc.IsFailure()) return rc; - rc = SetUpPath(out Path newPathNormalized, newPath); + using var newPathNormalized = new Path(); + rc = SetUpPath(ref newPathNormalized.Ref(), newPath); if (rc.IsFailure()) return rc; - rc = fs.RenameDirectory(in currentPathNormalized, in newPathNormalized); - - currentPathNormalized.Dispose(); - newPathNormalized.Dispose(); - return rc; + return fs.RenameDirectory(in currentPathNormalized, in newPathNormalized); } public static Result GetEntryType(this IFileSystem fs, out DirectoryEntryType entryType, string path) { UnsafeHelpers.SkipParamInit(out entryType); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.GetEntryType(out entryType, in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.GetEntryType(out entryType, in pathNormalized); } public static Result GetFreeSpaceSize(this IFileSystem fs, out long freeSpace, string path) { UnsafeHelpers.SkipParamInit(out freeSpace); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.GetFreeSpaceSize(out freeSpace, in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.GetFreeSpaceSize(out freeSpace, in pathNormalized); } public static Result GetTotalSpaceSize(this IFileSystem fs, out long totalSpace, string path) { UnsafeHelpers.SkipParamInit(out totalSpace); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.GetTotalSpaceSize(out totalSpace, in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.GetTotalSpaceSize(out totalSpace, in pathNormalized); } public static Result OpenFile(this IFileSystem fs, out IFile file, string path, OpenMode mode) { UnsafeHelpers.SkipParamInit(out file); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.OpenFile(out file, in pathNormalized, mode); - - pathNormalized.Dispose(); - return rc; + return fs.OpenFile(out file, in pathNormalized, mode); } public static Result OpenDirectory(this IFileSystem fs, out IDirectory directory, string path, OpenDirectoryMode mode) { UnsafeHelpers.SkipParamInit(out directory); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.OpenDirectory(out directory, in pathNormalized, mode); - - pathNormalized.Dispose(); - return rc; + return fs.OpenDirectory(out directory, in pathNormalized, mode); } public static Result GetFileTimeStampRaw(this IFileSystem fs, out FileTimeStampRaw timeStamp, string path) { UnsafeHelpers.SkipParamInit(out timeStamp); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.GetFileTimeStampRaw(out timeStamp, in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.GetFileTimeStampRaw(out timeStamp, in pathNormalized); } public static Result QueryEntry(this IFileSystem fs, Span outBuffer, ReadOnlySpan inBuffer, QueryId queryId, string path) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.QueryEntry(outBuffer, inBuffer, queryId, in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.QueryEntry(outBuffer, inBuffer, queryId, in pathNormalized); } public static Result CreateDirectory(this IAttributeFileSystem fs, string path, NxFileAttributes archiveAttribute) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.CreateDirectory(in pathNormalized, archiveAttribute); - - pathNormalized.Dispose(); - return rc; + return fs.CreateDirectory(in pathNormalized, archiveAttribute); } public static Result GetFileAttributes(this IAttributeFileSystem fs, out NxFileAttributes attributes, string path) { UnsafeHelpers.SkipParamInit(out attributes); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.GetFileAttributes(out attributes, in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.GetFileAttributes(out attributes, in pathNormalized); } public static Result SetFileAttributes(this IAttributeFileSystem fs, string path, NxFileAttributes attributes) { - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.SetFileAttributes(in pathNormalized, attributes); - - pathNormalized.Dispose(); - return rc; + return fs.SetFileAttributes(in pathNormalized, attributes); } public static Result GetFileSize(this IAttributeFileSystem fs, out long fileSize, string path) { UnsafeHelpers.SkipParamInit(out fileSize); - Result rc = SetUpPath(out Path pathNormalized, path); + using var pathNormalized = new Path(); + Result rc = SetUpPath(ref pathNormalized.Ref(), path); if (rc.IsFailure()) return rc; - rc = fs.GetFileSize(out fileSize, in pathNormalized); - - pathNormalized.Dispose(); - return rc; + return fs.GetFileSize(out fileSize, in pathNormalized); } } } diff --git a/tests/LibHac.Tests/Fs/SubdirectoryFileSystemTests.cs b/tests/LibHac.Tests/Fs/SubdirectoryFileSystemTests.cs index 0626b342..20960d66 100644 --- a/tests/LibHac.Tests/Fs/SubdirectoryFileSystemTests.cs +++ b/tests/LibHac.Tests/Fs/SubdirectoryFileSystemTests.cs @@ -20,8 +20,8 @@ namespace LibHac.Tests.Fs baseFs.CreateDirectory("/sub"); baseFs.CreateDirectory("/sub/path"); - var rootPath = new Path(); - PathFunctions.SetUpFixedPath(ref rootPath, "/sub/path".ToU8String()); + using var rootPath = new Path(); + PathFunctions.SetUpFixedPath(ref rootPath.Ref(), "/sub/path".ToU8String()); var subFs = new SubdirectoryFileSystem(baseFs); subFs.Initialize(in rootPath).ThrowIfFailure(); @@ -58,8 +58,8 @@ namespace LibHac.Tests.Fs { var baseFs = new InMemoryFileSystem(); - var rootPath = new Path(); - PathFunctions.SetUpFixedPath(ref rootPath, "/".ToU8String()); + using var rootPath = new Path(); + PathFunctions.SetUpFixedPath(ref rootPath.Ref(), "/".ToU8String()); var subFs = new SubdirectoryFileSystem(baseFs); subFs.Initialize(in rootPath).ThrowIfFailure();