diff --git a/src/LibHac/IO/Save/AllocationTableStorage.cs b/src/LibHac/IO/Save/AllocationTableStorage.cs index 9a061088..172539bc 100644 --- a/src/LibHac/IO/Save/AllocationTableStorage.cs +++ b/src/LibHac/IO/Save/AllocationTableStorage.cs @@ -1,4 +1,5 @@ using System; +using System.IO; namespace LibHac.IO.Save { @@ -92,6 +93,8 @@ namespace LibHac.IO.Save if (oldBlockCount == 0) { InitialBlock = Fat.Allocate(newBlockCount); + if (InitialBlock == -1) throw new IOException("Not enough space to resize file."); + _length = newBlockCount * BlockSize; return; @@ -101,7 +104,7 @@ namespace LibHac.IO.Save { Fat.Free(InitialBlock); - InitialBlock = -1; + InitialBlock = int.MinValue; _length = 0; return; @@ -110,6 +113,8 @@ namespace LibHac.IO.Save if (newBlockCount > oldBlockCount) { int newBlocks = Fat.Allocate(newBlockCount - oldBlockCount); + if (InitialBlock == -1) throw new IOException("Not enough space to resize file."); + Fat.Join(InitialBlock, newBlocks); } else diff --git a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs index 32f25abd..0fb2f3ee 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs @@ -38,6 +38,14 @@ namespace LibHac.IO.Save { path = PathTools.Normalize(path); + if (size == 0) + { + var emptyFileEntry = new SaveFileInfo { StartBlock = int.MinValue, Length = size }; + FileTable.AddFile(path, ref emptyFileEntry); + + return; + } + int blockCount = (int)Util.DivideByRoundUp(size, AllocationTable.Header.BlockSize); int startBlock = AllocationTable.Allocate(blockCount);