Fix bug in CachedStorage that would drop writes

If block 0 of the storage was written to before the cache was filled and then another block was accessed, any writes to block 0 would be lost.
This commit is contained in:
Alex Barney 2019-05-10 14:49:23 -05:00
parent b83fb6c7fc
commit b9236b973a

View file

@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Collections.Generic;
namespace LibHac.IO
@ -23,15 +22,14 @@ namespace LibHac.IO
for (int i = 0; i < cacheSize; i++)
{
// todo why is this rented?
var block = new CacheBlock { Buffer = ArrayPool<byte>.Shared.Rent(blockSize) };
var block = new CacheBlock { Buffer = new byte[blockSize], Index = -1 };
Blocks.AddLast(block);
}
}
public CachedStorage(SectorStorage baseStorage, int cacheSize, bool leaveOpen)
: this(baseStorage, baseStorage.SectorSize, cacheSize, leaveOpen) { }
protected override void ReadImpl(Span<byte> destination, long offset)
{
long remaining = destination.Length;
@ -124,7 +122,11 @@ namespace LibHac.IO
CacheBlock block = node.Value;
Blocks.RemoveLast();
BlockDict.Remove(block.Index);
if (block.Index != -1)
{
BlockDict.Remove(block.Index);
}
FlushBlock(block);
ReadBlock(block, blockIndex);