Make FileHandle and DirectoryHandle implement IDisposable

This commit is contained in:
Alex Barney 2021-04-18 23:55:47 -07:00
parent 646055de5a
commit 880916e117
4 changed files with 38 additions and 17 deletions

View file

@ -1,4 +1,6 @@
namespace LibHac.Fs
using LibHac.Fs.Fsa;
namespace LibHac.Fs
{
public readonly struct DirectoryHandle
{
@ -10,5 +12,13 @@
{
Directory = directory;
}
public void Dispose()
{
if (IsValid)
{
Directory.GetParent().FsClient.CloseDirectory(this);
}
}
}
}
}

View file

@ -1,6 +1,9 @@
namespace LibHac.Fs
using System;
using LibHac.Fs.Fsa;
namespace LibHac.Fs
{
public readonly struct FileHandle
public readonly struct FileHandle : IDisposable
{
internal readonly Impl.FileAccessor File;
@ -10,5 +13,13 @@
{
File = file;
}
public void Dispose()
{
if (IsValid)
{
File.FsClient.CloseFile(this);
}
}
}
}

View file

@ -26,12 +26,12 @@ namespace LibHac.Fs.Impl
// ReSharper disable once NotAccessedField.Local
private int _pathHashIndex;
private FileSystemClient _fsClient;
internal FileSystemClient FsClient { get; }
public FileAccessor(FileSystemClient fsClient, ref IFile file, FileSystemAccessor parentFileSystem,
OpenMode mode)
{
_fsClient = fsClient;
FsClient = fsClient;
_file = Shared.Move(ref file);
_parentFileSystem = parentFileSystem;
@ -91,18 +91,18 @@ namespace LibHac.Fs.Impl
if (_lastResult.IsFailure())
{
if (_fsClient.Impl.IsEnabledAccessLog() && _fsClient.Impl.IsEnabledHandleAccessLog(handle))
if (FsClient.Impl.IsEnabledAccessLog() && FsClient.Impl.IsEnabledHandleAccessLog(handle))
{
Tick start = _fsClient.Hos.Os.GetSystemTick();
Tick start = FsClient.Hos.Os.GetSystemTick();
rc = _lastResult;
Tick end = _fsClient.Hos.Os.GetSystemTick();
Tick end = FsClient.Hos.Os.GetSystemTick();
var sb = new U8StringBuilder(logBuffer, true);
sb.Append(LogOffset).AppendFormat(offset)
.Append(LogSize).AppendFormat(destination.Length)
.Append(LogReadSize).AppendFormat(AccessLogImpl.DereferenceOutValue(in bytesRead, rc));
_fsClient.Impl.OutputAccessLog(rc, start, end, handle, new U8Span(logBuffer),
FsClient.Impl.OutputAccessLog(rc, start, end, handle, new U8Span(logBuffer),
nameof(UserFile.ReadFile));
}
@ -123,18 +123,18 @@ namespace LibHac.Fs.Impl
}
else
{
if (_fsClient.Impl.IsEnabledAccessLog() && _fsClient.Impl.IsEnabledHandleAccessLog(handle))
if (FsClient.Impl.IsEnabledAccessLog() && FsClient.Impl.IsEnabledHandleAccessLog(handle))
{
Tick start = _fsClient.Hos.Os.GetSystemTick();
Tick start = FsClient.Hos.Os.GetSystemTick();
rc = ReadWithoutCacheAccessLog(out bytesRead, offset, destination, in option);
Tick end = _fsClient.Hos.Os.GetSystemTick();
Tick end = FsClient.Hos.Os.GetSystemTick();
var sb = new U8StringBuilder(logBuffer, true);
sb.Append(LogOffset).AppendFormat(offset)
.Append(LogSize).AppendFormat(destination.Length)
.Append(LogReadSize).AppendFormat(AccessLogImpl.DereferenceOutValue(in bytesRead, rc));
_fsClient.Impl.OutputAccessLog(rc, start, end, handle, new U8Span(logBuffer),
FsClient.Impl.OutputAccessLog(rc, start, end, handle, new U8Span(logBuffer),
nameof(UserFile.ReadFile));
}
else

View file

@ -25,13 +25,13 @@ namespace LibHac.Fs.Impl
private bool _isPathCacheAttached;
private IMultiCommitTarget _multiCommitTarget;
private FileSystemClient _fsClient;
internal FileSystemClient FsClient { get; }
public FileSystemAccessor(FileSystemClient fsClient, U8Span name, IMultiCommitTarget multiCommitTarget,
IFileSystem fileSystem, ICommonMountNameGenerator mountNameGenerator,
ISaveDataAttributeGetter saveAttributeGetter)
{
_fsClient = fsClient;
FsClient = fsClient;
_fileSystem = fileSystem;
_openFiles = new LinkedList<FileAccessor>();
@ -274,7 +274,7 @@ namespace LibHac.Fs.Impl
rc = _fileSystem.OpenFile(out iFile, path, mode);
if (rc.IsFailure()) return rc;
var fileAccessor = new FileAccessor(_fsClient, ref iFile, this, mode);
var fileAccessor = new FileAccessor(FsClient, ref iFile, this, mode);
using (ScopedLock.Lock(ref _openListLock))
{