Use more accurate Sf IFile and IStorage function signatures

This commit is contained in:
Alex Barney 2021-04-19 00:34:30 -07:00
parent 880916e117
commit 4d3a1418f6
6 changed files with 39 additions and 31 deletions

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using LibHac.Common; using LibHac.Common;
using LibHac.Sf;
using IFile = LibHac.Fs.Fsa.IFile; using IFile = LibHac.Fs.Fsa.IFile;
using IFileSf = LibHac.FsSrv.Sf.IFile; using IFileSf = LibHac.FsSrv.Sf.IFile;
@ -21,12 +22,12 @@ namespace LibHac.Fs.Impl
protected override Result DoRead(out long bytesRead, long offset, Span<byte> destination, in ReadOption option) protected override Result DoRead(out long bytesRead, long offset, Span<byte> destination, in ReadOption option)
{ {
return BaseFile.Target.Read(out bytesRead, offset, destination, option); return BaseFile.Target.Read(out bytesRead, offset, new OutBuffer(destination), destination.Length, option);
} }
protected override Result DoWrite(long offset, ReadOnlySpan<byte> source, in WriteOption option) protected override Result DoWrite(long offset, ReadOnlySpan<byte> source, in WriteOption option)
{ {
return BaseFile.Target.Write(offset, source, option); return BaseFile.Target.Write(offset, new InBuffer(source), source.Length, option);
} }
protected override Result DoFlush() protected override Result DoFlush()
@ -44,7 +45,8 @@ namespace LibHac.Fs.Impl
return BaseFile.Target.GetSize(out size); return BaseFile.Target.GetSize(out size);
} }
protected override Result DoOperateRange(Span<byte> outBuffer, OperationId operationId, long offset, long size, ReadOnlySpan<byte> inBuffer) protected override Result DoOperateRange(Span<byte> outBuffer, OperationId operationId, long offset, long size,
ReadOnlySpan<byte> inBuffer)
{ {
switch (operationId) switch (operationId)
{ {

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using LibHac.Common; using LibHac.Common;
using LibHac.Sf;
using IStorageSf = LibHac.FsSrv.Sf.IStorage; using IStorageSf = LibHac.FsSrv.Sf.IStorage;
namespace LibHac.Fs.Impl namespace LibHac.Fs.Impl
@ -31,12 +32,12 @@ namespace LibHac.Fs.Impl
protected override Result DoRead(long offset, Span<byte> destination) protected override Result DoRead(long offset, Span<byte> destination)
{ {
return BaseStorage.Target.Read(offset, destination); return BaseStorage.Target.Read(offset, new OutBuffer(destination), destination.Length);
} }
protected override Result DoWrite(long offset, ReadOnlySpan<byte> source) protected override Result DoWrite(long offset, ReadOnlySpan<byte> source)
{ {
return BaseStorage.Target.Write(offset, source); return BaseStorage.Target.Write(offset, new InBuffer(source), source.Length);
} }
protected override Result DoFlush() protected override Result DoFlush()
@ -54,7 +55,8 @@ namespace LibHac.Fs.Impl
return BaseStorage.Target.GetSize(out size); return BaseStorage.Target.GetSize(out size);
} }
protected override Result DoOperateRange(Span<byte> outBuffer, OperationId operationId, long offset, long size, ReadOnlySpan<byte> inBuffer) protected override Result DoOperateRange(Span<byte> outBuffer, OperationId operationId, long offset, long size,
ReadOnlySpan<byte> inBuffer)
{ {
switch (operationId) switch (operationId)
{ {

View file

@ -2,6 +2,7 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using LibHac.Common; using LibHac.Common;
using LibHac.Fs; using LibHac.Fs;
using LibHac.Sf;
using IFile = LibHac.Fs.Fsa.IFile; using IFile = LibHac.Fs.Fsa.IFile;
using IFileSf = LibHac.FsSrv.Sf.IFile; using IFileSf = LibHac.FsSrv.Sf.IFile;
@ -20,7 +21,7 @@ namespace LibHac.FsSrv.Impl
parentFileSystem = null; parentFileSystem = null;
} }
public Result Read(out long bytesRead, long offset, Span<byte> destination, ReadOption option) public Result Read(out long bytesRead, long offset, OutBuffer destination, long size, ReadOption option)
{ {
const int maxTryCount = 2; const int maxTryCount = 2;
UnsafeHelpers.SkipParamInit(out bytesRead); UnsafeHelpers.SkipParamInit(out bytesRead);
@ -28,7 +29,7 @@ namespace LibHac.FsSrv.Impl
if (offset < 0) if (offset < 0)
return ResultFs.InvalidOffset.Log(); return ResultFs.InvalidOffset.Log();
if (destination.Length < 0) if (destination.Size < 0)
return ResultFs.InvalidSize.Log(); return ResultFs.InvalidSize.Log();
Result rc = Result.Success; Result rc = Result.Success;
@ -36,7 +37,7 @@ namespace LibHac.FsSrv.Impl
for (int tryNum = 0; tryNum < maxTryCount; tryNum++) for (int tryNum = 0; tryNum < maxTryCount; tryNum++)
{ {
rc = BaseFile.Read(out tmpBytesRead, offset, destination, option); rc = BaseFile.Read(out tmpBytesRead, offset, destination.Buffer.Slice(0, (int)size), option);
// Retry on ResultDataCorrupted // Retry on ResultDataCorrupted
if (!ResultFs.DataCorrupted.Includes(rc)) if (!ResultFs.DataCorrupted.Includes(rc))
@ -49,17 +50,17 @@ namespace LibHac.FsSrv.Impl
return Result.Success; return Result.Success;
} }
public Result Write(long offset, ReadOnlySpan<byte> source, WriteOption option) public Result Write(long offset, InBuffer source, long size, WriteOption option)
{ {
if (offset < 0) if (offset < 0)
return ResultFs.InvalidOffset.Log(); return ResultFs.InvalidOffset.Log();
if (source.Length < 0) if (source.Size < 0)
return ResultFs.InvalidSize.Log(); return ResultFs.InvalidSize.Log();
// Note: Thread priority is temporarily when writing in FS // Note: Thread priority is temporarily increased when writing in FS
return BaseFile.Write(offset, source, option); return BaseFile.Write(offset, source.Buffer.Slice(0, (int)size), option);
} }
public Result Flush() public Result Flush()
@ -113,8 +114,8 @@ namespace LibHac.FsSrv.Impl
{ {
Unsafe.SkipInit(out QueryRangeInfo info); Unsafe.SkipInit(out QueryRangeInfo info);
Result rc = BaseFile.OperateRange(SpanHelpers.AsByteSpan(ref info), OperationId.QueryRange, offset, size, Result rc = BaseFile.OperateRange(SpanHelpers.AsByteSpan(ref info), OperationId.QueryRange, offset,
ReadOnlySpan<byte>.Empty); size, ReadOnlySpan<byte>.Empty);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;
rangeInfo.Merge(in info); rangeInfo.Merge(in info);

View file

@ -2,6 +2,7 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using LibHac.Common; using LibHac.Common;
using LibHac.Fs; using LibHac.Fs;
using LibHac.Sf;
using IStorageSf = LibHac.FsSrv.Sf.IStorage; using IStorageSf = LibHac.FsSrv.Sf.IStorage;
namespace LibHac.FsSrv.Impl namespace LibHac.FsSrv.Impl
@ -27,21 +28,21 @@ namespace LibHac.FsSrv.Impl
BaseStorage?.Dispose(); BaseStorage?.Dispose();
} }
public Result Read(long offset, Span<byte> destination) public Result Read(long offset, OutBuffer destination, long size)
{ {
const int maxTryCount = 2; const int maxTryCount = 2;
if (offset < 0) if (offset < 0)
return ResultFs.InvalidOffset.Log(); return ResultFs.InvalidOffset.Log();
if (destination.Length < 0) if (destination.Size < 0)
return ResultFs.InvalidSize.Log(); return ResultFs.InvalidSize.Log();
Result rc = Result.Success; Result rc = Result.Success;
for (int tryNum = 0; tryNum < maxTryCount; tryNum++) for (int tryNum = 0; tryNum < maxTryCount; tryNum++)
{ {
rc = BaseStorage.Target.Read(offset, destination); rc = BaseStorage.Target.Read(offset, destination.Buffer.Slice(0, (int)size));
// Retry on ResultDataCorrupted // Retry on ResultDataCorrupted
if (!ResultFs.DataCorrupted.Includes(rc)) if (!ResultFs.DataCorrupted.Includes(rc))
@ -51,17 +52,17 @@ namespace LibHac.FsSrv.Impl
return rc; return rc;
} }
public Result Write(long offset, ReadOnlySpan<byte> source) public Result Write(long offset, InBuffer source, long size)
{ {
if (offset < 0) if (offset < 0)
return ResultFs.InvalidOffset.Log(); return ResultFs.InvalidOffset.Log();
if (source.Length < 0) if (source.Size < 0)
return ResultFs.InvalidSize.Log(); return ResultFs.InvalidSize.Log();
// Note: Thread priority is temporarily increased when writing in FS // Note: Thread priority is temporarily increased when writing in FS
return BaseStorage.Target.Write(offset, source); return BaseStorage.Target.Write(offset, source.Buffer.Slice(0, (int)size));
} }
public Result Flush() public Result Flush()

View file

@ -1,12 +1,13 @@
using System; using System;
using LibHac.Fs; using LibHac.Fs;
using LibHac.Sf;
namespace LibHac.FsSrv.Sf namespace LibHac.FsSrv.Sf
{ {
public interface IFile : IDisposable public interface IFile : IDisposable
{ {
Result Read(out long bytesRead, long offset, Span<byte> destination, ReadOption option); Result Read(out long bytesRead, long offset, OutBuffer destination, long size, ReadOption option);
Result Write(long offset, ReadOnlySpan<byte> source, WriteOption option); Result Write(long offset, InBuffer source, long size, WriteOption option);
Result Flush(); Result Flush();
Result SetSize(long size); Result SetSize(long size);
Result GetSize(out long size); Result GetSize(out long size);

View file

@ -1,12 +1,13 @@
using System; using System;
using LibHac.Fs; using LibHac.Fs;
using LibHac.Sf;
namespace LibHac.FsSrv.Sf namespace LibHac.FsSrv.Sf
{ {
public interface IStorage : IDisposable public interface IStorage : IDisposable
{ {
Result Read(long offset, Span<byte> destination); Result Read(long offset, OutBuffer destination, long size);
Result Write(long offset, ReadOnlySpan<byte> source); Result Write(long offset, InBuffer source, long size);
Result Flush(); Result Flush();
Result SetSize(long size); Result SetSize(long size);
Result GetSize(out long size); Result GetSize(out long size);