Change IFile and IStorage interfaces

This commit is contained in:
Alex Barney 2019-09-01 18:35:59 -05:00
parent 6823aa7cb8
commit d073bdfa54
4 changed files with 33 additions and 28 deletions

View file

@ -8,11 +8,11 @@ namespace LibHac.Fs
protected bool IsDisposed { get; private set; } protected bool IsDisposed { get; private set; }
internal List<IDisposable> ToDispose { get; } = new List<IDisposable>(); internal List<IDisposable> ToDispose { get; } = new List<IDisposable>();
public abstract int Read(Span<byte> destination, long offset, ReadOption options); public abstract Result Read(out long bytesRead, long offset, Span<byte> destination, ReadOption options);
public abstract void Write(ReadOnlySpan<byte> source, long offset, WriteOption options); public abstract Result Write(long offset, ReadOnlySpan<byte> source, WriteOption options);
public abstract void Flush(); public abstract Result Flush();
public abstract long GetSize(); public abstract Result GetSize(out long size);
public abstract void SetSize(long size); public abstract Result SetSize(long size);
public OpenMode Mode { get; protected set; } public OpenMode Mode { get; protected set; }
@ -24,7 +24,9 @@ namespace LibHac.Fs
if (span == null) throw new ArgumentNullException(nameof(span)); if (span == null) throw new ArgumentNullException(nameof(span));
if (offset < 0) ThrowHelper.ThrowResult(ResultFs.ValueOutOfRange, "Offset must be non-negative."); 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; int size = span.Length;
if (offset > fileSize) ThrowHelper.ThrowResult(ResultFs.ValueOutOfRange, "Offset must be less than the file size."); 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 (span == null) throw new ArgumentNullException(nameof(span));
if (offset < 0) ThrowHelper.ThrowResult(ResultFs.ValueOutOfRange, "Offset must be non-negative."); 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; int size = span.Length;
if (offset + size > fileSize) if (offset + size > fileSize)

View file

@ -34,7 +34,7 @@ namespace LibHac.Fs
/// size of the buffer if the IFile is too short to fulfill the request.</returns> /// size of the buffer if the IFile is too short to fulfill the request.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> is invalid.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> is invalid.</exception>
/// <exception cref="NotSupportedException">The file's <see cref="OpenMode"/> does not allow reading.</exception> /// <exception cref="NotSupportedException">The file's <see cref="OpenMode"/> does not allow reading.</exception>
int Read(Span<byte> destination, long offset, ReadOption options); Result Read(out long bytesRead, long offset, Span<byte> destination, ReadOption options);
/// <summary> /// <summary>
/// Writes a sequence of bytes to the current <see cref="IFile"/>. /// Writes a sequence of bytes to the current <see cref="IFile"/>.
@ -44,18 +44,18 @@ namespace LibHac.Fs
/// <param name="options">Options for writing to the <see cref="IFile"/>.</param> /// <param name="options">Options for writing to the <see cref="IFile"/>.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> is negative.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> is negative.</exception>
/// <exception cref="NotSupportedException">The file's <see cref="OpenMode"/> does not allow this request.</exception> /// <exception cref="NotSupportedException">The file's <see cref="OpenMode"/> does not allow this request.</exception>
void Write(ReadOnlySpan<byte> source, long offset, WriteOption options); Result Write(long offset, ReadOnlySpan<byte> source, WriteOption options);
/// <summary> /// <summary>
/// Causes any buffered data to be written to the underlying device. /// Causes any buffered data to be written to the underlying device.
/// </summary> /// </summary>
void Flush(); Result Flush();
/// <summary> /// <summary>
/// Gets the number of bytes in the file. /// Gets the number of bytes in the file.
/// </summary> /// </summary>
/// <returns>The length of the file in bytes.</returns> /// <returns>The length of the file in bytes.</returns>
long GetSize(); Result GetSize(out long size);
/// <summary> /// <summary>
/// Sets the size of the file in bytes. /// Sets the size of the file in bytes.
@ -63,6 +63,6 @@ namespace LibHac.Fs
/// <param name="size">The desired size of the file in bytes.</param> /// <param name="size">The desired size of the file in bytes.</param>
/// <exception cref="NotSupportedException">If increasing the file size, The file's /// <exception cref="NotSupportedException">If increasing the file size, The file's
/// <see cref="OpenMode"/> does not allow this appending.</exception> /// <see cref="OpenMode"/> does not allow this appending.</exception>
void SetSize(long size); Result SetSize(long size);
} }
} }

View file

@ -14,7 +14,7 @@ namespace LibHac.Fs
/// The number of bytes read will be equal to the length of the buffer.</param> /// The number of bytes read will be equal to the length of the buffer.</param>
/// <param name="offset">The offset in the <see cref="IStorage"/> at which to begin reading.</param> /// <param name="offset">The offset in the <see cref="IStorage"/> at which to begin reading.</param>
/// <exception cref="ArgumentException">Invalid offset or the IStorage contains fewer bytes than requested. </exception> /// <exception cref="ArgumentException">Invalid offset or the IStorage contains fewer bytes than requested. </exception>
void Read(Span<byte> destination, long offset); Result Read(long offset, Span<byte> destination);
/// <summary> /// <summary>
/// Writes a sequence of bytes to the current <see cref="IStorage"/>. /// Writes a sequence of bytes to the current <see cref="IStorage"/>.
@ -23,24 +23,24 @@ namespace LibHac.Fs
/// <param name="offset">The offset in the <see cref="IStorage"/> at which to begin writing.</param> /// <param name="offset">The offset in the <see cref="IStorage"/> at which to begin writing.</param>
/// <exception cref="ArgumentException">Invalid offset or <paramref name="source"/> /// <exception cref="ArgumentException">Invalid offset or <paramref name="source"/>
/// is too large to be written to the IStorage. </exception> /// is too large to be written to the IStorage. </exception>
void Write(ReadOnlySpan<byte> source, long offset); Result Write(long offset, ReadOnlySpan<byte> source);
/// <summary> /// <summary>
/// Causes any buffered data to be written to the underlying device. /// Causes any buffered data to be written to the underlying device.
/// </summary> /// </summary>
void Flush(); Result Flush();
/// <summary> /// <summary>
/// Sets the size of the current IStorage. /// Sets the size of the current IStorage.
/// </summary> /// </summary>
/// <param name="size">The desired size of the current IStorage in bytes.</param> /// <param name="size">The desired size of the current IStorage in bytes.</param>
void SetSize(long size); Result SetSize(long size);
/// <summary> /// <summary>
/// The size of the<see cref="IStorage"/>. -1 will be returned if /// The size of the<see cref="IStorage"/>. -1 will be returned if
/// the <see cref="IStorage"/> cannot be represented as a sequence of contiguous bytes. /// the <see cref="IStorage"/> cannot be represented as a sequence of contiguous bytes.
/// </summary> /// </summary>
/// <returns>The size of the <see cref="IStorage"/> in bytes.</returns> /// <returns>The size of the <see cref="IStorage"/> in bytes.</returns>
long GetSize(); Result GetSize(out long size);
} }
} }

View file

@ -9,26 +9,26 @@ namespace LibHac.Fs
protected internal List<IDisposable> ToDispose { get; } = new List<IDisposable>(); protected internal List<IDisposable> ToDispose { get; } = new List<IDisposable>();
protected bool CanAutoExpand { get; set; } protected bool CanAutoExpand { get; set; }
protected abstract void ReadImpl(Span<byte> destination, long offset); protected abstract Result ReadImpl(long offset, Span<byte> destination);
protected abstract void WriteImpl(ReadOnlySpan<byte> source, long offset); protected abstract Result WriteImpl(long offset, ReadOnlySpan<byte> source);
public abstract void Flush(); public abstract Result Flush();
public abstract long GetSize(); public abstract Result GetSize(out long size);
public void Read(Span<byte> destination, long offset) public Result Read(long offset, Span<byte> destination)
{ {
ValidateParameters(destination, offset); ValidateParameters(destination, offset);
ReadImpl(destination, offset); return ReadImpl(offset, destination);
} }
public void Write(ReadOnlySpan<byte> source, long offset) public Result Write(long offset, ReadOnlySpan<byte> source)
{ {
ValidateParameters(source, offset); 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) protected virtual void Dispose(bool disposing)
@ -58,7 +58,8 @@ namespace LibHac.Fs
if (_isDisposed) throw new ObjectDisposedException(null); if (_isDisposed) throw new ObjectDisposedException(null);
if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset), "Argument must be non-negative."); 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) if (length != -1 && !CanAutoExpand)
{ {