diff --git a/src/LibHac/Common/SharedRef.cs b/src/LibHac/Common/SharedRef.cs index 3610bf7f..5f1b603f 100644 --- a/src/LibHac/Common/SharedRef.cs +++ b/src/LibHac/Common/SharedRef.cs @@ -4,7 +4,7 @@ using System.Threading; using InlineIL; using LibHac.Diag; -#pragma warning disable LH0001, LH0002, LH0003, LH0004, LH0005 +#pragma warning disable LH0001 namespace LibHac.Common { diff --git a/src/LibHac/Fs/Common/DirectoryPathParser.cs b/src/LibHac/Fs/Common/DirectoryPathParser.cs index 49f7cfa0..4eb7887e 100644 --- a/src/LibHac/Fs/Common/DirectoryPathParser.cs +++ b/src/LibHac/Fs/Common/DirectoryPathParser.cs @@ -5,6 +5,7 @@ using static LibHac.Fs.StringTraits; namespace LibHac.Fs.Common { + [NonCopyableDisposable] public ref struct DirectoryPathParser { private Span _buffer; @@ -48,10 +49,10 @@ namespace LibHac.Fs.Common } // Todo: Return reference when escape semantics are better - public readonly Path GetCurrentPath() - { - return CurrentPath; - } + //public ref readonly Path GetCurrentPath() + //{ + // return ref CurrentPath; + //} public Result ReadNext(out bool isFinished) { diff --git a/src/LibHac/Fs/Common/Path.cs b/src/LibHac/Fs/Common/Path.cs index d01bb9bd..e580423a 100644 --- a/src/LibHac/Fs/Common/Path.cs +++ b/src/LibHac/Fs/Common/Path.cs @@ -1077,7 +1077,7 @@ namespace LibHac.Fs } } - public override string ToString() => StringUtils.Utf8ZToString(_string); + public override readonly string ToString() => StringUtils.Utf8ZToString(_string); public override bool Equals(object obj) => throw new NotSupportedException(); public override int GetHashCode() => throw new NotImplementedException(); diff --git a/src/LibHac/Fs/FileSystemClient.cs b/src/LibHac/Fs/FileSystemClient.cs index 703a1134..a9a91d7b 100644 --- a/src/LibHac/Fs/FileSystemClient.cs +++ b/src/LibHac/Fs/FileSystemClient.cs @@ -1,4 +1,5 @@ using System; +using LibHac.Common; using LibHac.Fs.Fsa; using LibHac.Fs.Shim; using LibHac.FsSystem; @@ -23,6 +24,7 @@ namespace LibHac.Fs } } + [NonCopyable] internal struct FileSystemClientGlobals : IDisposable { public HorizonClient Hos; diff --git a/src/LibHac/Fs/Shim/Bis.cs b/src/LibHac/Fs/Shim/Bis.cs index f10c7292..b5a5fe8f 100644 --- a/src/LibHac/Fs/Shim/Bis.cs +++ b/src/LibHac/Fs/Shim/Bis.cs @@ -175,7 +175,7 @@ namespace LibHac.Fs.Shim fs.Impl.AbortIfNeeded(rc); if (rc.IsFailure()) return rc; - var storageAdapter = new UniqueRef(new StorageServiceObjectAdapter(ref storage.Ref())); + using var storageAdapter = new UniqueRef(new StorageServiceObjectAdapter(ref storage.Ref())); if (!storageAdapter.HasValue) return ResultFs.AllocationMemoryFailedInBisC.Log(); diff --git a/src/LibHac/Fs/Shim/Code.cs b/src/LibHac/Fs/Shim/Code.cs index 305e4d9b..014d9252 100644 --- a/src/LibHac/Fs/Shim/Code.cs +++ b/src/LibHac/Fs/Shim/Code.cs @@ -76,7 +76,7 @@ namespace LibHac.Fs.Shim programId); if (rc.IsFailure()) return rc; - var fileSystemAdapter = + using var fileSystemAdapter = new UniqueRef(new FileSystemServiceObjectAdapter(ref fileSystem.Ref())); if (!fileSystemAdapter.HasValue) diff --git a/src/LibHac/Fs/Shim/SaveDataManagement.cs b/src/LibHac/Fs/Shim/SaveDataManagement.cs index 74ff30c5..fe60e5ee 100644 --- a/src/LibHac/Fs/Shim/SaveDataManagement.cs +++ b/src/LibHac/Fs/Shim/SaveDataManagement.cs @@ -501,12 +501,12 @@ namespace LibHac.Fs.Shim Result rc = fileSystemProxy.Get.OpenSaveDataInfoReaderBySaveDataSpaceId(ref reader.Ref(), spaceId); if (rc.IsFailure()) return rc; - var iterator = new UniqueRef(new SaveDataIterator(fs.Fs, ref reader.Ref())); + using var iterator = new UniqueRef(new SaveDataIterator(fs.Fs, ref reader.Ref())); if (!iterator.HasValue) return ResultFs.AllocationMemoryFailedInSaveDataManagementA.Log(); - outIterator.Set(ref iterator); + outIterator.Set(ref iterator.Ref()); return Result.Success; } @@ -520,12 +520,12 @@ namespace LibHac.Fs.Shim Result rc = fileSystemProxy.Get.OpenSaveDataInfoReaderWithFilter(ref reader.Ref(), spaceId, in filter); if (rc.IsFailure()) return rc; - var iterator = new UniqueRef(new SaveDataIterator(fs.Fs, ref reader.Ref())); + using var iterator = new UniqueRef(new SaveDataIterator(fs.Fs, ref reader.Ref())); if (!iterator.HasValue) return ResultFs.AllocationMemoryFailedInSaveDataManagementA.Log(); - outIterator.Set(ref iterator); + outIterator.Set(ref iterator.Ref()); return Result.Success; } diff --git a/src/LibHac/FsSrv/FileSystemProxyImpl.cs b/src/LibHac/FsSrv/FileSystemProxyImpl.cs index 7d3cd608..8a806490 100644 --- a/src/LibHac/FsSrv/FileSystemProxyImpl.cs +++ b/src/LibHac/FsSrv/FileSystemProxyImpl.cs @@ -180,8 +180,13 @@ namespace LibHac.FsSrv _currentProcess = processId; // Initialize the NCA file system service - _ncaFsService = NcaFileSystemService.CreateShared(Globals.NcaFileSystemServiceImpl, processId); - _saveFsService = SaveDataFileSystemService.CreateShared(Globals.SaveDataFileSystemServiceImpl, processId); + using SharedRef ncaFsService = + NcaFileSystemService.CreateShared(Globals.NcaFileSystemServiceImpl, processId); + _ncaFsService.SetByMove(ref ncaFsService.Ref()); + + using SharedRef saveFsService = + SaveDataFileSystemService.CreateShared(Globals.SaveDataFileSystemServiceImpl, processId); + _saveFsService.SetByMove(ref saveFsService.Ref()); return Result.Success; } diff --git a/src/LibHac/FsSrv/SaveDataFileSystemService.cs b/src/LibHac/FsSrv/SaveDataFileSystemService.cs index 83cf6fc9..207ddefa 100644 --- a/src/LibHac/FsSrv/SaveDataFileSystemService.cs +++ b/src/LibHac/FsSrv/SaveDataFileSystemService.cs @@ -461,7 +461,7 @@ namespace LibHac.FsSrv return rc; // Delete the actual save data. - Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); + using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); rc = _serviceImpl.DeleteSaveDataFileSystem(spaceId, saveDataId, wipeSaveFile, in saveDataRootPath); if (rc.IsFailure() && !ResultFs.PathNotFound.Includes(rc)) return rc; @@ -774,7 +774,7 @@ namespace LibHac.FsSrv } // After the new save was added to the save indexer, create the save data file or directory. - Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); + using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); rc = _serviceImpl.CreateSaveDataFileSystem(saveDataId, in attribute, in creationInfo, in saveDataRootPath, in hashSalt, false); @@ -1020,7 +1020,7 @@ namespace LibHac.FsSrv } // Open the save data using its ID - Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); + using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); Result saveFsResult = _serviceImpl.OpenSaveDataFileSystem(ref outFileSystem, spaceId, tempSaveDataId, in saveDataRootPath, openReadOnly, attribute.Type, cacheExtraData); @@ -1205,7 +1205,7 @@ namespace LibHac.FsSrv if (!accessibility.CanRead || !accessibility.CanWrite) return ResultFs.PermissionDenied.Log(); - Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); + using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); bool useAsyncFileSystem = !_serviceImpl.IsAllowedDirectorySaveData(spaceId, in saveDataRootPath); using var fileSystem = new SharedRef(); @@ -1220,7 +1220,7 @@ namespace LibHac.FsSrv Result ReadExtraData(out SaveDataExtraData data) { - Path savePath = _saveDataRootPath.DangerousGetPath(); + using Path savePath = _saveDataRootPath.DangerousGetPath(); return _serviceImpl.ReadSaveDataFileSystemExtraData(out data, spaceId, saveDataId, type, in savePath); } @@ -1342,7 +1342,7 @@ namespace LibHac.FsSrv ReadExtraData); if (rc.IsFailure()) return rc; - Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); + using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); rc = _serviceImpl.ReadSaveDataFileSystemExtraData(out SaveDataExtraData tempExtraData, resolvedSpaceId, saveDataId, key.Type, in saveDataRootPath); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/FsSystem/AesXtsFile.cs b/src/LibHac/FsSystem/AesXtsFile.cs index caaf43de..d57d0921 100644 --- a/src/LibHac/FsSystem/AesXtsFile.cs +++ b/src/LibHac/FsSystem/AesXtsFile.cs @@ -8,7 +8,7 @@ namespace LibHac.FsSystem { public class AesXtsFile : IFile { - private UniqueRef BaseFile { get; } + private UniqueRef _baseFile; private U8String Path { get; } private byte[] KekSeed { get; } private byte[] VerificationKey { get; } @@ -23,15 +23,15 @@ namespace LibHac.FsSystem public AesXtsFile(OpenMode mode, ref UniqueRef baseFile, U8String path, ReadOnlySpan kekSeed, ReadOnlySpan verificationKey, int blockSize) { Mode = mode; - BaseFile = new UniqueRef(ref baseFile); + _baseFile = new UniqueRef(ref baseFile); Path = path; KekSeed = kekSeed.ToArray(); VerificationKey = verificationKey.ToArray(); BlockSize = blockSize; - Header = new AesXtsFileHeader(BaseFile.Get); + Header = new AesXtsFileHeader(_baseFile.Get); - BaseFile.Get.GetSize(out long fileSize).ThrowIfFailure(); + _baseFile.Get.GetSize(out long fileSize).ThrowIfFailure(); if (!Header.TryDecryptHeader(Path.ToString(), KekSeed, VerificationKey)) { @@ -43,7 +43,7 @@ namespace LibHac.FsSystem ThrowHelper.ThrowResult(ResultFs.AesXtsFileTooShort.Value, "NAX0 key derivation failed."); } - var fileStorage = new FileStorage(BaseFile.Get); + var fileStorage = new FileStorage(_baseFile.Get); var encStorage = new SubStorage(fileStorage, HeaderLength, fileSize - HeaderLength); encStorage.SetResizable(true); @@ -116,7 +116,7 @@ namespace LibHac.FsSystem { Header.SetSize(size, VerificationKey); - Result rc = BaseFile.Get.Write(0, Header.ToBytes(false)); + Result rc = _baseFile.Get.Write(0, Header.ToBytes(false)); if (rc.IsFailure()) return rc; return BaseStorage.SetSize(Alignment.AlignUp(size, 0x10)); @@ -126,7 +126,7 @@ namespace LibHac.FsSystem { BaseStorage.Flush(); BaseStorage.Dispose(); - BaseFile.Destroy(); + _baseFile.Destroy(); base.Dispose(); } diff --git a/src/LibHac/FsSystem/ConcatenationFileSystem.cs b/src/LibHac/FsSystem/ConcatenationFileSystem.cs index 28599349..0f3c9167 100644 --- a/src/LibHac/FsSystem/ConcatenationFileSystem.cs +++ b/src/LibHac/FsSystem/ConcatenationFileSystem.cs @@ -457,7 +457,7 @@ namespace LibHac.FsSystem Unsafe.SkipInit(out DirectoryEntry entry); using var directory = new UniqueRef(); - Path path = _path.DangerousGetPath(); + using Path path = _path.DangerousGetPath(); Result rc = _baseFileSystem.OpenDirectory(ref directory.Ref(), in path, OpenDirectoryMode.All | OpenDirectoryMode.NoFileSize); diff --git a/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs b/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs index 1048bdbc..1988f951 100644 --- a/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs +++ b/src/LibHac/FsSystem/DirectorySaveDataFileSystem.cs @@ -178,6 +178,7 @@ namespace LibHac.FsSystem base.Dispose(); } + [NonCopyable] private ref struct RetryClosure { public DirectorySaveDataFileSystem This; diff --git a/src/LibHac/FsSystem/FileSystemExtensions.cs b/src/LibHac/FsSystem/FileSystemExtensions.cs index 09c9f766..a93a399f 100644 --- a/src/LibHac/FsSystem/FileSystemExtensions.cs +++ b/src/LibHac/FsSystem/FileSystemExtensions.cs @@ -159,11 +159,15 @@ namespace LibHac.FsSystem bool ignoreCase = searchOptions.HasFlag(SearchOptions.CaseInsensitive); bool recurse = searchOptions.HasFlag(SearchOptions.RecurseSubdirectories); - var pathNormalized = new Path(); - InitializeFromString(ref pathNormalized, path).ThrowIfFailure(); - using var directory = new UniqueRef(); - fileSystem.OpenDirectory(ref directory.Ref(), in pathNormalized, OpenDirectoryMode.All).ThrowIfFailure(); + + using (var pathNormalized = new Path()) + { + InitializeFromString(ref pathNormalized.Ref(), path).ThrowIfFailure(); + + fileSystem.OpenDirectory(ref directory.Ref(), in pathNormalized, OpenDirectoryMode.All) + .ThrowIfFailure(); + } while (true) { diff --git a/src/LibHac/FsSystem/LayeredFileSystem.cs b/src/LibHac/FsSystem/LayeredFileSystem.cs index eb350444..1435e211 100644 --- a/src/LibHac/FsSystem/LayeredFileSystem.cs +++ b/src/LibHac/FsSystem/LayeredFileSystem.cs @@ -270,7 +270,7 @@ namespace LibHac.FsSystem // todo: Efficient way to remove duplicates var names = new HashSet(); - Path path = _path.DangerousGetPath(); + using Path path = _path.DangerousGetPath(); using var dir = new UniqueRef(); // Open new directories for each source because we need to remove duplicate entries diff --git a/src/LibHac/FsSystem/LocalFileSystem.cs b/src/LibHac/FsSystem/LocalFileSystem.cs index 9877da62..91c5497a 100644 --- a/src/LibHac/FsSystem/LocalFileSystem.cs +++ b/src/LibHac/FsSystem/LocalFileSystem.cs @@ -175,7 +175,7 @@ namespace LibHac.FsSystem rc = pathNormalized.Normalize(pathFlags); if (rc.IsFailure()) return rc; - Path rootPath = _rootPath.DangerousGetPath(); + using Path rootPath = _rootPath.DangerousGetPath(); using var fullPath = new Path(); rc = fullPath.Combine(in rootPath, in pathNormalized); diff --git a/src/LibHac/FsSystem/PartitionFileSystemCore.cs b/src/LibHac/FsSystem/PartitionFileSystemCore.cs index 7f833ac7..b6074534 100644 --- a/src/LibHac/FsSystem/PartitionFileSystemCore.cs +++ b/src/LibHac/FsSystem/PartitionFileSystemCore.cs @@ -22,7 +22,7 @@ namespace LibHac.FsSystem Result rc = Initialize(baseStorage.Get); if (rc.IsFailure()) return rc; - _baseStorageShared = SharedRef.CreateMove(ref baseStorage); + _baseStorageShared.SetByMove(ref baseStorage); return Result.Success; } diff --git a/src/LibHac/FsSystem/SubdirectoryFileSystem.cs b/src/LibHac/FsSystem/SubdirectoryFileSystem.cs index 14d29240..df20492c 100644 --- a/src/LibHac/FsSystem/SubdirectoryFileSystem.cs +++ b/src/LibHac/FsSystem/SubdirectoryFileSystem.cs @@ -39,7 +39,7 @@ namespace LibHac.FsSystem private Result ResolveFullPath(ref Path outPath, in Path relativePath) { - Path rootPath = _rootPath.DangerousGetPath(); + using Path rootPath = _rootPath.DangerousGetPath(); return outPath.Combine(in rootPath, in relativePath); } diff --git a/src/LibHac/FsSystem/Utility.cs b/src/LibHac/FsSystem/Utility.cs index 0ab7b97a..37cc468d 100644 --- a/src/LibHac/FsSystem/Utility.cs +++ b/src/LibHac/FsSystem/Utility.cs @@ -25,6 +25,7 @@ namespace LibHac.FsSystem /// The struct must also be manually passed through the method. /// And because ref fields aren't as thing as of C# 10, some ref structs may have to be copied into the closure struct. /// + [NonCopyable] public ref struct FsIterationTaskClosure { public Span Buffer; diff --git a/src/LibHac/Lr/LrClient.cs b/src/LibHac/Lr/LrClient.cs index f8d47414..d71759dc 100644 --- a/src/LibHac/Lr/LrClient.cs +++ b/src/LibHac/Lr/LrClient.cs @@ -1,4 +1,5 @@ using System; +using LibHac.Common; namespace LibHac.Lr { @@ -18,6 +19,7 @@ namespace LibHac.Lr } } + [NonCopyable] internal struct LrClientGlobals : IDisposable { public HorizonClient Hos; diff --git a/src/LibHac/Lr/LrService.cs b/src/LibHac/Lr/LrService.cs index 9da646dd..bf30744b 100644 --- a/src/LibHac/Lr/LrService.cs +++ b/src/LibHac/Lr/LrService.cs @@ -6,6 +6,7 @@ using LibHac.Os; namespace LibHac.Lr { + [NonCopyable] internal struct LrServiceGlobals : IDisposable { public SharedRef LocationResolver;