mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Add DeleteDirectory to savedata
This commit is contained in:
parent
3c45e7e71b
commit
47ba61db45
2 changed files with 56 additions and 1 deletions
|
@ -200,6 +200,59 @@ namespace LibHac.IO.Save
|
|||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
public void DeleteDirectory(string path)
|
||||
{
|
||||
path = PathTools.Normalize(path);
|
||||
ReadOnlySpan<byte> pathBytes = Util.GetUtf8Bytes(path);
|
||||
|
||||
FindPathRecursive(pathBytes, out SaveEntryKey key);
|
||||
int parentIndex = key.Parent;
|
||||
|
||||
DirectoryTable.GetValue(parentIndex, out TableEntry<SaveFindPosition> parentEntry);
|
||||
|
||||
int toDeleteIndex = DirectoryTable.GetIndexFromKey(ref key).Index;
|
||||
if (toDeleteIndex < 0) throw new DirectoryNotFoundException();
|
||||
|
||||
DirectoryTable.GetValue(toDeleteIndex, out TableEntry<SaveFindPosition> 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<SaveFindPosition> prevEntry);
|
||||
int curIndex = prevEntry.NextSibling;
|
||||
|
||||
while (curIndex != 0)
|
||||
{
|
||||
DirectoryTable.GetValue(curIndex, out TableEntry<SaveFindPosition> 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))
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue