Fix bugs when running out of save data space

This commit is contained in:
Alex Barney 2019-05-08 18:05:37 -05:00
parent 47ba61db45
commit 26ac0ed975
2 changed files with 11 additions and 6 deletions

View file

@ -154,27 +154,27 @@ namespace LibHac.IO.Save
public int Trim(int listHeadBlockIndex, int newListLength) public int Trim(int listHeadBlockIndex, int newListLength)
{ {
int blocksRemaining = newListLength; int blocksRemaining = newListLength;
int next = BlockToEntryIndex(listHeadBlockIndex); int nextEntry = BlockToEntryIndex(listHeadBlockIndex);
int listAIndex = -1; int listAIndex = -1;
int listBIndex = -1; int listBIndex = -1;
while (blocksRemaining > 0) while (blocksRemaining > 0)
{ {
if (next < 0) if (nextEntry == 0)
{ {
return -1; return -1;
} }
int currentEntryIndex = next; int currentEntryIndex = nextEntry;
ReadEntry(EntryIndexToBlock(currentEntryIndex), out next, out int _, out int segmentLength); ReadEntry(EntryIndexToBlock(currentEntryIndex), out int nextBlock, out int _, out int segmentLength);
next = BlockToEntryIndex(next); nextEntry = BlockToEntryIndex(nextBlock);
if (segmentLength == blocksRemaining) if (segmentLength == blocksRemaining)
{ {
listAIndex = currentEntryIndex; listAIndex = currentEntryIndex;
listBIndex = next; listBIndex = nextEntry;
} }
else if (segmentLength > blocksRemaining) else if (segmentLength > blocksRemaining)
{ {

View file

@ -39,6 +39,11 @@ namespace LibHac.IO.Save
int blockCount = (int)Util.DivideByRoundUp(size, AllocationTable.Header.BlockSize); int blockCount = (int)Util.DivideByRoundUp(size, AllocationTable.Header.BlockSize);
int startBlock = AllocationTable.Allocate(blockCount); int startBlock = AllocationTable.Allocate(blockCount);
if (startBlock == -1)
{
throw new IOException("Not enough available space to create file.");
}
var fileEntry = new SaveFileInfo { StartBlock = startBlock, Length = size }; var fileEntry = new SaveFileInfo { StartBlock = startBlock, Length = size };
FileTable.AddFile(path, ref fileEntry); FileTable.AddFile(path, ref fileEntry);