diff --git a/src/LibHac.Nand/FatFileSystemProvider.cs b/src/LibHac.Nand/FatFileSystemProvider.cs index d1cfb2c2..3c5f9d87 100644 --- a/src/LibHac.Nand/FatFileSystemProvider.cs +++ b/src/LibHac.Nand/FatFileSystemProvider.cs @@ -20,6 +20,28 @@ namespace LibHac.Nand Fs.DeleteDirectory(path); } + public void DeleteDirectoryRecursively(string path) + { + path = ToDiscUtilsPath(PathTools.Normalize(path)); + + Fs.DeleteDirectory(path, true); + } + + public void CleanDirectoryRecursively(string path) + { + path = ToDiscUtilsPath(PathTools.Normalize(path)); + + foreach (string file in Fs.GetFiles(path)) + { + Fs.DeleteFile(file); + } + + foreach (string file in Fs.GetDirectories(path)) + { + Fs.DeleteDirectory(file, true); + } + } + public void DeleteFile(string path) { path = ToDiscUtilsPath(PathTools.Normalize(path)); diff --git a/src/LibHac/IO/AesXtsFileSystem.cs b/src/LibHac/IO/AesXtsFileSystem.cs index 021f158d..c20f5262 100644 --- a/src/LibHac/IO/AesXtsFileSystem.cs +++ b/src/LibHac/IO/AesXtsFileSystem.cs @@ -64,6 +64,16 @@ namespace LibHac.IO BaseFileSystem.DeleteDirectory(path); } + public void DeleteDirectoryRecursively(string path) + { + BaseFileSystem.DeleteDirectoryRecursively(path); + } + + public void CleanDirectoryRecursively(string path) + { + BaseFileSystem.CleanDirectoryRecursively(path); + } + public void DeleteFile(string path) { BaseFileSystem.DeleteFile(path); diff --git a/src/LibHac/IO/ConcatenationFileSystem.cs b/src/LibHac/IO/ConcatenationFileSystem.cs index 4b539e95..c99627d1 100644 --- a/src/LibHac/IO/ConcatenationFileSystem.cs +++ b/src/LibHac/IO/ConcatenationFileSystem.cs @@ -91,6 +91,24 @@ namespace LibHac.IO BaseFileSystem.DeleteDirectory(path); } + public void DeleteDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + + if (IsConcatenationFile(path)) throw new DirectoryNotFoundException(); + + BaseFileSystem.DeleteDirectoryRecursively(path); + } + + public void CleanDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + + if (IsConcatenationFile(path)) throw new DirectoryNotFoundException(); + + BaseFileSystem.CleanDirectoryRecursively(path); + } + public void DeleteFile(string path) { path = PathTools.Normalize(path); @@ -220,7 +238,7 @@ namespace LibHac.IO public void QueryEntry(Span outBuffer, ReadOnlySpan inBuffer, string path, QueryId queryId) { - if(queryId != QueryId.MakeConcatFile) throw new NotSupportedException(); + if (queryId != QueryId.MakeConcatFile) throw new NotSupportedException(); SetConcatenationFileAttribute(path); } diff --git a/src/LibHac/IO/FileSystemExtensions.cs b/src/LibHac/IO/FileSystemExtensions.cs index bccb6a58..da982524 100644 --- a/src/LibHac/IO/FileSystemExtensions.cs +++ b/src/LibHac/IO/FileSystemExtensions.cs @@ -182,6 +182,27 @@ namespace LibHac.IO { fs.QueryEntry(Span.Empty, Span.Empty, path, QueryId.MakeConcatFile); } + + public static void CleanDirectoryRecursivelyGeneric(IDirectory directory) + { + IFileSystem fs = directory.ParentFileSystem; + + foreach (DirectoryEntry entry in directory.Read()) + { + if (entry.Type == DirectoryEntryType.Directory) + { + string subPath = directory.FullPath + '/' + entry.Name; + IDirectory subDir = fs.OpenDirectory(subPath, OpenDirectoryMode.All); + + CleanDirectoryRecursivelyGeneric(subDir); + fs.DeleteDirectory(subPath); + } + else if (entry.Type == DirectoryEntryType.File) + { + fs.DeleteFile(directory.FullPath + '/' + entry.Name); + } + } + } } [Flags] diff --git a/src/LibHac/IO/IFileSystem.cs b/src/LibHac/IO/IFileSystem.cs index 1d1a0ba5..19b74dc2 100644 --- a/src/LibHac/IO/IFileSystem.cs +++ b/src/LibHac/IO/IFileSystem.cs @@ -33,9 +33,9 @@ namespace LibHac.IO /// An I/O error occurred while deleting the directory. void DeleteDirectory(string path); - //void DeleteDirectoryRecursively(string path); + void DeleteDirectoryRecursively(string path); - //void CleanDirectoryRecursively(string path); + void CleanDirectoryRecursively(string path); /// /// Deletes the specified file. diff --git a/src/LibHac/IO/LayeredFileSystem.cs b/src/LibHac/IO/LayeredFileSystem.cs index 57f7c34e..1562e2bf 100644 --- a/src/LibHac/IO/LayeredFileSystem.cs +++ b/src/LibHac/IO/LayeredFileSystem.cs @@ -133,6 +133,8 @@ namespace LibHac.IO public void CreateDirectory(string path) => throw new NotSupportedException(); public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException(); public void DeleteDirectory(string path) => throw new NotSupportedException(); + public void DeleteDirectoryRecursively(string path) => throw new NotSupportedException(); + public void CleanDirectoryRecursively(string path) => throw new NotSupportedException(); public void DeleteFile(string path) => throw new NotSupportedException(); public void RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); diff --git a/src/LibHac/IO/LocalFileSystem.cs b/src/LibHac/IO/LocalFileSystem.cs index 489c0233..0f2206ca 100644 --- a/src/LibHac/IO/LocalFileSystem.cs +++ b/src/LibHac/IO/LocalFileSystem.cs @@ -75,8 +75,30 @@ namespace LibHac.IO { path = PathTools.Normalize(path); - string resolveLocalPath = ResolveLocalPath(path); - Directory.Delete(resolveLocalPath); + Directory.Delete(ResolveLocalPath(path)); + } + + public void DeleteDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + + Directory.Delete(ResolveLocalPath(path), true); + } + + public void CleanDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + string localPath = ResolveLocalPath(path); + + foreach (string file in Directory.EnumerateFiles(localPath)) + { + File.Delete(file); + } + + foreach (string dir in Directory.EnumerateDirectories(localPath)) + { + Directory.Delete(dir, true); + } } public void DeleteFile(string path) diff --git a/src/LibHac/IO/PartitionFileSystem.cs b/src/LibHac/IO/PartitionFileSystem.cs index ed36a7d3..239bc8b3 100644 --- a/src/LibHac/IO/PartitionFileSystem.cs +++ b/src/LibHac/IO/PartitionFileSystem.cs @@ -78,6 +78,8 @@ namespace LibHac.IO public void CreateDirectory(string path) => throw new NotSupportedException(); public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException(); public void DeleteDirectory(string path) => throw new NotSupportedException(); + public void DeleteDirectoryRecursively(string path) => throw new NotSupportedException(); + public void CleanDirectoryRecursively(string path) => throw new NotSupportedException(); public void DeleteFile(string path) => throw new NotSupportedException(); public void RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); diff --git a/src/LibHac/IO/RomFs/RomFsFileSystem.cs b/src/LibHac/IO/RomFs/RomFsFileSystem.cs index 60b60ca7..9addfbdd 100644 --- a/src/LibHac/IO/RomFs/RomFsFileSystem.cs +++ b/src/LibHac/IO/RomFs/RomFsFileSystem.cs @@ -86,6 +86,8 @@ namespace LibHac.IO.RomFs public void CreateDirectory(string path) => throw new NotSupportedException(); public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException(); public void DeleteDirectory(string path) => throw new NotSupportedException(); + public void DeleteDirectoryRecursively(string path) => throw new NotSupportedException(); + public void CleanDirectoryRecursively(string path) => throw new NotSupportedException(); public void DeleteFile(string path) => throw new NotSupportedException(); public void RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); diff --git a/src/LibHac/IO/Save/SaveDataFileSystem.cs b/src/LibHac/IO/Save/SaveDataFileSystem.cs index a1652d44..223d70be 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystem.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystem.cs @@ -157,6 +157,16 @@ namespace LibHac.IO.Save SaveDataFileSystemCore.DeleteDirectory(path); } + public void DeleteDirectoryRecursively(string path) + { + SaveDataFileSystemCore.DeleteDirectoryRecursively(path); + } + + public void CleanDirectoryRecursively(string path) + { + SaveDataFileSystemCore.CleanDirectoryRecursively(path); + } + public void DeleteFile(string path) { SaveDataFileSystemCore.DeleteFile(path); diff --git a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs index d011961a..47f3c177 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs @@ -49,6 +49,22 @@ namespace LibHac.IO.Save throw new NotImplementedException(); } + public void DeleteDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + + CleanDirectoryRecursively(path); + DeleteDirectory(path); + } + + public void CleanDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + + IDirectory dir = OpenDirectory(path, OpenDirectoryMode.All); + FileSystemExtensions.CleanDirectoryRecursivelyGeneric(dir); + } + public void DeleteFile(string path) { path = PathTools.Normalize(path); diff --git a/src/LibHac/IO/SubdirectoryFileSystem.cs b/src/LibHac/IO/SubdirectoryFileSystem.cs index e240b028..03ae9f39 100644 --- a/src/LibHac/IO/SubdirectoryFileSystem.cs +++ b/src/LibHac/IO/SubdirectoryFileSystem.cs @@ -40,6 +40,20 @@ namespace LibHac.IO ParentFileSystem.DeleteDirectory(ResolveFullPath(path)); } + public void DeleteDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + + ParentFileSystem.DeleteDirectoryRecursively(ResolveFullPath(path)); + } + + public void CleanDirectoryRecursively(string path) + { + path = PathTools.Normalize(path); + + ParentFileSystem.CleanDirectoryRecursively(ResolveFullPath(path)); + } + public void DeleteFile(string path) { path = PathTools.Normalize(path);