diff --git a/src/LibHac/Fs/FileHandleStorage.cs b/src/LibHac/Fs/FileHandleStorage.cs index c4a98419..95f103a8 100644 --- a/src/LibHac/Fs/FileHandleStorage.cs +++ b/src/LibHac/Fs/FileHandleStorage.cs @@ -2,7 +2,7 @@ namespace LibHac.Fs { - public class FileHandleStorage : StorageBase + public class FileHandleStorage : IStorage { private const long InvalidSize = -1; private readonly object _locker = new object(); @@ -21,7 +21,7 @@ namespace LibHac.Fs FsClient = Handle.File.Parent.FsClient; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { lock (_locker) { @@ -36,7 +36,7 @@ namespace LibHac.Fs } } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { lock (_locker) { @@ -51,19 +51,19 @@ namespace LibHac.Fs } } - protected override Result FlushImpl() + protected override Result DoFlush() { return FsClient.FlushFile(Handle); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { FileSize = InvalidSize; return FsClient.SetFileSize(Handle, size); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = default; diff --git a/src/LibHac/Fs/FileStorage2.cs b/src/LibHac/Fs/FileStorage2.cs index 1f94d562..80c942bd 100644 --- a/src/LibHac/Fs/FileStorage2.cs +++ b/src/LibHac/Fs/FileStorage2.cs @@ -5,7 +5,7 @@ using LibHac.Fs.Fsa; namespace LibHac.Fs { - public class FileStorage2 : StorageBase + public class FileStorage2 : IStorage { protected const long SizeNotInitialized = -1; @@ -40,7 +40,7 @@ namespace LibHac.Fs return Result.Success; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { if (destination.Length == 0) return Result.Success; @@ -54,7 +54,7 @@ namespace LibHac.Fs return BaseFile.Read(out _, offset, destination, ReadOption.None); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { if (source.Length == 0) return Result.Success; @@ -68,12 +68,12 @@ namespace LibHac.Fs return BaseFile.Write(offset, source, WriteOption.None); } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseFile.Flush(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { Result rc = UpdateSize(); if (rc.IsFailure()) @@ -86,13 +86,13 @@ namespace LibHac.Fs return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { FileSize = SizeNotInitialized; return BaseFile.SetSize(size); } - protected override Result OperateRangeImpl(Span outBuffer, OperationId operationId, long offset, long size, ReadOnlySpan inBuffer) + protected override Result DoOperateRange(Span outBuffer, OperationId operationId, long offset, long size, ReadOnlySpan inBuffer) { switch (operationId) { diff --git a/src/LibHac/Fs/IStorage.cs b/src/LibHac/Fs/IStorage.cs index 455388ef..05121695 100644 --- a/src/LibHac/Fs/IStorage.cs +++ b/src/LibHac/Fs/IStorage.cs @@ -1,47 +1,97 @@ using System; +using System.Runtime.CompilerServices; +using System.Threading; namespace LibHac.Fs { + // ReSharper disable once InconsistentNaming /// /// Provides an interface for reading and writing a sequence of bytes. /// - public interface IStorage : IDisposable + /// + /// The official IStorage makes the Read etc. methods abstract and doesn't + /// have DoRead etc. methods. We're using them here so we can make sure + /// the object isn't disposed before calling the method implementation. + /// + public abstract class IStorage : IDisposable { + // 0 = not disposed; 1 = disposed + private int _disposedState; + private bool IsDisposed => _disposedState != 0; + /// /// Reads a sequence of bytes from the current . /// + /// The offset in the at which to begin reading. /// The buffer where the read bytes will be stored. /// 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. - Result Read(long offset, Span destination); + /// The of the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Result Read(long offset, Span destination) + { + if (IsDisposed) + return ResultFs.PreconditionViolation.Log(); + + return DoRead(offset, destination); + } /// /// Writes a sequence of bytes to the current . /// - /// The buffer containing the bytes to be written. /// The offset in the at which to begin writing. - /// Invalid offset or - /// is too large to be written to the IStorage. - Result Write(long offset, ReadOnlySpan source); + /// The buffer containing the bytes to be written. + /// The of the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Result Write(long offset, ReadOnlySpan source) + { + if (IsDisposed) + return ResultFs.PreconditionViolation.Log(); + + return DoWrite(offset, source); + } /// /// Causes any buffered data to be written to the underlying device. /// - Result Flush(); + /// The of the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Result Flush() + { + if (IsDisposed) + return ResultFs.PreconditionViolation.Log(); + + return DoFlush(); + } /// - /// Sets the size of the current IStorage. + /// Sets the size of the current . /// - /// The desired size of the current IStorage in bytes. - Result SetSize(long size); + /// The desired size of the in bytes. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Result SetSize(long size) + { + if (IsDisposed) + return ResultFs.PreconditionViolation.Log(); + + return DoSetSize(size); + } /// - /// The size of the. -1 will be returned if - /// the cannot be represented as a sequence of contiguous bytes. + /// Gets the number of bytes in the . /// - /// The size of the in bytes. - Result GetSize(out long size); + /// If the operation returns successfully, the length of the file in bytes. + /// The of the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Result GetSize(out long size) + { + if (IsDisposed) + { + size = default; + return ResultFs.PreconditionViolation.Log(); + } + + return DoGetSize(out size); + } /// /// Performs various operations on the file. Used to extend the functionality of the interface. @@ -51,8 +101,56 @@ namespace LibHac.Fs /// The offset of the range to operate on. /// The size of the range to operate on. /// An input buffer. Size may vary depending on the operation performed. - /// The of the requested operation. - Result OperateRange(Span outBuffer, OperationId operationId, long offset, long size, - ReadOnlySpan inBuffer); + /// The of the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Result OperateRange(Span outBuffer, OperationId operationId, long offset, long size, + ReadOnlySpan inBuffer) + { + if (IsDisposed) + return ResultFs.PreconditionViolation.Log(); + + return DoOperateRange(outBuffer, operationId, offset, size, inBuffer); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsRangeValid(long offset, long size, long totalSize) + { + return offset >= 0 && + size >= 0 && + size <= totalSize && + offset <= totalSize - size; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsOffsetAndSizeValid(long offset, long size) + { + return offset >= 0 && + size >= 0 && + offset <= offset - size; + } + + protected abstract Result DoRead(long offset, Span destination); + protected abstract Result DoWrite(long offset, ReadOnlySpan source); + protected abstract Result DoFlush(); + protected abstract Result DoGetSize(out long size); + protected abstract Result DoSetSize(long size); + + protected virtual Result DoOperateRange(Span outBuffer, OperationId operationId, long offset, long size, + ReadOnlySpan inBuffer) + { + return ResultFs.NotImplemented.Log(); + } + + public void Dispose() + { + // Make sure Dispose is only called once + if (Interlocked.CompareExchange(ref _disposedState, 1, 0) == 0) + { + Dispose(true); + GC.SuppressFinalize(this); + } + } + + protected virtual void Dispose(bool disposing) { } } } diff --git a/src/LibHac/Fs/MemoryStorage.cs b/src/LibHac/Fs/MemoryStorage.cs index 511610f2..4b0e8f12 100644 --- a/src/LibHac/Fs/MemoryStorage.cs +++ b/src/LibHac/Fs/MemoryStorage.cs @@ -2,7 +2,7 @@ namespace LibHac.Fs { - public class MemoryStorage : StorageBase + public class MemoryStorage : IStorage { private byte[] StorageBuffer { get; } @@ -11,7 +11,7 @@ namespace LibHac.Fs StorageBuffer = buffer; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { if (destination.Length == 0) return Result.Success; @@ -24,7 +24,7 @@ namespace LibHac.Fs return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { if (source.Length == 0) return Result.Success; @@ -37,17 +37,17 @@ namespace LibHac.Fs return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.UnsupportedOperationInMemoryStorageSetSize.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = StorageBuffer.Length; diff --git a/src/LibHac/Fs/StorageBase.cs b/src/LibHac/Fs/StorageBase.cs deleted file mode 100644 index 1ea1789b..00000000 --- a/src/LibHac/Fs/StorageBase.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Threading; - -namespace LibHac.Fs -{ - public abstract class StorageBase : IStorage - { - // 0 = not disposed; 1 = disposed - private int _disposedState; - private bool IsDisposed => _disposedState != 0; - - protected abstract Result ReadImpl(long offset, Span destination); - protected abstract Result WriteImpl(long offset, ReadOnlySpan source); - protected abstract Result FlushImpl(); - protected abstract Result GetSizeImpl(out long size); - protected abstract Result SetSizeImpl(long size); - - protected virtual Result OperateRangeImpl(Span outBuffer, OperationId operationId, long offset, long size, - ReadOnlySpan inBuffer) - { - return ResultFs.NotImplemented.Log(); - } - - public Result Read(long offset, Span destination) - { - if (IsDisposed) return ResultFs.PreconditionViolation.Log(); - - return ReadImpl(offset, destination); - } - - public Result Write(long offset, ReadOnlySpan source) - { - if (IsDisposed) return ResultFs.PreconditionViolation.Log(); - - return WriteImpl(offset, source); - } - - public Result Flush() - { - if (IsDisposed) return ResultFs.PreconditionViolation.Log(); - - return FlushImpl(); - } - - public Result SetSize(long size) - { - if (IsDisposed) return ResultFs.PreconditionViolation.Log(); - - return SetSizeImpl(size); - } - - public Result GetSize(out long size) - { - size = default; - if (IsDisposed) return ResultFs.PreconditionViolation.Log(); - - return GetSizeImpl(out size); - } - - public Result OperateRange(Span outBuffer, OperationId operationId, long offset, long size, - ReadOnlySpan inBuffer) - { - if (IsDisposed) return ResultFs.PreconditionViolation.Log(); - - return OperateRange(outBuffer, operationId, offset, size, inBuffer); - } - - public void Dispose() - { - // Make sure Dispose is only called once - if (Interlocked.CompareExchange(ref _disposedState, 1, 0) == 0) - { - Dispose(true); - GC.SuppressFinalize(this); - } - } - - protected virtual void Dispose(bool disposing) { } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsRangeValid(long offset, long size, long totalSize) - { - return offset >= 0 && size >= 0 && size <= totalSize && offset <= totalSize - size; - } - } -} diff --git a/src/LibHac/Fs/SubStorage2.cs b/src/LibHac/Fs/SubStorage2.cs index af9f7b48..3f459d7d 100644 --- a/src/LibHac/Fs/SubStorage2.cs +++ b/src/LibHac/Fs/SubStorage2.cs @@ -2,7 +2,7 @@ namespace LibHac.Fs { - public class SubStorage2 : StorageBase + public class SubStorage2 : IStorage { private IStorage BaseStorage { get; } private long Offset { get; } @@ -23,7 +23,7 @@ namespace LibHac.Fs Size = size; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { if (BaseStorage == null) return ResultFs.SubStorageNotInitialized.Log(); if (destination.Length == 0) return Result.Success; @@ -33,7 +33,7 @@ namespace LibHac.Fs return BaseStorage.Read(Offset + offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { if (BaseStorage == null) return ResultFs.SubStorageNotInitialized.Log(); if (source.Length == 0) return Result.Success; @@ -43,14 +43,14 @@ namespace LibHac.Fs return BaseStorage.Write(Offset + offset, source); } - protected override Result FlushImpl() + protected override Result DoFlush() { if (BaseStorage == null) return ResultFs.SubStorageNotInitialized.Log(); return BaseStorage.Flush(); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { if (BaseStorage == null) return ResultFs.SubStorageNotInitialized.Log(); if (!IsResizable) return ResultFs.SubStorageNotResizable.Log(); @@ -72,7 +72,7 @@ namespace LibHac.Fs return Result.Success; } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = default; diff --git a/src/LibHac/FsService/Creators/EmulatedGameCardStorageCreator.cs b/src/LibHac/FsService/Creators/EmulatedGameCardStorageCreator.cs index c8dc4bb7..11de4dc1 100644 --- a/src/LibHac/FsService/Creators/EmulatedGameCardStorageCreator.cs +++ b/src/LibHac/FsService/Creators/EmulatedGameCardStorageCreator.cs @@ -62,7 +62,7 @@ namespace LibHac.FsService.Creators throw new NotImplementedException(); } - private class ReadOnlyGameCardStorage : StorageBase + private class ReadOnlyGameCardStorage : IStorage { private EmulatedGameCard GameCard { get; } private GameCardHandle Handle { get; set; } @@ -87,7 +87,7 @@ namespace LibHac.FsService.Creators imageHash.CopyTo(ImageHash); } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { // In secure mode, if Handle is old and the card's device ID and // header hash are still the same, Handle is updated to the new handle @@ -95,22 +95,22 @@ namespace LibHac.FsService.Creators return GameCard.Read(Handle, offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return ResultFs.UnsupportedOperationInRoGameCardStorageWrite.Log(); } - protected override Result FlushImpl() + protected override Result DoFlush() { return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.UnsupportedOperationInRoGameCardStorageSetSize.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = 0; diff --git a/src/LibHac/FsSystem/Aes128CtrExStorage.cs b/src/LibHac/FsSystem/Aes128CtrExStorage.cs index 491b7ac2..a6cae265 100644 --- a/src/LibHac/FsSystem/Aes128CtrExStorage.cs +++ b/src/LibHac/FsSystem/Aes128CtrExStorage.cs @@ -31,7 +31,7 @@ namespace LibHac.FsSystem SubsectionOffsets = SubsectionEntries.Select(x => x.Offset).ToList(); } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { AesSubsectionEntry entry = GetSubsectionEntry(offset); @@ -47,7 +47,7 @@ namespace LibHac.FsSystem { UpdateCounterSubsection(entry.Counter); - Result rc = base.ReadImpl(inPos, destination.Slice(outPos, bytesToRead)); + Result rc = base.DoRead(inPos, destination.Slice(outPos, bytesToRead)); if (rc.IsFailure()) return rc; } @@ -64,12 +64,12 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return ResultFs.UnsupportedOperationInAesCtrExStorageWrite.Log(); } - protected override Result FlushImpl() + protected override Result DoFlush() { return Result.Success; } diff --git a/src/LibHac/FsSystem/Aes128CtrStorage.cs b/src/LibHac/FsSystem/Aes128CtrStorage.cs index e413bff3..01f16020 100644 --- a/src/LibHac/FsSystem/Aes128CtrStorage.cs +++ b/src/LibHac/FsSystem/Aes128CtrStorage.cs @@ -59,9 +59,9 @@ namespace LibHac.FsSystem Counter = _decryptor.Counter; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { - Result rc = base.ReadImpl(offset, destination); + Result rc = base.DoRead(offset, destination); if (rc.IsFailure()) return rc; lock (_locker) @@ -73,7 +73,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { byte[] encrypted = ArrayPool.Shared.Rent(source.Length); try @@ -87,7 +87,7 @@ namespace LibHac.FsSystem _decryptor.TransformBlock(encryptedSpan); } - Result rc = base.WriteImpl(offset, encryptedSpan); + Result rc = base.DoWrite(offset, encryptedSpan); if (rc.IsFailure()) return rc; } finally diff --git a/src/LibHac/FsSystem/Aes128XtsStorage.cs b/src/LibHac/FsSystem/Aes128XtsStorage.cs index b879dc92..858a19f2 100644 --- a/src/LibHac/FsSystem/Aes128XtsStorage.cs +++ b/src/LibHac/FsSystem/Aes128XtsStorage.cs @@ -38,14 +38,14 @@ namespace LibHac.FsSystem _key2 = key2.ToArray(); } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { int size = destination.Length; long sectorIndex = offset / SectorSize; if (_decryptor == null) _decryptor = new Aes128XtsTransform(_key1, _key2, true); - Result rc = base.ReadImpl(offset, _tempBuffer.AsSpan(0, size)); + Result rc = base.DoRead(offset, _tempBuffer.AsSpan(0, size)); if (rc.IsFailure()) return rc; _decryptor.TransformBlock(_tempBuffer, 0, size, (ulong)sectorIndex); @@ -54,7 +54,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { int size = source.Length; long sectorIndex = offset / SectorSize; @@ -64,10 +64,10 @@ namespace LibHac.FsSystem source.CopyTo(_tempBuffer); _encryptor.TransformBlock(_tempBuffer, 0, size, (ulong)sectorIndex); - return base.WriteImpl(offset, _tempBuffer.AsSpan(0, size)); + return base.DoWrite(offset, _tempBuffer.AsSpan(0, size)); } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseStorage.Flush(); } diff --git a/src/LibHac/FsSystem/CachedStorage.cs b/src/LibHac/FsSystem/CachedStorage.cs index bba36958..614d50f8 100644 --- a/src/LibHac/FsSystem/CachedStorage.cs +++ b/src/LibHac/FsSystem/CachedStorage.cs @@ -4,7 +4,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class CachedStorage : StorageBase + public class CachedStorage : IStorage { private IStorage BaseStorage { get; } private int BlockSize { get; } @@ -33,7 +33,7 @@ namespace LibHac.FsSystem public CachedStorage(SectorStorage baseStorage, int cacheSize, bool leaveOpen) : this(baseStorage, baseStorage.SectorSize, cacheSize, leaveOpen) { } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { long remaining = destination.Length; long inOffset = offset; @@ -63,7 +63,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { long remaining = source.Length; long inOffset = offset; @@ -95,7 +95,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { lock (Blocks) { @@ -108,13 +108,13 @@ namespace LibHac.FsSystem return BaseStorage.Flush(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { Result rc = BaseStorage.SetSize(size); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/FsSystem/ConcatenationStorage.cs b/src/LibHac/FsSystem/ConcatenationStorage.cs index e4256d5c..112baccd 100644 --- a/src/LibHac/FsSystem/ConcatenationStorage.cs +++ b/src/LibHac/FsSystem/ConcatenationStorage.cs @@ -4,7 +4,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class ConcatenationStorage : StorageBase + public class ConcatenationStorage : IStorage { private ConcatSource[] Sources { get; } private long Length { get; } @@ -28,7 +28,7 @@ namespace LibHac.FsSystem Length = length; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { long inPos = offset; int outPos = 0; @@ -59,7 +59,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { long inPos = offset; int outPos = 0; @@ -90,7 +90,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { foreach (ConcatSource source in Sources) { @@ -101,12 +101,12 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.NotImplemented.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; diff --git a/src/LibHac/FsSystem/FileStorage.cs b/src/LibHac/FsSystem/FileStorage.cs index 133b6a7e..1826f2fb 100644 --- a/src/LibHac/FsSystem/FileStorage.cs +++ b/src/LibHac/FsSystem/FileStorage.cs @@ -4,7 +4,7 @@ using LibHac.Fs.Fsa; namespace LibHac.FsSystem { - public class FileStorage : StorageBase + public class FileStorage : IStorage { protected IFile BaseFile { get; } @@ -13,27 +13,27 @@ namespace LibHac.FsSystem BaseFile = baseFile; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { return BaseFile.Read(out long _, offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return BaseFile.Write(offset, source); } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseFile.Flush(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { return BaseFile.GetSize(out size); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return BaseFile.SetSize(size); } diff --git a/src/LibHac/FsSystem/HierarchicalIntegrityVerificationStorage.cs b/src/LibHac/FsSystem/HierarchicalIntegrityVerificationStorage.cs index 520436ea..3e063966 100644 --- a/src/LibHac/FsSystem/HierarchicalIntegrityVerificationStorage.cs +++ b/src/LibHac/FsSystem/HierarchicalIntegrityVerificationStorage.cs @@ -7,7 +7,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class HierarchicalIntegrityVerificationStorage : StorageBase + public class HierarchicalIntegrityVerificationStorage : IStorage { public IStorage[] Levels { get; } public IStorage DataLevel { get; } @@ -96,27 +96,27 @@ namespace LibHac.FsSystem return initInfo; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { return DataLevel.Read(offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return DataLevel.Write(offset, source); } - protected override Result FlushImpl() + protected override Result DoFlush() { return DataLevel.Flush(); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.UnsupportedOperationInHierarchicalIvfcStorageSetSize.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; diff --git a/src/LibHac/FsSystem/IndirectStorage.cs b/src/LibHac/FsSystem/IndirectStorage.cs index c1387d8e..2acb3ae0 100644 --- a/src/LibHac/FsSystem/IndirectStorage.cs +++ b/src/LibHac/FsSystem/IndirectStorage.cs @@ -5,7 +5,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class IndirectStorage : StorageBase + public class IndirectStorage : IStorage { private List RelocationEntries { get; } private List RelocationOffsets { get; } @@ -29,7 +29,7 @@ namespace LibHac.FsSystem Length = BucketTree.BucketOffsets.OffsetEnd; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { RelocationEntry entry = GetRelocationEntry(offset); @@ -64,23 +64,23 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return ResultFs.UnsupportedOperationInIndirectStorageSetSize.Log(); } - protected override Result FlushImpl() + protected override Result DoFlush() { return Result.Success; } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.UnsupportedOperationInIndirectStorageSetSize.Log(); } diff --git a/src/LibHac/FsSystem/IntegrityVerificationStorage.cs b/src/LibHac/FsSystem/IntegrityVerificationStorage.cs index a0df10a0..75a86ddb 100644 --- a/src/LibHac/FsSystem/IntegrityVerificationStorage.cs +++ b/src/LibHac/FsSystem/IntegrityVerificationStorage.cs @@ -117,7 +117,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { return ReadImpl(offset, destination, IntegrityCheckLevel); } @@ -128,7 +128,7 @@ namespace LibHac.FsSystem return ReadImpl(offset, destination, integrityCheckLevel); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { long blockIndex = offset / SectorSize; long hashPos = blockIndex * DigestSize; @@ -188,12 +188,12 @@ namespace LibHac.FsSystem } } - protected override Result FlushImpl() + protected override Result DoFlush() { Result rc = HashStorage.Flush(); if (rc.IsFailure()) return rc; - return base.FlushImpl(); + return base.DoFlush(); } public void FsTrim() diff --git a/src/LibHac/FsSystem/LocalStorage.cs b/src/LibHac/FsSystem/LocalStorage.cs index ebc47f58..0b9a4f51 100644 --- a/src/LibHac/FsSystem/LocalStorage.cs +++ b/src/LibHac/FsSystem/LocalStorage.cs @@ -4,7 +4,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class LocalStorage : StorageBase + public class LocalStorage : IStorage { private string Path { get; } private FileStream Stream { get; } @@ -19,27 +19,27 @@ namespace LibHac.FsSystem Storage = new StreamStorage(Stream, false); } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { return Storage.Read(offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return Storage.Write(offset, source); } - protected override Result FlushImpl() + protected override Result DoFlush() { return Storage.Flush(); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.NotImplemented.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { return Storage.GetSize(out size); } diff --git a/src/LibHac/FsSystem/NullStorage.cs b/src/LibHac/FsSystem/NullStorage.cs index 81b4d23e..d6e6cf18 100644 --- a/src/LibHac/FsSystem/NullStorage.cs +++ b/src/LibHac/FsSystem/NullStorage.cs @@ -6,7 +6,7 @@ namespace LibHac.FsSystem /// /// An that returns all zeros when read, and does nothing on write. /// - public class NullStorage : StorageBase + public class NullStorage : IStorage { private long Length { get; } @@ -14,28 +14,28 @@ namespace LibHac.FsSystem public NullStorage(long length) => Length = length; - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { destination.Clear(); return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.NotImplemented.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; diff --git a/src/LibHac/FsSystem/Save/AllocationTableStorage.cs b/src/LibHac/FsSystem/Save/AllocationTableStorage.cs index f68dfe16..170f3749 100644 --- a/src/LibHac/FsSystem/Save/AllocationTableStorage.cs +++ b/src/LibHac/FsSystem/Save/AllocationTableStorage.cs @@ -4,7 +4,7 @@ using LibHac.Fs; namespace LibHac.FsSystem.Save { - public class AllocationTableStorage : StorageBase + public class AllocationTableStorage : IStorage { private IStorage BaseStorage { get; } private int BlockSize { get; } @@ -23,7 +23,7 @@ namespace LibHac.FsSystem.Save _length = initialBlock == -1 ? 0 : table.GetListLength(initialBlock) * blockSize; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { var iterator = new AllocationTableIterator(Fat, InitialBlock); @@ -53,7 +53,7 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { var iterator = new AllocationTableIterator(Fat, InitialBlock); @@ -83,18 +83,18 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseStorage.Flush(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = _length; return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { int oldBlockCount = (int)Util.DivideByRoundUp(_length, BlockSize); int newBlockCount = (int)Util.DivideByRoundUp(size, BlockSize); diff --git a/src/LibHac/FsSystem/Save/DuplexStorage.cs b/src/LibHac/FsSystem/Save/DuplexStorage.cs index 7b6867d8..aeda29dc 100644 --- a/src/LibHac/FsSystem/Save/DuplexStorage.cs +++ b/src/LibHac/FsSystem/Save/DuplexStorage.cs @@ -3,7 +3,7 @@ using LibHac.Fs; namespace LibHac.FsSystem.Save { - public class DuplexStorage : StorageBase + public class DuplexStorage : IStorage { private int BlockSize { get; } private IStorage BitmapStorage { get; } @@ -27,7 +27,7 @@ namespace LibHac.FsSystem.Save Length = dataSize; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { long inPos = offset; int outPos = 0; @@ -56,7 +56,7 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { long inPos = offset; int outPos = 0; @@ -85,7 +85,7 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { Result rc = BitmapStorage.Flush(); if (rc.IsFailure()) return rc; @@ -99,12 +99,12 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.NotImplemented.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; diff --git a/src/LibHac/FsSystem/Save/HierarchicalDuplexStorage.cs b/src/LibHac/FsSystem/Save/HierarchicalDuplexStorage.cs index 132b3ad5..42292cf0 100644 --- a/src/LibHac/FsSystem/Save/HierarchicalDuplexStorage.cs +++ b/src/LibHac/FsSystem/Save/HierarchicalDuplexStorage.cs @@ -3,7 +3,7 @@ using LibHac.Fs; namespace LibHac.FsSystem.Save { - public class HierarchicalDuplexStorage : StorageBase + public class HierarchicalDuplexStorage : IStorage { private DuplexStorage[] Layers { get; } private DuplexStorage DataLayer { get; } @@ -34,27 +34,27 @@ namespace LibHac.FsSystem.Save Length = dataSize; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { return DataLayer.Read(offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { return DataLayer.Write(offset, source); } - protected override Result FlushImpl() + protected override Result DoFlush() { return DataLayer.Flush(); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.NotImplemented.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; diff --git a/src/LibHac/FsSystem/Save/JournalStorage.cs b/src/LibHac/FsSystem/Save/JournalStorage.cs index 3eb6081b..b52dba43 100644 --- a/src/LibHac/FsSystem/Save/JournalStorage.cs +++ b/src/LibHac/FsSystem/Save/JournalStorage.cs @@ -5,7 +5,7 @@ using LibHac.Fs; namespace LibHac.FsSystem.Save { - public class JournalStorage : StorageBase + public class JournalStorage : IStorage { private IStorage BaseStorage { get; } private IStorage HeaderStorage { get; } @@ -33,7 +33,7 @@ namespace LibHac.FsSystem.Save LeaveOpen = leaveOpen; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { long inPos = offset; int outPos = 0; @@ -62,7 +62,7 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { long inPos = offset; int outPos = 0; @@ -91,17 +91,17 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseStorage.Flush(); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.NotImplemented.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; diff --git a/src/LibHac/FsSystem/Save/RemapStorage.cs b/src/LibHac/FsSystem/Save/RemapStorage.cs index 32432176..2f5697ea 100644 --- a/src/LibHac/FsSystem/Save/RemapStorage.cs +++ b/src/LibHac/FsSystem/Save/RemapStorage.cs @@ -6,7 +6,7 @@ using LibHac.Fs; namespace LibHac.FsSystem.Save { - public class RemapStorage : StorageBase + public class RemapStorage : IStorage { private const int MapEntryLength = 0x20; @@ -48,7 +48,7 @@ namespace LibHac.FsSystem.Save Segments = InitSegments(Header, MapEntries); } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { if (destination.Length == 0) return Result.Success; @@ -78,7 +78,7 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { if (source.Length == 0) return Result.Success; @@ -110,17 +110,17 @@ namespace LibHac.FsSystem.Save return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseStorage.Flush(); } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.UnsupportedOperationInHierarchicalIvfcStorageSetSize.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { // todo: Different result code size = -1; diff --git a/src/LibHac/FsSystem/SectorStorage.cs b/src/LibHac/FsSystem/SectorStorage.cs index 6e0a09bc..0012f4db 100644 --- a/src/LibHac/FsSystem/SectorStorage.cs +++ b/src/LibHac/FsSystem/SectorStorage.cs @@ -3,7 +3,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class SectorStorage : StorageBase + public class SectorStorage : IStorage { protected IStorage BaseStorage { get; } @@ -26,30 +26,30 @@ namespace LibHac.FsSystem LeaveOpen = leaveOpen; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { ValidateSize(destination.Length, offset); return BaseStorage.Read(offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { ValidateSize(source.Length, offset); return BaseStorage.Write(offset, source); } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseStorage.Flush(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { Result rc = BaseStorage.SetSize(size); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/FsSystem/StreamStorage.cs b/src/LibHac/FsSystem/StreamStorage.cs index a5d1c0bc..943b9698 100644 --- a/src/LibHac/FsSystem/StreamStorage.cs +++ b/src/LibHac/FsSystem/StreamStorage.cs @@ -4,7 +4,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class StreamStorage : StorageBase + public class StreamStorage : IStorage { // todo: handle Stream exceptions @@ -20,7 +20,7 @@ namespace LibHac.FsSystem LeaveOpen = leaveOpen; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { lock (Locker) { @@ -35,7 +35,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { lock (Locker) { @@ -50,7 +50,7 @@ namespace LibHac.FsSystem return Result.Success; } - protected override Result FlushImpl() + protected override Result DoFlush() { lock (Locker) { @@ -60,12 +60,12 @@ namespace LibHac.FsSystem } } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { return ResultFs.NotImplemented.Log(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; diff --git a/src/LibHac/FsSystem/SubStorage.cs b/src/LibHac/FsSystem/SubStorage.cs index efaa4b7e..90991dc7 100644 --- a/src/LibHac/FsSystem/SubStorage.cs +++ b/src/LibHac/FsSystem/SubStorage.cs @@ -4,7 +4,7 @@ using LibHac.Fs; namespace LibHac.FsSystem { - public class SubStorage : StorageBase + public class SubStorage : IStorage { private IStorage BaseStorage { get; } private long Offset { get; } @@ -38,30 +38,30 @@ namespace LibHac.FsSystem Access = access; } - protected override Result ReadImpl(long offset, Span destination) + protected override Result DoRead(long offset, Span destination) { if ((Access & FileAccess.Read) == 0) throw new InvalidOperationException("Storage is not readable"); return BaseStorage.Read(offset + Offset, destination); } - protected override Result WriteImpl(long offset, ReadOnlySpan source) + protected override Result DoWrite(long offset, ReadOnlySpan source) { if ((Access & FileAccess.Write) == 0) throw new InvalidOperationException("Storage is not writable"); return BaseStorage.Write(offset + Offset, source); } - protected override Result FlushImpl() + protected override Result DoFlush() { return BaseStorage.Flush(); } - protected override Result GetSizeImpl(out long size) + protected override Result DoGetSize(out long size) { size = Length; return Result.Success; } - protected override Result SetSizeImpl(long size) + protected override Result DoSetSize(long size) { if (BaseStorage == null) return ResultFs.SubStorageNotInitialized.Log();