From d073bdfa54b7051e200b27706bd98dd911b1f33a Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sun, 1 Sep 2019 18:35:59 -0500 Subject: [PATCH] Change IFile and IStorage interfaces --- src/LibHac/Fs/FileBase.cs | 18 +++++++++++------- src/LibHac/Fs/IFile.cs | 10 +++++----- src/LibHac/Fs/IStorage.cs | 10 +++++----- src/LibHac/Fs/StorageBase.cs | 23 ++++++++++++----------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/LibHac/Fs/FileBase.cs b/src/LibHac/Fs/FileBase.cs index d84b30d4..1beff741 100644 --- a/src/LibHac/Fs/FileBase.cs +++ b/src/LibHac/Fs/FileBase.cs @@ -8,11 +8,11 @@ namespace LibHac.Fs protected bool IsDisposed { get; private set; } internal List ToDispose { get; } = new List(); - public abstract int Read(Span destination, long offset, ReadOption options); - public abstract void Write(ReadOnlySpan source, long offset, WriteOption options); - public abstract void Flush(); - public abstract long GetSize(); - public abstract void SetSize(long size); + public abstract Result Read(out long bytesRead, long offset, Span destination, ReadOption options); + public abstract Result Write(long offset, ReadOnlySpan source, WriteOption options); + public abstract Result Flush(); + public abstract Result GetSize(out long size); + public abstract Result SetSize(long size); public OpenMode Mode { get; protected set; } @@ -24,7 +24,9 @@ namespace LibHac.Fs if (span == null) throw new ArgumentNullException(nameof(span)); if (offset < 0) ThrowHelper.ThrowResult(ResultFs.ValueOutOfRange, "Offset must be non-negative."); - long fileSize = GetSize(); + Result sizeResult = GetSize(out long fileSize); + sizeResult.ThrowIfFailure(); + int size = span.Length; if (offset > fileSize) ThrowHelper.ThrowResult(ResultFs.ValueOutOfRange, "Offset must be less than the file size."); @@ -41,7 +43,9 @@ namespace LibHac.Fs if (span == null) throw new ArgumentNullException(nameof(span)); if (offset < 0) ThrowHelper.ThrowResult(ResultFs.ValueOutOfRange, "Offset must be non-negative."); - long fileSize = GetSize(); + Result sizeResult = GetSize(out long fileSize); + sizeResult.ThrowIfFailure(); + int size = span.Length; if (offset + size > fileSize) diff --git a/src/LibHac/Fs/IFile.cs b/src/LibHac/Fs/IFile.cs index ad4136f9..08da0ddd 100644 --- a/src/LibHac/Fs/IFile.cs +++ b/src/LibHac/Fs/IFile.cs @@ -34,7 +34,7 @@ namespace LibHac.Fs /// size of the buffer if the IFile is too short to fulfill the request. /// is invalid. /// The file's does not allow reading. - int Read(Span destination, long offset, ReadOption options); + Result Read(out long bytesRead, long offset, Span destination, ReadOption options); /// /// Writes a sequence of bytes to the current . @@ -44,18 +44,18 @@ namespace LibHac.Fs /// Options for writing to the . /// is negative. /// The file's does not allow this request. - void Write(ReadOnlySpan source, long offset, WriteOption options); + Result Write(long offset, ReadOnlySpan source, WriteOption options); /// /// Causes any buffered data to be written to the underlying device. /// - void Flush(); + Result Flush(); /// /// Gets the number of bytes in the file. /// /// The length of the file in bytes. - long GetSize(); + Result GetSize(out long size); /// /// Sets the size of the file in bytes. @@ -63,6 +63,6 @@ namespace LibHac.Fs /// The desired size of the file in bytes. /// If increasing the file size, The file's /// does not allow this appending. - void SetSize(long size); + Result SetSize(long size); } } \ No newline at end of file diff --git a/src/LibHac/Fs/IStorage.cs b/src/LibHac/Fs/IStorage.cs index 7008d50c..8f0a1b47 100644 --- a/src/LibHac/Fs/IStorage.cs +++ b/src/LibHac/Fs/IStorage.cs @@ -14,7 +14,7 @@ namespace LibHac.Fs /// The number of bytes read will be equal to the length of the buffer. /// The offset in the at which to begin reading. /// Invalid offset or the IStorage contains fewer bytes than requested. - void Read(Span destination, long offset); + Result Read(long offset, Span destination); /// /// Writes a sequence of bytes to the current . @@ -23,24 +23,24 @@ namespace LibHac.Fs /// The offset in the at which to begin writing. /// Invalid offset or /// is too large to be written to the IStorage. - void Write(ReadOnlySpan source, long offset); + Result Write(long offset, ReadOnlySpan source); /// /// Causes any buffered data to be written to the underlying device. /// - void Flush(); + Result Flush(); /// /// Sets the size of the current IStorage. /// /// The desired size of the current IStorage in bytes. - void SetSize(long size); + Result SetSize(long size); /// /// The size of the. -1 will be returned if /// the cannot be represented as a sequence of contiguous bytes. /// /// The size of the in bytes. - long GetSize(); + Result GetSize(out long size); } } diff --git a/src/LibHac/Fs/StorageBase.cs b/src/LibHac/Fs/StorageBase.cs index fe42a6b3..580479b7 100644 --- a/src/LibHac/Fs/StorageBase.cs +++ b/src/LibHac/Fs/StorageBase.cs @@ -9,26 +9,26 @@ namespace LibHac.Fs protected internal List ToDispose { get; } = new List(); protected bool CanAutoExpand { get; set; } - protected abstract void ReadImpl(Span destination, long offset); - protected abstract void WriteImpl(ReadOnlySpan source, long offset); - public abstract void Flush(); - public abstract long GetSize(); + protected abstract Result ReadImpl(long offset, Span destination); + protected abstract Result WriteImpl(long offset, ReadOnlySpan source); + public abstract Result Flush(); + public abstract Result GetSize(out long size); - public void Read(Span destination, long offset) + public Result Read(long offset, Span destination) { ValidateParameters(destination, offset); - ReadImpl(destination, offset); + return ReadImpl(offset, destination); } - public void Write(ReadOnlySpan source, long offset) + public Result Write(long offset, ReadOnlySpan source) { ValidateParameters(source, offset); - WriteImpl(source, offset); + return WriteImpl(offset, source); } - public virtual void SetSize(long size) + public virtual Result SetSize(long size) { - ThrowHelper.ThrowResult(ResultFs.NotImplemented); + return ResultFs.NotImplemented.Log(); } protected virtual void Dispose(bool disposing) @@ -58,7 +58,8 @@ namespace LibHac.Fs if (_isDisposed) throw new ObjectDisposedException(null); if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset), "Argument must be non-negative."); - long length = GetSize(); + Result sizeResult = GetSize(out long length); + sizeResult.ThrowIfFailure(); if (length != -1 && !CanAutoExpand) {