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)