mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Add TruncatedSubStorage
This commit is contained in:
parent
e9c38dc7ba
commit
d7ae809cf3
2 changed files with 68 additions and 2 deletions
|
@ -6,11 +6,38 @@ namespace LibHac.Fs
|
||||||
public class SubStorage : IStorage
|
public class SubStorage : IStorage
|
||||||
{
|
{
|
||||||
private ReferenceCountedDisposable<IStorage> SharedBaseStorage { get; set; }
|
private ReferenceCountedDisposable<IStorage> SharedBaseStorage { get; set; }
|
||||||
private IStorage BaseStorage { get; }
|
protected IStorage BaseStorage { get; private set; }
|
||||||
private long Offset { get; }
|
private long Offset { get; set; }
|
||||||
private long Size { get; set; }
|
private long Size { get; set; }
|
||||||
private bool IsResizable { get; set; }
|
private bool IsResizable { get; set; }
|
||||||
|
|
||||||
|
public SubStorage()
|
||||||
|
{
|
||||||
|
BaseStorage = null;
|
||||||
|
Offset = 0;
|
||||||
|
Size = 0;
|
||||||
|
IsResizable = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SubStorage(SubStorage other)
|
||||||
|
{
|
||||||
|
BaseStorage = other.BaseStorage;
|
||||||
|
Offset = other.Offset;
|
||||||
|
Size = other.Size;
|
||||||
|
IsResizable = other.IsResizable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InitializeFrom(SubStorage other)
|
||||||
|
{
|
||||||
|
if (this != other)
|
||||||
|
{
|
||||||
|
BaseStorage = other.BaseStorage;
|
||||||
|
Offset = other.Offset;
|
||||||
|
Size = other.Size;
|
||||||
|
IsResizable = other.IsResizable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public SubStorage(IStorage baseStorage, long offset, long size)
|
public SubStorage(IStorage baseStorage, long offset, long size)
|
||||||
{
|
{
|
||||||
BaseStorage = baseStorage;
|
BaseStorage = baseStorage;
|
||||||
|
|
39
src/LibHac/FsSystem/TruncatedSubStorage.cs
Normal file
39
src/LibHac/FsSystem/TruncatedSubStorage.cs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
using System;
|
||||||
|
using LibHac.Fs;
|
||||||
|
|
||||||
|
namespace LibHac.FsSystem
|
||||||
|
{
|
||||||
|
public class TruncatedSubStorage : SubStorage
|
||||||
|
{
|
||||||
|
public TruncatedSubStorage() { }
|
||||||
|
public TruncatedSubStorage(SubStorage other) : base(other) { }
|
||||||
|
|
||||||
|
protected override Result DoRead(long offset, Span<byte> destination)
|
||||||
|
{
|
||||||
|
if (destination.Length == 0)
|
||||||
|
return Result.Success;
|
||||||
|
|
||||||
|
Result rc = BaseStorage.GetSize(out long baseStorageSize);
|
||||||
|
if (rc.IsFailure()) return rc;
|
||||||
|
|
||||||
|
long availableSize = baseStorageSize - offset;
|
||||||
|
long sizeToRead = Math.Min(destination.Length, availableSize);
|
||||||
|
|
||||||
|
return base.DoRead(offset, destination.Slice(0, (int)sizeToRead));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Result DoWrite(long offset, ReadOnlySpan<byte> source)
|
||||||
|
{
|
||||||
|
if (source.Length == 0)
|
||||||
|
return Result.Success;
|
||||||
|
|
||||||
|
Result rc = BaseStorage.GetSize(out long baseStorageSize);
|
||||||
|
if (rc.IsFailure()) return rc;
|
||||||
|
|
||||||
|
long availableSize = baseStorageSize - offset;
|
||||||
|
long sizeToWrite = Math.Min(source.Length, availableSize);
|
||||||
|
|
||||||
|
return base.DoWrite(offset, source.Slice(0, (int)sizeToWrite));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue