From c4783f458941d8b8019a731e7ad9e3865a580a94 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sun, 19 Dec 2021 14:07:20 -0700 Subject: [PATCH] Update FileStorage for 13.1.0 --- src/LibHac/Fs/Common/FileStorage.cs | 62 ++++++++++--- src/LibHac/Fs/FileHandleStorage.cs | 98 --------------------- src/LibHac/Fs/FileStorageBasedFileSystem.cs | 41 --------- 3 files changed, 52 insertions(+), 149 deletions(-) delete mode 100644 src/LibHac/Fs/FileHandleStorage.cs delete mode 100644 src/LibHac/Fs/FileStorageBasedFileSystem.cs diff --git a/src/LibHac/Fs/Common/FileStorage.cs b/src/LibHac/Fs/Common/FileStorage.cs index 8317065a..93fb1144 100644 --- a/src/LibHac/Fs/Common/FileStorage.cs +++ b/src/LibHac/Fs/Common/FileStorage.cs @@ -8,6 +8,10 @@ using LibHac.Os; // ReSharper disable once CheckNamespace namespace LibHac.Fs; +/// +/// Allows interacting with an via an interface. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) public class FileStorage : IStorage { private const long InvalidSize = -1; @@ -100,18 +104,22 @@ public class FileStorage : IStorage protected override Result DoOperateRange(Span outBuffer, OperationId operationId, long offset, long size, ReadOnlySpan inBuffer) { - if (operationId == OperationId.InvalidateCache || operationId == OperationId.QueryRange) + if (operationId == OperationId.InvalidateCache) + { + Result rc = _baseFile.OperateRange(OperationId.InvalidateCache, offset, size); + if (rc.IsFailure()) return rc.Miss(); + + return Result.Success; + } + + if (operationId == OperationId.QueryRange) { if (size == 0) { - if (operationId == OperationId.QueryRange) - { - if (outBuffer.Length != Unsafe.SizeOf()) - return ResultFs.InvalidSize.Log(); - - SpanHelpers.AsStruct(outBuffer).Clear(); - } + if (outBuffer.Length != Unsafe.SizeOf()) + return ResultFs.InvalidSize.Log(); + SpanHelpers.AsStruct(outBuffer).Clear(); return Result.Success; } @@ -140,6 +148,12 @@ public class FileStorage : IStorage } } +/// +/// Opens a file from an and allows interacting with it through an +/// interface. The opened file will automatically be closed when the +/// is disposed. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) public class FileStorageBasedFileSystem : FileStorage { private SharedRef _baseFileSystem; @@ -153,6 +167,16 @@ public class FileStorageBasedFileSystem : FileStorage base.Dispose(); } + /// + /// Initializes this with the file at the specified path. + /// + /// The containing the file to open. + /// The full path of the file to open. + /// Specifies the access permissions of the opened file. + /// : The operation was successful.
+ /// : The specified path does not exist or is a directory.
+ /// : When opening as , + /// the file is already opened as .
public Result Initialize(ref SharedRef baseFileSystem, in Path path, OpenMode mode) { using var baseFile = new UniqueRef(); @@ -168,6 +192,11 @@ public class FileStorageBasedFileSystem : FileStorage } } +/// +/// Provides an interface for interacting with an opened file from a mounted file system. +/// The caller may choose whether or not the file will be closed when the is disposed. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) public class FileHandleStorage : IStorage { private const long InvalidSize = -1; @@ -177,11 +206,24 @@ public class FileHandleStorage : IStorage private long _size; private SdkMutexType _mutex; - // LibHac addition + // LibHac addition because we don't use global state for the FS client private FileSystemClient _fsClient; + /// + /// Initializes a new with the provided . + /// The file will not be closed when this is disposed. + /// + /// The of the provided . + /// The handle of the file to use. public FileHandleStorage(FileSystemClient fsClient, FileHandle handle) : this(fsClient, handle, false) { } + /// + /// Initializes a new with the provided . + /// + /// The of the provided . + /// The handle of the file to use. + /// Should be closed when this + /// is disposed? public FileHandleStorage(FileSystemClient fsClient, FileHandle handle, bool closeFile) { _fsClient = fsClient; @@ -282,4 +324,4 @@ public class FileHandleStorage : IStorage _size = size; return Result.Success; } -} +} \ No newline at end of file diff --git a/src/LibHac/Fs/FileHandleStorage.cs b/src/LibHac/Fs/FileHandleStorage.cs deleted file mode 100644 index 6b484513..00000000 --- a/src/LibHac/Fs/FileHandleStorage.cs +++ /dev/null @@ -1,98 +0,0 @@ -//using System; -//using LibHac.Common; -//using LibHac.Fs.Fsa; - -//namespace LibHac.Fs -//{ -// public class FileHandleStorage : IStorage -// { -// private const long InvalidSize = -1; -// private readonly object _locker = new object(); - -// private FileSystemClient FsClient { get; } -// private FileHandle Handle { get; } -// private long FileSize { get; set; } = InvalidSize; -// private bool CloseHandle { get; } - -// public FileHandleStorage(FileSystemClient fsClient, FileHandle handle) : this(fsClient, handle, false) { } - -// public FileHandleStorage(FileSystemClient fsClient, FileHandle handle, bool closeHandleOnDispose) -// { -// FsClient = fsClient; -// Handle = handle; -// CloseHandle = closeHandleOnDispose; -// } - -// protected override Result DoRead(long offset, Span destination) -// { -// lock (_locker) -// { -// if (destination.Length == 0) return Result.Success; - -// Result rc = UpdateSize(); -// if (rc.IsFailure()) return rc; - -// if (!CheckAccessRange(offset, destination.Length, FileSize)) return ResultFs.OutOfRange.Log(); - -// return FsClient.ReadFile(Handle, offset, destination); -// } -// } - -// protected override Result DoWrite(long offset, ReadOnlySpan source) -// { -// lock (_locker) -// { -// if (source.Length == 0) return Result.Success; - -// Result rc = UpdateSize(); -// if (rc.IsFailure()) return rc; - -// if (!CheckAccessRange(offset, source.Length, FileSize)) return ResultFs.OutOfRange.Log(); - -// return FsClient.WriteFile(Handle, offset, source, WriteOption.None); -// } -// } - -// protected override Result DoFlush() -// { -// return FsClient.FlushFile(Handle); -// } - -// protected override Result DoSetSize(long size) -// { -// FileSize = InvalidSize; - -// return FsClient.SetFileSize(Handle, size); -// } - -// protected override Result DoGetSize(out long size) -// { -// UnsafeHelpers.SkipParamInit(out size); - -// Result rc = UpdateSize(); -// if (rc.IsFailure()) return rc; - -// size = FileSize; -// return Result.Success; -// } - -// private Result UpdateSize() -// { -// if (FileSize != InvalidSize) return Result.Success; - -// Result rc = FsClient.GetFileSize(out long fileSize, Handle); -// if (rc.IsFailure()) return rc; - -// FileSize = fileSize; -// return Result.Success; -// } - -// protected override void Dispose(bool disposing) -// { -// if (CloseHandle) -// { -// FsClient.CloseFile(Handle); -// } -// } -// } -//} diff --git a/src/LibHac/Fs/FileStorageBasedFileSystem.cs b/src/LibHac/Fs/FileStorageBasedFileSystem.cs deleted file mode 100644 index 27f340a5..00000000 --- a/src/LibHac/Fs/FileStorageBasedFileSystem.cs +++ /dev/null @@ -1,41 +0,0 @@ -//using LibHac.Common; -//using LibHac.Fs.Fsa; - -//namespace LibHac.Fs -//{ -// public class FileStorageBasedFileSystem : FileStorage2 -// { -// private ReferenceCountedDisposable _baseFileSystem; -// private UniqueRef _baseFile; - -// public FileStorageBasedFileSystem() -// { -// FileSize = SizeNotInitialized; -// } - -// public Result Initialize(ref ReferenceCountedDisposable baseFileSystem, in Path path, -// OpenMode mode) -// { -// using var baseFile = new UniqueRef(); -// Result rc = baseFileSystem.Get.OpenFile(ref baseFile.Ref(), in path, mode); -// if (rc.IsFailure()) return rc; - -// SetFile(baseFile.Get); -// _baseFileSystem = Shared.Move(ref baseFileSystem); -// _baseFile.Set(ref _baseFile.Ref()); - -// return Result.Success; -// } - -// protected override void Dispose(bool disposing) -// { -// if (disposing) -// { -// _baseFile.Destroy(); -// _baseFileSystem?.Dispose(); -// } - -// base.Dispose(disposing); -// } -// } -//}