diff --git a/src/LibHac/Fs/DirectoryHandle.cs b/src/LibHac/Fs/DirectoryHandle.cs index ba8903fe..608280c7 100644 --- a/src/LibHac/Fs/DirectoryHandle.cs +++ b/src/LibHac/Fs/DirectoryHandle.cs @@ -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); + } + } } -} +} \ No newline at end of file diff --git a/src/LibHac/Fs/FileHandle.cs b/src/LibHac/Fs/FileHandle.cs index 18ce7cbb..2ded399b 100644 --- a/src/LibHac/Fs/FileHandle.cs +++ b/src/LibHac/Fs/FileHandle.cs @@ -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); + } + } } } diff --git a/src/LibHac/Fs/Fsa/FileAccessor.cs b/src/LibHac/Fs/Fsa/FileAccessor.cs index 8e2e5328..c0541819 100644 --- a/src/LibHac/Fs/Fsa/FileAccessor.cs +++ b/src/LibHac/Fs/Fsa/FileAccessor.cs @@ -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 diff --git a/src/LibHac/Fs/Fsa/FileSystemAccessor.cs b/src/LibHac/Fs/Fsa/FileSystemAccessor.cs index 2eac29d0..544e88ba 100644 --- a/src/LibHac/Fs/Fsa/FileSystemAccessor.cs +++ b/src/LibHac/Fs/Fsa/FileSystemAccessor.cs @@ -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(); @@ -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)) {