From 26ac0ed975dd345ed5fcabd43926b5aa9239a836 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Wed, 8 May 2019 18:05:37 -0500 Subject: [PATCH] Fix bugs when running out of save data space --- src/LibHac/IO/Save/AllocationTable.cs | 12 ++++++------ src/LibHac/IO/Save/SaveDataFileSystemCore.cs | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/LibHac/IO/Save/AllocationTable.cs b/src/LibHac/IO/Save/AllocationTable.cs index e986dadf..a10a5b80 100644 --- a/src/LibHac/IO/Save/AllocationTable.cs +++ b/src/LibHac/IO/Save/AllocationTable.cs @@ -154,27 +154,27 @@ namespace LibHac.IO.Save public int Trim(int listHeadBlockIndex, int newListLength) { int blocksRemaining = newListLength; - int next = BlockToEntryIndex(listHeadBlockIndex); + int nextEntry = BlockToEntryIndex(listHeadBlockIndex); int listAIndex = -1; int listBIndex = -1; while (blocksRemaining > 0) { - if (next < 0) + if (nextEntry == 0) { 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) { listAIndex = currentEntryIndex; - listBIndex = next; + listBIndex = nextEntry; } else if (segmentLength > blocksRemaining) { diff --git a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs index e5cdc715..beb45bb0 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs @@ -39,6 +39,11 @@ namespace LibHac.IO.Save int blockCount = (int)Util.DivideByRoundUp(size, AllocationTable.Header.BlockSize); 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 }; FileTable.AddFile(path, ref fileEntry);