From 47ba61db459bd945421999c0e65423e602d8831c Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Wed, 8 May 2019 16:27:48 -0500 Subject: [PATCH] Add DeleteDirectory to savedata --- .../IO/Save/HierarchicalSaveFileTable.cs | 53 +++++++++++++++++++ src/LibHac/IO/Save/SaveDataFileSystemCore.cs | 4 +- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs b/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs index 13064da8..4d9c7d25 100644 --- a/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs +++ b/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs @@ -200,6 +200,59 @@ namespace LibHac.IO.Save throw new FileNotFoundException(); } + public void DeleteDirectory(string path) + { + path = PathTools.Normalize(path); + ReadOnlySpan pathBytes = Util.GetUtf8Bytes(path); + + FindPathRecursive(pathBytes, out SaveEntryKey key); + int parentIndex = key.Parent; + + DirectoryTable.GetValue(parentIndex, out TableEntry parentEntry); + + int toDeleteIndex = DirectoryTable.GetIndexFromKey(ref key).Index; + if (toDeleteIndex < 0) throw new DirectoryNotFoundException(); + + DirectoryTable.GetValue(toDeleteIndex, out TableEntry toDeleteEntry); + + if (toDeleteEntry.Value.NextDirectory != 0 || toDeleteEntry.Value.NextFile != 0) + { + throw new IOException("Directory is not empty."); + } + + if (parentEntry.Value.NextDirectory == toDeleteIndex) + { + parentEntry.Value.NextDirectory = toDeleteEntry.NextSibling; + DirectoryTable.SetValue(parentIndex, ref parentEntry); + DirectoryTable.Remove(ref key); + return; + } + + int prevIndex = parentEntry.Value.NextDirectory; + DirectoryTable.GetValue(prevIndex, out TableEntry prevEntry); + int curIndex = prevEntry.NextSibling; + + while (curIndex != 0) + { + DirectoryTable.GetValue(curIndex, out TableEntry curEntry); + + if (curIndex == toDeleteIndex) + { + prevEntry.NextSibling = curEntry.NextSibling; + DirectoryTable.SetValue(prevIndex, ref prevEntry); + + DirectoryTable.Remove(ref key); + return; + } + + prevIndex = curIndex; + prevEntry = curEntry; + curIndex = prevEntry.NextSibling; + } + + throw new DirectoryNotFoundException(); + } + public bool TryOpenDirectory(string path, out SaveFindPosition position) { if (!FindPathRecursive(Util.GetUtf8Bytes(path), out SaveEntryKey key)) diff --git a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs index 47f3c177..e5cdc715 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs @@ -46,7 +46,9 @@ namespace LibHac.IO.Save public void DeleteDirectory(string path) { - throw new NotImplementedException(); + path = PathTools.Normalize(path); + + FileTable.DeleteDirectory(path); } public void DeleteDirectoryRecursively(string path)