Fix usages of SharedRef/UniqueRef

This commit is contained in:
Alex Barney 2021-11-02 10:48:09 -07:00
parent f9f31056ef
commit 257bdf0c46
20 changed files with 53 additions and 36 deletions

View file

@ -4,7 +4,7 @@ using System.Threading;
using InlineIL; using InlineIL;
using LibHac.Diag; using LibHac.Diag;
#pragma warning disable LH0001, LH0002, LH0003, LH0004, LH0005 #pragma warning disable LH0001
namespace LibHac.Common namespace LibHac.Common
{ {

View file

@ -5,6 +5,7 @@ using static LibHac.Fs.StringTraits;
namespace LibHac.Fs.Common namespace LibHac.Fs.Common
{ {
[NonCopyableDisposable]
public ref struct DirectoryPathParser public ref struct DirectoryPathParser
{ {
private Span<byte> _buffer; private Span<byte> _buffer;
@ -48,10 +49,10 @@ namespace LibHac.Fs.Common
} }
// Todo: Return reference when escape semantics are better // Todo: Return reference when escape semantics are better
public readonly Path GetCurrentPath() //public ref readonly Path GetCurrentPath()
{ //{
return CurrentPath; // return ref CurrentPath;
} //}
public Result ReadNext(out bool isFinished) public Result ReadNext(out bool isFinished)
{ {

View file

@ -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 bool Equals(object obj) => throw new NotSupportedException();
public override int GetHashCode() => throw new NotImplementedException(); public override int GetHashCode() => throw new NotImplementedException();

View file

@ -1,4 +1,5 @@
using System; using System;
using LibHac.Common;
using LibHac.Fs.Fsa; using LibHac.Fs.Fsa;
using LibHac.Fs.Shim; using LibHac.Fs.Shim;
using LibHac.FsSystem; using LibHac.FsSystem;
@ -23,6 +24,7 @@ namespace LibHac.Fs
} }
} }
[NonCopyable]
internal struct FileSystemClientGlobals : IDisposable internal struct FileSystemClientGlobals : IDisposable
{ {
public HorizonClient Hos; public HorizonClient Hos;

View file

@ -175,7 +175,7 @@ namespace LibHac.Fs.Shim
fs.Impl.AbortIfNeeded(rc); fs.Impl.AbortIfNeeded(rc);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
var storageAdapter = new UniqueRef<IStorage>(new StorageServiceObjectAdapter(ref storage.Ref())); using var storageAdapter = new UniqueRef<IStorage>(new StorageServiceObjectAdapter(ref storage.Ref()));
if (!storageAdapter.HasValue) if (!storageAdapter.HasValue)
return ResultFs.AllocationMemoryFailedInBisC.Log(); return ResultFs.AllocationMemoryFailedInBisC.Log();

View file

@ -76,7 +76,7 @@ namespace LibHac.Fs.Shim
programId); programId);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
var fileSystemAdapter = using var fileSystemAdapter =
new UniqueRef<IFileSystem>(new FileSystemServiceObjectAdapter(ref fileSystem.Ref())); new UniqueRef<IFileSystem>(new FileSystemServiceObjectAdapter(ref fileSystem.Ref()));
if (!fileSystemAdapter.HasValue) if (!fileSystemAdapter.HasValue)

View file

@ -501,12 +501,12 @@ namespace LibHac.Fs.Shim
Result rc = fileSystemProxy.Get.OpenSaveDataInfoReaderBySaveDataSpaceId(ref reader.Ref(), spaceId); Result rc = fileSystemProxy.Get.OpenSaveDataInfoReaderBySaveDataSpaceId(ref reader.Ref(), spaceId);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
var iterator = new UniqueRef<SaveDataIterator>(new SaveDataIterator(fs.Fs, ref reader.Ref())); using var iterator = new UniqueRef<SaveDataIterator>(new SaveDataIterator(fs.Fs, ref reader.Ref()));
if (!iterator.HasValue) if (!iterator.HasValue)
return ResultFs.AllocationMemoryFailedInSaveDataManagementA.Log(); return ResultFs.AllocationMemoryFailedInSaveDataManagementA.Log();
outIterator.Set(ref iterator); outIterator.Set(ref iterator.Ref());
return Result.Success; return Result.Success;
} }
@ -520,12 +520,12 @@ namespace LibHac.Fs.Shim
Result rc = fileSystemProxy.Get.OpenSaveDataInfoReaderWithFilter(ref reader.Ref(), spaceId, in filter); Result rc = fileSystemProxy.Get.OpenSaveDataInfoReaderWithFilter(ref reader.Ref(), spaceId, in filter);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
var iterator = new UniqueRef<SaveDataIterator>(new SaveDataIterator(fs.Fs, ref reader.Ref())); using var iterator = new UniqueRef<SaveDataIterator>(new SaveDataIterator(fs.Fs, ref reader.Ref()));
if (!iterator.HasValue) if (!iterator.HasValue)
return ResultFs.AllocationMemoryFailedInSaveDataManagementA.Log(); return ResultFs.AllocationMemoryFailedInSaveDataManagementA.Log();
outIterator.Set(ref iterator); outIterator.Set(ref iterator.Ref());
return Result.Success; return Result.Success;
} }

View file

@ -180,8 +180,13 @@ namespace LibHac.FsSrv
_currentProcess = processId; _currentProcess = processId;
// Initialize the NCA file system service // Initialize the NCA file system service
_ncaFsService = NcaFileSystemService.CreateShared(Globals.NcaFileSystemServiceImpl, processId); using SharedRef<NcaFileSystemService> ncaFsService =
_saveFsService = SaveDataFileSystemService.CreateShared(Globals.SaveDataFileSystemServiceImpl, processId); NcaFileSystemService.CreateShared(Globals.NcaFileSystemServiceImpl, processId);
_ncaFsService.SetByMove(ref ncaFsService.Ref());
using SharedRef<SaveDataFileSystemService> saveFsService =
SaveDataFileSystemService.CreateShared(Globals.SaveDataFileSystemServiceImpl, processId);
_saveFsService.SetByMove(ref saveFsService.Ref());
return Result.Success; return Result.Success;
} }

View file

@ -461,7 +461,7 @@ namespace LibHac.FsSrv
return rc; return rc;
// Delete the actual save data. // Delete the actual save data.
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
rc = _serviceImpl.DeleteSaveDataFileSystem(spaceId, saveDataId, wipeSaveFile, in saveDataRootPath); rc = _serviceImpl.DeleteSaveDataFileSystem(spaceId, saveDataId, wipeSaveFile, in saveDataRootPath);
if (rc.IsFailure() && !ResultFs.PathNotFound.Includes(rc)) if (rc.IsFailure() && !ResultFs.PathNotFound.Includes(rc))
return 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. // 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, rc = _serviceImpl.CreateSaveDataFileSystem(saveDataId, in attribute, in creationInfo,
in saveDataRootPath, in hashSalt, false); in saveDataRootPath, in hashSalt, false);
@ -1020,7 +1020,7 @@ namespace LibHac.FsSrv
} }
// Open the save data using its ID // Open the save data using its ID
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
Result saveFsResult = _serviceImpl.OpenSaveDataFileSystem(ref outFileSystem, spaceId, tempSaveDataId, Result saveFsResult = _serviceImpl.OpenSaveDataFileSystem(ref outFileSystem, spaceId, tempSaveDataId,
in saveDataRootPath, openReadOnly, attribute.Type, cacheExtraData); in saveDataRootPath, openReadOnly, attribute.Type, cacheExtraData);
@ -1205,7 +1205,7 @@ namespace LibHac.FsSrv
if (!accessibility.CanRead || !accessibility.CanWrite) if (!accessibility.CanRead || !accessibility.CanWrite)
return ResultFs.PermissionDenied.Log(); return ResultFs.PermissionDenied.Log();
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
bool useAsyncFileSystem = !_serviceImpl.IsAllowedDirectorySaveData(spaceId, in saveDataRootPath); bool useAsyncFileSystem = !_serviceImpl.IsAllowedDirectorySaveData(spaceId, in saveDataRootPath);
using var fileSystem = new SharedRef<IFileSystem>(); using var fileSystem = new SharedRef<IFileSystem>();
@ -1220,7 +1220,7 @@ namespace LibHac.FsSrv
Result ReadExtraData(out SaveDataExtraData data) Result ReadExtraData(out SaveDataExtraData data)
{ {
Path savePath = _saveDataRootPath.DangerousGetPath(); using Path savePath = _saveDataRootPath.DangerousGetPath();
return _serviceImpl.ReadSaveDataFileSystemExtraData(out data, spaceId, saveDataId, type, return _serviceImpl.ReadSaveDataFileSystemExtraData(out data, spaceId, saveDataId, type,
in savePath); in savePath);
} }
@ -1342,7 +1342,7 @@ namespace LibHac.FsSrv
ReadExtraData); ReadExtraData);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
Path saveDataRootPath = _saveDataRootPath.DangerousGetPath(); using Path saveDataRootPath = _saveDataRootPath.DangerousGetPath();
rc = _serviceImpl.ReadSaveDataFileSystemExtraData(out SaveDataExtraData tempExtraData, resolvedSpaceId, rc = _serviceImpl.ReadSaveDataFileSystemExtraData(out SaveDataExtraData tempExtraData, resolvedSpaceId,
saveDataId, key.Type, in saveDataRootPath); saveDataId, key.Type, in saveDataRootPath);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;

View file

@ -8,7 +8,7 @@ namespace LibHac.FsSystem
{ {
public class AesXtsFile : IFile public class AesXtsFile : IFile
{ {
private UniqueRef<IFile> BaseFile { get; } private UniqueRef<IFile> _baseFile;
private U8String Path { get; } private U8String Path { get; }
private byte[] KekSeed { get; } private byte[] KekSeed { get; }
private byte[] VerificationKey { get; } private byte[] VerificationKey { get; }
@ -23,15 +23,15 @@ namespace LibHac.FsSystem
public AesXtsFile(OpenMode mode, ref UniqueRef<IFile> baseFile, U8String path, ReadOnlySpan<byte> kekSeed, ReadOnlySpan<byte> verificationKey, int blockSize) public AesXtsFile(OpenMode mode, ref UniqueRef<IFile> baseFile, U8String path, ReadOnlySpan<byte> kekSeed, ReadOnlySpan<byte> verificationKey, int blockSize)
{ {
Mode = mode; Mode = mode;
BaseFile = new UniqueRef<IFile>(ref baseFile); _baseFile = new UniqueRef<IFile>(ref baseFile);
Path = path; Path = path;
KekSeed = kekSeed.ToArray(); KekSeed = kekSeed.ToArray();
VerificationKey = verificationKey.ToArray(); VerificationKey = verificationKey.ToArray();
BlockSize = blockSize; 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)) if (!Header.TryDecryptHeader(Path.ToString(), KekSeed, VerificationKey))
{ {
@ -43,7 +43,7 @@ namespace LibHac.FsSystem
ThrowHelper.ThrowResult(ResultFs.AesXtsFileTooShort.Value, "NAX0 key derivation failed."); 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); var encStorage = new SubStorage(fileStorage, HeaderLength, fileSize - HeaderLength);
encStorage.SetResizable(true); encStorage.SetResizable(true);
@ -116,7 +116,7 @@ namespace LibHac.FsSystem
{ {
Header.SetSize(size, VerificationKey); 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; if (rc.IsFailure()) return rc;
return BaseStorage.SetSize(Alignment.AlignUp(size, 0x10)); return BaseStorage.SetSize(Alignment.AlignUp(size, 0x10));
@ -126,7 +126,7 @@ namespace LibHac.FsSystem
{ {
BaseStorage.Flush(); BaseStorage.Flush();
BaseStorage.Dispose(); BaseStorage.Dispose();
BaseFile.Destroy(); _baseFile.Destroy();
base.Dispose(); base.Dispose();
} }

View file

@ -457,7 +457,7 @@ namespace LibHac.FsSystem
Unsafe.SkipInit(out DirectoryEntry entry); Unsafe.SkipInit(out DirectoryEntry entry);
using var directory = new UniqueRef<IDirectory>(); using var directory = new UniqueRef<IDirectory>();
Path path = _path.DangerousGetPath(); using Path path = _path.DangerousGetPath();
Result rc = _baseFileSystem.OpenDirectory(ref directory.Ref(), in path, Result rc = _baseFileSystem.OpenDirectory(ref directory.Ref(), in path,
OpenDirectoryMode.All | OpenDirectoryMode.NoFileSize); OpenDirectoryMode.All | OpenDirectoryMode.NoFileSize);

View file

@ -178,6 +178,7 @@ namespace LibHac.FsSystem
base.Dispose(); base.Dispose();
} }
[NonCopyable]
private ref struct RetryClosure private ref struct RetryClosure
{ {
public DirectorySaveDataFileSystem This; public DirectorySaveDataFileSystem This;

View file

@ -159,11 +159,15 @@ namespace LibHac.FsSystem
bool ignoreCase = searchOptions.HasFlag(SearchOptions.CaseInsensitive); bool ignoreCase = searchOptions.HasFlag(SearchOptions.CaseInsensitive);
bool recurse = searchOptions.HasFlag(SearchOptions.RecurseSubdirectories); bool recurse = searchOptions.HasFlag(SearchOptions.RecurseSubdirectories);
var pathNormalized = new Path();
InitializeFromString(ref pathNormalized, path).ThrowIfFailure();
using var directory = new UniqueRef<IDirectory>(); using var directory = new UniqueRef<IDirectory>();
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) while (true)
{ {

View file

@ -270,7 +270,7 @@ namespace LibHac.FsSystem
// todo: Efficient way to remove duplicates // todo: Efficient way to remove duplicates
var names = new HashSet<string>(); var names = new HashSet<string>();
Path path = _path.DangerousGetPath(); using Path path = _path.DangerousGetPath();
using var dir = new UniqueRef<IDirectory>(); using var dir = new UniqueRef<IDirectory>();
// Open new directories for each source because we need to remove duplicate entries // Open new directories for each source because we need to remove duplicate entries

View file

@ -175,7 +175,7 @@ namespace LibHac.FsSystem
rc = pathNormalized.Normalize(pathFlags); rc = pathNormalized.Normalize(pathFlags);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
Path rootPath = _rootPath.DangerousGetPath(); using Path rootPath = _rootPath.DangerousGetPath();
using var fullPath = new Path(); using var fullPath = new Path();
rc = fullPath.Combine(in rootPath, in pathNormalized); rc = fullPath.Combine(in rootPath, in pathNormalized);

View file

@ -22,7 +22,7 @@ namespace LibHac.FsSystem
Result rc = Initialize(baseStorage.Get); Result rc = Initialize(baseStorage.Get);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
_baseStorageShared = SharedRef<IStorage>.CreateMove(ref baseStorage); _baseStorageShared.SetByMove(ref baseStorage);
return Result.Success; return Result.Success;
} }

View file

@ -39,7 +39,7 @@ namespace LibHac.FsSystem
private Result ResolveFullPath(ref Path outPath, in Path relativePath) 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); return outPath.Combine(in rootPath, in relativePath);
} }

View file

@ -25,6 +25,7 @@ namespace LibHac.FsSystem
/// The struct must also be manually passed through the <see cref="Utility.IterateDirectoryRecursively"/> method. /// The struct must also be manually passed through the <see cref="Utility.IterateDirectoryRecursively"/> 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. /// And because ref fields aren't as thing as of C# 10, some ref structs may have to be copied into the closure struct.
/// </remarks> /// </remarks>
[NonCopyable]
public ref struct FsIterationTaskClosure public ref struct FsIterationTaskClosure
{ {
public Span<byte> Buffer; public Span<byte> Buffer;

View file

@ -1,4 +1,5 @@
using System; using System;
using LibHac.Common;
namespace LibHac.Lr namespace LibHac.Lr
{ {
@ -18,6 +19,7 @@ namespace LibHac.Lr
} }
} }
[NonCopyable]
internal struct LrClientGlobals : IDisposable internal struct LrClientGlobals : IDisposable
{ {
public HorizonClient Hos; public HorizonClient Hos;

View file

@ -6,6 +6,7 @@ using LibHac.Os;
namespace LibHac.Lr namespace LibHac.Lr
{ {
[NonCopyable]
internal struct LrServiceGlobals : IDisposable internal struct LrServiceGlobals : IDisposable
{ {
public SharedRef<ILocationResolverManager> LocationResolver; public SharedRef<ILocationResolverManager> LocationResolver;